File size: 1,614 Bytes
04ebf7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from sentence_transformers import SentenceTransformer
from pinecone import Pinecone

import json
import os

# اتصال به Pinecone
api_key = "pcsk_6p6AmJ_Qua4tQN69badNHEGZTj3tt5Bd7LiyiDGcXDj92LxSaBzK2ypYxTRx2rafTEJhjL"  # 🔐 کلید خودت رو اینجا بذار
index_name = "tiyam-chat"
region = "us-east-1"

pc = Pinecone(api_key=api_key)
index = pc.Index(index_name)

# بارگذاری مدل
model = SentenceTransformer('sentence-transformers/paraphrase-multilingual-mpnet-base-v2')

# بارگذاری داده‌ها از JSON (فایل کنار app.py باشه)
with open("tiyam_qa_data.json", "r", encoding="utf-8") as f:
    data = json.load(f)

# تابع پاسخ‌دهی
def chatbot_fn(message, history):
    query_vector = model.encode(message).tolist()
    result = index.query(vector=query_vector, top_k=1, include_metadata=True)

    if result and result.matches:
        top_match = result.matches[0]
        score = top_match.score
        answer = top_match.metadata.get("پاسخ", "پاسخی یافت نشد.")
        if score < 0.6:
            return "متأسفم، اطلاعات کافی ندارم. لطفاً با ما تماس بگیرید."
        return answer
    else:
        return "متأسفم، متوجه سؤال نشدم. لطفاً واضح‌تر بپرسید."

# رابط چت
demo = gr.ChatInterface(
    fn=chatbot_fn,
    title="🤖 چت‌بات هوشمند تیام",
    description="سؤالات خود را درباره آژانس دیجیتال مارکتینگ تیام بپرسید!",
    theme="soft"
)

demo.launch()