Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,7 @@
|
|
1 |
-
# app.py
|
2 |
-
|
3 |
-
import json
|
4 |
import gradio as gr
|
5 |
from sentence_transformers import SentenceTransformer
|
6 |
-
from transformers import
|
7 |
from pinecone import Pinecone
|
8 |
-
import os
|
9 |
|
10 |
# ===============================
|
11 |
# 🌐 اتصال به Pinecone
|
@@ -22,18 +18,10 @@ index = pc.Index(PINECONE_INDEX_NAME)
|
|
22 |
embedding_model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
|
23 |
|
24 |
# ===============================
|
25 |
-
# 🧠 بارگذاری مدل
|
26 |
# ===============================
|
27 |
-
|
28 |
-
|
29 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
30 |
-
"google/mt5-small",
|
31 |
-
use_auth_token=hf_token
|
32 |
-
)
|
33 |
-
mt5_model = AutoModelForSeq2SeqLM.from_pretrained(
|
34 |
-
"google/mt5-small",
|
35 |
-
use_auth_token=hf_token
|
36 |
-
)
|
37 |
|
38 |
# ===============================
|
39 |
# 🔍 بازیابی پاسخ از Pinecone
|
@@ -48,12 +36,12 @@ def retrieve_answer(query, threshold=0.65, top_k=1):
|
|
48 |
return None
|
49 |
|
50 |
# ===============================
|
51 |
-
# ✨
|
52 |
# ===============================
|
53 |
-
def
|
54 |
prompt = f"پاسخ به سوال: {question} بر اساس اطلاعات: {answer}"
|
55 |
-
input_ids = tokenizer.encode(prompt, return_tensors="pt", truncation=True)
|
56 |
-
output_ids =
|
57 |
return tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
58 |
|
59 |
# ===============================
|
@@ -63,7 +51,7 @@ def final_answer(user_question):
|
|
63 |
answer = retrieve_answer(user_question)
|
64 |
|
65 |
if answer:
|
66 |
-
return
|
67 |
else:
|
68 |
# پاسخ عمومی برای سوالات چتی یا ناموجود
|
69 |
general_prompts = {
|
@@ -85,7 +73,7 @@ demo = gr.Interface(
|
|
85 |
inputs=gr.Textbox(lines=2, label="سؤال شما"),
|
86 |
outputs=gr.Textbox(label="پاسخ تیام"),
|
87 |
title="💬 چتبات هوشمند تیام",
|
88 |
-
description="سؤالات خود را از آژانس دیجیتال مارکتینگ تیام بپرسید. سیستم از ترکیب جستجوی دقیق و
|
89 |
)
|
90 |
|
91 |
demo.launch()
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from sentence_transformers import SentenceTransformer
|
3 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
4 |
from pinecone import Pinecone
|
|
|
5 |
|
6 |
# ===============================
|
7 |
# 🌐 اتصال به Pinecone
|
|
|
18 |
embedding_model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
|
19 |
|
20 |
# ===============================
|
21 |
+
# 🧠 بارگذاری مدل GPT-2 برای تولید پاسخ
|
22 |
# ===============================
|
23 |
+
tokenizer = GPT2Tokenizer.from_pretrained("openai-community/gpt2")
|
24 |
+
gpt2_model = GPT2LMHeadModel.from_pretrained("openai-community/gpt2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
# ===============================
|
27 |
# 🔍 بازیابی پاسخ از Pinecone
|
|
|
36 |
return None
|
37 |
|
38 |
# ===============================
|
39 |
+
# ✨ تولید پاسخ با GPT-2
|
40 |
# ===============================
|
41 |
+
def generate_response_with_gpt2(answer, question):
|
42 |
prompt = f"پاسخ به سوال: {question} بر اساس اطلاعات: {answer}"
|
43 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt", truncation=True, max_length=512)
|
44 |
+
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)
|
45 |
return tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
46 |
|
47 |
# ===============================
|
|
|
51 |
answer = retrieve_answer(user_question)
|
52 |
|
53 |
if answer:
|
54 |
+
return generate_response_with_gpt2(answer, user_question)
|
55 |
else:
|
56 |
# پاسخ عمومی برای سوالات چتی یا ناموجود
|
57 |
general_prompts = {
|
|
|
73 |
inputs=gr.Textbox(lines=2, label="سؤال شما"),
|
74 |
outputs=gr.Textbox(label="پاسخ تیام"),
|
75 |
title="💬 چتبات هوشمند تیام",
|
76 |
+
description="سؤالات خود را از آژانس دیجیتال مارکتینگ تیام بپرسید. سیستم از ترکیب جستجوی دقیق و تولید پاسخ طبیعی استفاده میکند."
|
77 |
)
|
78 |
|
79 |
demo.launch()
|