File size: 3,434 Bytes
f957bce
281c0ae
30e6416
281c0ae
 
 
 
 
 
 
e1de535
281c0ae
 
74c93be
281c0ae
 
 
 
7291a91
281c0ae
30e6416
281c0ae
30e6416
 
be823ee
281c0ae
 
 
e4cb7b4
281c0ae
f957bce
35e4839
f957bce
281c0ae
f957bce
281c0ae
7291a91
281c0ae
30e6416
281c0ae
30e6416
281c0ae
30e6416
 
281c0ae
 
 
 
 
 
 
 
 
30e6416
281c0ae
 
 
 
 
 
 
 
 
 
 
 
f957bce
281c0ae
 
 
 
 
 
 
 
30e6416
281c0ae
7291a91
f957bce
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
import gradio as gr
from sentence_transformers import SentenceTransformer
from transformers import GPT2LMHeadModel, GPT2Tokenizer
from pinecone import Pinecone

# ===============================
# 🌐 اتصال به Pinecone
# ===============================
PINECONE_API_KEY = "pcsk_6p6AmJ_Qua4tQN69badNHEGZTj3tt5Bd7LiyiDGcXDj92LxSaBzK2ypYxTRx2rafTEJhjL"
PINECONE_INDEX_NAME = "tiyam-chat"

pc = Pinecone(api_key=PINECONE_API_KEY)
index = pc.Index(PINECONE_INDEX_NAME)

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

# ===============================
# 🧠 بارگذاری مدل GPT-2 برای تولید پاسخ
# ===============================
tokenizer = GPT2Tokenizer.from_pretrained("openai-community/gpt2")
gpt2_model = GPT2LMHeadModel.from_pretrained("openai-community/gpt2")

# ===============================
# 🔍 بازیابی پاسخ از Pinecone
# ===============================
def retrieve_answer(query, threshold=0.65, top_k=1):
    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:
        return result['matches'][0]['metadata'].get('answer', 'پاسخ یافت نشد')
    else:
        return None

# ===============================
# ✨ تولید پاسخ با GPT-2
# ===============================
def generate_response_with_gpt2(answer, question):
    prompt = f"پاسخ به سوال: {question} بر اساس اطلاعات: {answer}"
    input_ids = tokenizer.encode(prompt, return_tensors="pt", truncation=True, max_length=512)
    output_ids = gpt2_model.generate(input_ids, max_length=150, num_return_sequences=1, no_repeat_ngram_size=2, top_p=0.95, temperature=0.7)
    return tokenizer.decode(output_ids[0], skip_special_tokens=True)

# ===============================
# 💬 منطق نهایی پاسخ‌دهی
# ===============================
def final_answer(user_question):
    answer = retrieve_answer(user_question)

    if answer:
        return generate_response_with_gpt2(answer, user_question)
    else:
        # پاسخ عمومی برای سوالات چتی یا ناموجود
        general_prompts = {
            "سلام": "سلام! خوشحالیم که اینجایی 😊",
            "خوبی؟": "مرسی! من خوبم، شما چطورید؟",
            "مرسی": "خواهش می‌کنم، در خدمت شما هستیم."
        }
        for key in general_prompts:
            if key in user_question:
                return general_prompts[key]

        return "متأسفم، پاسخ دقیقی برای این سوال نداریم. لطفاً با ما تماس بگیرید."

# ===============================
# 🎛️ رابط Gradio
# ===============================
demo = gr.Interface(
    fn=final_answer,
    inputs=gr.Textbox(lines=2, label="سؤال شما"),
    outputs=gr.Textbox(label="پاسخ تیام"),
    title="💬 چت‌بات هوشمند تیام",
    description="سؤالات خود را از آژانس دیجیتال مارکتینگ تیام بپرسید. سیستم از ترکیب جستجوی دقیق و تولید پاسخ طبیعی استفاده می‌کند."
)

demo.launch()