Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from sentence_transformers import SentenceTransformer
|
3 |
+
from pinecone import Pinecone
|
4 |
+
|
5 |
+
import json
|
6 |
+
import os
|
7 |
+
|
8 |
+
# اتصال به Pinecone
|
9 |
+
api_key = "pcsk_6p6AmJ_Qua4tQN69badNHEGZTj3tt5Bd7LiyiDGcXDj92LxSaBzK2ypYxTRx2rafTEJhjL" # 🔐 کلید خودت رو اینجا بذار
|
10 |
+
index_name = "tiyam-chat"
|
11 |
+
region = "us-east-1"
|
12 |
+
|
13 |
+
pc = Pinecone(api_key=api_key)
|
14 |
+
index = pc.Index(index_name)
|
15 |
+
|
16 |
+
# بارگذاری مدل
|
17 |
+
model = SentenceTransformer('sentence-transformers/paraphrase-multilingual-mpnet-base-v2')
|
18 |
+
|
19 |
+
# بارگذاری دادهها از JSON (فایل کنار app.py باشه)
|
20 |
+
with open("tiyam_qa_data.json", "r", encoding="utf-8") as f:
|
21 |
+
data = json.load(f)
|
22 |
+
|
23 |
+
# تابع پاسخدهی
|
24 |
+
def chatbot_fn(message, history):
|
25 |
+
query_vector = model.encode(message).tolist()
|
26 |
+
result = index.query(vector=query_vector, top_k=1, include_metadata=True)
|
27 |
+
|
28 |
+
if result and result.matches:
|
29 |
+
top_match = result.matches[0]
|
30 |
+
score = top_match.score
|
31 |
+
answer = top_match.metadata.get("پاسخ", "پاسخی یافت نشد.")
|
32 |
+
if score < 0.6:
|
33 |
+
return "متأسفم، اطلاعات کافی ندارم. لطفاً با ما تماس بگیرید."
|
34 |
+
return answer
|
35 |
+
else:
|
36 |
+
return "متأسفم، متوجه سؤال نشدم. لطفاً واضحتر بپرسید."
|
37 |
+
|
38 |
+
# رابط چت
|
39 |
+
demo = gr.ChatInterface(
|
40 |
+
fn=chatbot_fn,
|
41 |
+
title="🤖 چتبات هوشمند تیام",
|
42 |
+
description="سؤالات خود را درباره آژانس دیجیتال مارکتینگ تیام بپرسید!",
|
43 |
+
theme="soft"
|
44 |
+
)
|
45 |
+
|
46 |
+
demo.launch()
|