Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,47 @@
|
|
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 |
-
# اتصال
|
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 |
-
|
18 |
-
|
19 |
-
#
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
query_vector =
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
39 |
demo = gr.ChatInterface(
|
40 |
-
fn=
|
41 |
-
title="🤖 چتبات هوشمند تیام",
|
42 |
-
description="
|
43 |
theme="soft"
|
44 |
)
|
45 |
|
|
|
1 |
import gradio as gr
|
2 |
from sentence_transformers import SentenceTransformer
|
3 |
from pinecone import Pinecone
|
4 |
+
from transformers import pipeline
|
5 |
import json
|
|
|
6 |
|
7 |
+
# پیکربندی اتصال Pinecone
|
8 |
+
api_key = "pcsk_6p6AmJ_Qua4tQN69badNHEGZTj3tt5Bd7LiyiDGcXDj92LxSaBzK2ypYxTRx2rafTEJhjL"
|
9 |
index_name = "tiyam-chat"
|
10 |
region = "us-east-1"
|
11 |
|
12 |
+
# اتصال به Pinecone
|
13 |
pc = Pinecone(api_key=api_key)
|
14 |
index = pc.Index(index_name)
|
15 |
|
16 |
+
# مدل embedding برای بازیابی
|
17 |
+
embedder = SentenceTransformer("sentence-transformers/paraphrase-multilingual-mpnet-base-v2")
|
18 |
+
|
19 |
+
# مدل زبانی برای تولید پاسخ طبیعی
|
20 |
+
generator = pipeline("text-generation", model="HooshvareLab/gpt2-fa")
|
21 |
+
|
22 |
+
# تابع چت RAG
|
23 |
+
def rag_chatbot(message, history):
|
24 |
+
# مرحله 1: تبدیل سؤال به بردار
|
25 |
+
query_vector = embedder.encode(message).tolist()
|
26 |
+
|
27 |
+
# مرحله 2: جستجو در Pinecone
|
28 |
+
result = index.query(vector=query_vector, top_k=3, include_metadata=True)
|
29 |
+
context = "\n".join([match.metadata.get("پاسخ", "") for match in result.matches])
|
30 |
+
|
31 |
+
# مرحله 3: ساخت پرامپت ترکیبی
|
32 |
+
prompt = f"سؤال: {message}\nاطلاعات:\n{context}\nپاسخ:"
|
33 |
+
|
34 |
+
# مرحله 4: تولید پاسخ طبیعی
|
35 |
+
response = generator(prompt, max_new_tokens=100, do_sample=True, temperature=0.7)[0]['generated_text']
|
36 |
+
answer = response.split("پاسخ:")[-1].strip()
|
37 |
+
|
38 |
+
return answer
|
39 |
+
|
40 |
+
# رابط Gradio
|
41 |
demo = gr.ChatInterface(
|
42 |
+
fn=rag_chatbot,
|
43 |
+
title="🤖 چتبات هوشمند تیام (نسخه RAG)",
|
44 |
+
description="پاسخگویی ترکیبی با دادههای شرکت و تولید متنی هوشمند",
|
45 |
theme="soft"
|
46 |
)
|
47 |
|