Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,65 @@
|
|
1 |
-
|
|
|
2 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
|
|
4 |
tokenizer = AutoTokenizer.from_pretrained("SeyedAli/Persian-QA-Bert-V1")
|
5 |
model = AutoModelForQuestionAnswering.from_pretrained("SeyedAli/Persian-QA-Bert-V1")
|
6 |
|
|
|
|
|
|
|
|
|
|
|
7 |
def rag_chatbot(message, history):
|
8 |
-
greetings = ["سلام", "
|
9 |
if message.strip() in greetings:
|
10 |
return "سلام! چطور میتونم کمکتون کنم؟ 😊"
|
11 |
|
|
|
12 |
query_vector = embedder.encode(message).tolist()
|
13 |
result = index.query(vector=query_vector, top_k=3, include_metadata=True)
|
14 |
|
15 |
if not result['matches']:
|
16 |
return "متأسفم، اطلاعاتی در پایگاه داده پیدا نکردم."
|
17 |
|
18 |
-
context = "\n".join([match['metadata'].get("پاسخ", "") for match in result['matches']])
|
19 |
|
20 |
if not context.strip():
|
21 |
return "متأسفم، اطلاعات کافی در این زمینه ندارم."
|
22 |
|
23 |
-
#
|
24 |
inputs = tokenizer(message, context, return_tensors="pt", truncation=True, max_length=512)
|
25 |
with torch.no_grad():
|
26 |
outputs = model(**inputs)
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
)
|
33 |
|
34 |
-
if not answer.strip() or answer.strip()
|
35 |
return "متأسفم، نتونستم پاسخ دقیقی پیدا کنم."
|
36 |
|
37 |
return answer.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
import torch
|
4 |
+
from pinecone import Pinecone
|
5 |
+
from sentence_transformers import SentenceTransformer
|
6 |
+
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
|
7 |
+
|
8 |
+
# بارگذاری کلیدها
|
9 |
+
PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY")
|
10 |
+
INDEX_NAME = os.environ.get("INDEX_NAME")
|
11 |
+
|
12 |
+
# مدل embedding
|
13 |
+
embedder = SentenceTransformer("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
|
14 |
|
15 |
+
# مدل QA فارسی
|
16 |
tokenizer = AutoTokenizer.from_pretrained("SeyedAli/Persian-QA-Bert-V1")
|
17 |
model = AutoModelForQuestionAnswering.from_pretrained("SeyedAli/Persian-QA-Bert-V1")
|
18 |
|
19 |
+
# اتصال به پایگاه داده Pinecone
|
20 |
+
pc = Pinecone(api_key=PINECONE_API_KEY)
|
21 |
+
index = pc.Index(INDEX_NAME)
|
22 |
+
|
23 |
+
# تابع RAG
|
24 |
def rag_chatbot(message, history):
|
25 |
+
greetings = ["سلام", "سلام وقت بخیر", "درود", "خسته نباشید"]
|
26 |
if message.strip() in greetings:
|
27 |
return "سلام! چطور میتونم کمکتون کنم؟ 😊"
|
28 |
|
29 |
+
# مرحله ۱: بازیابی نزدیکترین اسناد
|
30 |
query_vector = embedder.encode(message).tolist()
|
31 |
result = index.query(vector=query_vector, top_k=3, include_metadata=True)
|
32 |
|
33 |
if not result['matches']:
|
34 |
return "متأسفم، اطلاعاتی در پایگاه داده پیدا نکردم."
|
35 |
|
36 |
+
context = "\n".join([match['metadata'].get("پاسخ", "") for match in result['matches'] if match['metadata'].get("پاسخ")])
|
37 |
|
38 |
if not context.strip():
|
39 |
return "متأسفم، اطلاعات کافی در این زمینه ندارم."
|
40 |
|
41 |
+
# مرحله ۲: استفاده از مدل پاسخگویی BERT
|
42 |
inputs = tokenizer(message, context, return_tensors="pt", truncation=True, max_length=512)
|
43 |
with torch.no_grad():
|
44 |
outputs = model(**inputs)
|
45 |
|
46 |
+
start_idx = torch.argmax(outputs.start_logits)
|
47 |
+
end_idx = torch.argmax(outputs.end_logits)
|
48 |
+
|
49 |
+
answer_tokens = inputs["input_ids"][0][start_idx:end_idx + 1]
|
50 |
+
answer = tokenizer.decode(answer_tokens, skip_special_tokens=True)
|
51 |
|
52 |
+
if not answer.strip() or answer.strip() in ["[CLS]", "[SEP]"]:
|
53 |
return "متأسفم، نتونستم پاسخ دقیقی پیدا کنم."
|
54 |
|
55 |
return answer.strip()
|
56 |
+
|
57 |
+
# رابط Gradio
|
58 |
+
chat_ui = gr.ChatInterface(
|
59 |
+
fn=rag_chatbot,
|
60 |
+
title="🤖 چتبات هوشمند تیام (نسخه RAG)",
|
61 |
+
description="پاسخگویی ترکیبی بر پایه دادههای واقعی شرکت + تولید پاسخ با مدل BERT فارسی",
|
62 |
+
theme="soft",
|
63 |
+
)
|
64 |
+
|
65 |
+
chat_ui.launch()
|