File size: 3,189 Bytes
9ad95ef
0f5d2ed
 
9ad95ef
0f5d2ed
9ad95ef
8bb4acb
0f5d2ed
 
cd32b4c
281c0ae
cd32b4c
0f5d2ed
 
 
74c93be
8bb4acb
 
0f5d2ed
cd32b4c
8bb4acb
 
d2af11c
8bb4acb
 
cd32b4c
9ad95ef
 
7291a91
0f5d2ed
 
 
 
cd32b4c
 
 
0f5d2ed
 
be823ee
cd32b4c
08f847c
0f5d2ed
f957bce
35e4839
f957bce
9ad95ef
 
f957bce
9ad95ef
7291a91
cd32b4c
0f5d2ed
 
 
 
 
 
 
 
cd32b4c
0f5d2ed
 
 
 
 
 
 
cd32b4c
9ad95ef
cd32b4c
0f5d2ed
cd32b4c
0f5d2ed
cd32b4c
0f5d2ed
f957bce
0f5d2ed
 
 
 
 
 
 
7291a91
84a7b0a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# app.py

import os
import json
import torch
import gradio as gr
from huggingface_hub import login
from pinecone import Pinecone
from sentence_transformers import SentenceTransformer
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

# 📦 گرفتن سکرت‌ها
PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY")
PINECONE_INDEX_NAME = os.environ.get("INDEX_NAME", "tiyam-chat")
HF_TOKEN = os.environ.get("HF_TOKEN")

if not HF_TOKEN:
    raise ValueError("❌ سکرت HF_TOKEN یافت نشد. لطفاً آن را در Settings > Secrets ثبت کنید.")

# 🔐 ورود به Hugging Face
login(token=HF_TOKEN)

# 🔹 بارگذاری مدل embedding
embedding_model = SentenceTransformer('sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2')

# 🔹 بارگذاری داده اولیه (اختیاری)
with open("tiyam_qa_data.json", "r", encoding="utf-8") as f:
    data = json.load(f)

# 🔹 اتصال به Pinecone
pc = Pinecone(api_key=PINECONE_API_KEY)
index = pc.Index(PINECONE_INDEX_NAME)

# 🔹 بارگذاری مدل سبک mt5-small برای بازنویسی
tokenizer = AutoTokenizer.from_pretrained("google/mt5-small", token=HF_TOKEN)
model = AutoModelForSeq2SeqLM.from_pretrained("google/mt5-small", token=HF_TOKEN)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)

# 🔍 گرفتن پاسخ اولیه از Pinecone
def retrieve_answer(query, threshold=0.65, top_k=3):
    query_embedding = embedding_model.encode([query])[0]
    result = index.query(vector=query_embedding.tolist(), top_k=top_k, include_metadata=True)

    if result['matches'] and result['matches'][0]['score'] > threshold:
        metadata = result['matches'][0]['metadata']
        return metadata.get('answer', 'پاسخ یافت نشد')
    else:
        return "متأسفم، پاسخ دقیقی برای این سوال نداریم. لطفاً با ما تماس بگیرید."

# 🧠 بازنویسی پاسخ با MT5 کوچک
def rewrite_answer(question, retrieved_answer):
    prompt = f"""سؤال: {question}
پاسخ اولیه: {retrieved_answer}
پاسخ نهایی را به زبان طبیعی، حرفه‌ای و دوستانه بازنویسی کن:"""

    inputs = tokenizer(prompt, return_tensors="pt").to(device)
    outputs = model.generate(
        **inputs,
        max_new_tokens=96,
        temperature=0.7,
        do_sample=True,
        top_p=0.9
    )
    final_answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return final_answer.replace(prompt, "").strip()

# ✨ رابط نهایی
def chat_interface(question):
    print("📥 سوال:", question)
    raw_answer = retrieve_answer(question)
    print("📤 پاسخ اولیه:", raw_answer)
    final_answer = rewrite_answer(question, raw_answer)
    print("✅ پاسخ نهایی:", final_answer)
    return final_answer

demo = gr.Interface(
    fn=chat_interface,
    inputs="text",
    outputs="text",
    title="💬 چت‌بات هوشمند تیام",
    description="سؤالات خود درباره خدمات بازاریابی دیجیتال تیام را بپرسید."
)

demo.launch()