Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,36 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
-
from sentence_transformers import SentenceTransformer, util
|
5 |
from pinecone import Pinecone
|
|
|
6 |
import gradio as gr
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
|
11 |
-
PINECONE_INDEX_NAME = os.getenv("PINECONE_INDEX_NAME")
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
# مدل زبانی GPT2 فارسی
|
17 |
-
tokenizer = AutoTokenizer.from_pretrained("HooshvareLab/gpt2-fa")
|
18 |
-
model = AutoModelForCausalLM.from_pretrained("HooshvareLab/gpt2-fa")
|
19 |
|
20 |
# اتصال به Pinecone
|
21 |
-
pc = Pinecone(api_key=
|
22 |
-
index = pc.Index(
|
23 |
-
|
24 |
-
#
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
return
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
max_new_tokens=30, # کمک به تسریع پاسخگویی
|
44 |
-
temperature=0.7,
|
45 |
-
do_sample=True,
|
46 |
-
pad_token_id=tokenizer.eos_token_id
|
47 |
-
)
|
48 |
-
|
49 |
-
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
50 |
-
|
51 |
-
# جدا کردن پاسخ تولیدی از prompt
|
52 |
-
return response.replace(prompt, "").strip()
|
53 |
-
|
54 |
-
# رابط کاربری Gradio
|
55 |
-
iface = gr.Interface(
|
56 |
-
fn=generate_response,
|
57 |
-
inputs=gr.Textbox(label="question", placeholder="سوال خود را وارد کنید"),
|
58 |
-
outputs=gr.Textbox(label="output"),
|
59 |
-
title="چتبات هوشمند تیام",
|
60 |
-
description="سوالات خود درباره خدمات دیجیتال مارکتینگ تیام را بپرسید"
|
61 |
-
)
|
62 |
-
|
63 |
-
iface.launch()
|
|
|
1 |
+
# app.py
|
2 |
+
from sentence_transformers import SentenceTransformer
|
|
|
|
|
3 |
from pinecone import Pinecone
|
4 |
+
import json
|
5 |
import gradio as gr
|
6 |
|
7 |
+
# بارگذاری مدل embedding
|
8 |
+
model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
|
|
|
|
|
9 |
|
10 |
+
# بارگذاری داده محلی برای نمایش اولیه (در صورت نیاز)
|
11 |
+
with open("tiyam_qa_data.json", "r", encoding="utf-8") as f:
|
12 |
+
data = json.load(f)
|
|
|
|
|
|
|
13 |
|
14 |
# اتصال به Pinecone
|
15 |
+
pc = Pinecone(api_key="pcsk_6p6AmJ_Qua4tQN69badNHEGZTj3tt5Bd7LiyiDGcXDj92LxSaBzK2ypYxTRx2rafTEJhjL") # 🔐 جایگزین کن با کلیدت
|
16 |
+
index = pc.Index("tiyam-chat") # ایندکس از قبل ساختهشده
|
17 |
+
|
18 |
+
# تابع پاسخگو
|
19 |
+
def retrieve_answer(query, threshold=0.65, top_k=1):
|
20 |
+
query_embedding = model.encode([query])[0]
|
21 |
+
result = index.query(vector=query_embedding.tolist(), top_k=top_k, include_metadata=True)
|
22 |
+
|
23 |
+
if result['matches'] and result['matches'][0]['score'] > threshold:
|
24 |
+
print(f"📊 Similarity: {result['matches'][0]['score']:.3f}")
|
25 |
+
metadata = result['matches'][0]['metadata']
|
26 |
+
return metadata.get('answer', 'پاسخ یافت نشد')
|
27 |
+
else:
|
28 |
+
return "متأسفم، پاسخ دقیقی برای این سوال نداریم. لطفاً با ما تماس بگیرید."
|
29 |
+
|
30 |
+
# رابط Gradio
|
31 |
+
def chat_interface(question):
|
32 |
+
return retrieve_answer(question)
|
33 |
+
|
34 |
+
demo = gr.Interface(fn=chat_interface, inputs="text", outputs="text", title="چتبات تیام", description="سؤالات خود را از آژانس دیجیتال مارکتینگ تیام بپرسید.")
|
35 |
+
|
36 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|