Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,75 @@
|
|
1 |
# app.py
|
2 |
-
|
3 |
-
|
4 |
import json
|
|
|
5 |
import gradio as gr
|
|
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
|
|
|
|
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=
|
16 |
-
index = pc.Index(
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
#
|
19 |
def retrieve_answer(query, threshold=0.65, top_k=3):
|
20 |
-
query_embedding =
|
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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
def chat_interface(question):
|
32 |
-
|
|
|
|
|
33 |
|
34 |
-
demo = gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
demo.launch()
|
|
|
1 |
# app.py
|
2 |
+
|
3 |
+
import os
|
4 |
import json
|
5 |
+
import torch
|
6 |
import gradio as gr
|
7 |
+
from pinecone import Pinecone
|
8 |
+
from sentence_transformers import SentenceTransformer
|
9 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
10 |
|
11 |
+
# 🔐 گرفتن کلیدها از Environment Variables (Secrets در Hugging Face)
|
12 |
+
PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY")
|
13 |
+
PINECONE_INDEX_NAME = os.environ.get("INDEX_NAME", "tiyam-chat")
|
14 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
15 |
|
16 |
+
# 🔹 بارگذاری مدل embedding
|
17 |
+
embedding_model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
|
18 |
+
|
19 |
+
# 🔹 بارگذاری دادهها (اختیاری برای تست لوکال)
|
20 |
with open("tiyam_qa_data.json", "r", encoding="utf-8") as f:
|
21 |
data = json.load(f)
|
22 |
|
23 |
+
# 🔹 اتصال به Pinecone
|
24 |
+
pc = Pinecone(api_key=PINECONE_API_KEY)
|
25 |
+
index = pc.Index(PINECONE_INDEX_NAME)
|
26 |
+
|
27 |
+
# 🔹 بارگذاری مدل GEMMA
|
28 |
+
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b-it", token=HF_TOKEN)
|
29 |
+
model = AutoModelForCausalLM.from_pretrained("google/gemma-2b-it", token=HF_TOKEN)
|
30 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
31 |
+
model = model.to(device)
|
32 |
|
33 |
+
# 🔹 گرفتن پاسخ از Pinecone
|
34 |
def retrieve_answer(query, threshold=0.65, top_k=3):
|
35 |
+
query_embedding = embedding_model.encode([query])[0]
|
36 |
result = index.query(vector=query_embedding.tolist(), top_k=top_k, include_metadata=True)
|
37 |
|
38 |
if result['matches'] and result['matches'][0]['score'] > threshold:
|
|
|
39 |
metadata = result['matches'][0]['metadata']
|
40 |
return metadata.get('answer', 'پاسخ یافت نشد')
|
41 |
else:
|
42 |
return "متأسفم، پاسخ دقیقی برای این سوال نداریم. لطفاً با ما تماس بگیرید."
|
43 |
|
44 |
+
# 🔹 بازنویسی پاسخ با GEMMA
|
45 |
+
def rewrite_answer(question, retrieved_answer):
|
46 |
+
prompt = f"""سؤال: {question}
|
47 |
+
پاسخ اولیه: {retrieved_answer}
|
48 |
+
پاسخ نهایی را به زبان طبیعی، حرفهای و دوستانه بازنویسی کن:"""
|
49 |
+
|
50 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
51 |
+
outputs = model.generate(
|
52 |
+
**inputs,
|
53 |
+
max_new_tokens=150,
|
54 |
+
temperature=0.7,
|
55 |
+
do_sample=True,
|
56 |
+
top_p=0.9
|
57 |
+
)
|
58 |
+
final_answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
59 |
+
return final_answer.replace(prompt, "").strip()
|
60 |
+
|
61 |
+
# 🔹 رابط Gradio
|
62 |
def chat_interface(question):
|
63 |
+
raw_answer = retrieve_answer(question)
|
64 |
+
final_answer = rewrite_answer(question, raw_answer)
|
65 |
+
return final_answer
|
66 |
|
67 |
+
demo = gr.Interface(
|
68 |
+
fn=chat_interface,
|
69 |
+
inputs="text",
|
70 |
+
outputs="text",
|
71 |
+
title="💬 چتبات هوشمند تیام",
|
72 |
+
description="سؤالات خود درباره خدمات بازاریابی دیجیتال تیام را بپرسید."
|
73 |
+
)
|
74 |
|
75 |
demo.launch()
|