diginoron commited on
Commit
e42cbcd
·
verified ·
1 Parent(s): f2be9e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -11
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  from sentence_transformers import SentenceTransformer
3
- from transformers import GPT2LMHeadModel, GPT2Tokenizer
4
  from pinecone import Pinecone
5
 
6
  # ===============================
@@ -20,8 +20,8 @@ embedding_model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
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
@@ -38,6 +38,24 @@ def retrieve_answer(query, threshold=0.65, top_k=1):
38
  # ===============================
39
  # ✨ تولید پاسخ با GPT-2
40
  # ===============================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  def final_answer(user_question):
42
  # ابتدا پاسخ را از Pinecone جستجو می‌کنیم
43
  answer = retrieve_answer(user_question)
@@ -60,13 +78,6 @@ def final_answer(user_question):
60
  # اگر سوال عمومی نبود، از GPT2 برای تولید پاسخ طبیعی استفاده می‌کنیم
61
  return generate_response_with_gpt2("", user_question)
62
 
63
- def generate_response_with_gpt2(answer, user_question):
64
- # اگر پاسخ خاصی از Pinecone نیست، فقط به مدل GPT2 درخواست می‌دهیم تا جواب بدهد
65
- prompt = f"پاسخ به سوال: {user_question} بر اساس اطلاعات: {answer}"
66
- input_ids = gpt2_tokenizer.encode(prompt, return_tensors="pt", truncation=True)
67
- 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)
68
- generated_text = gpt2_tokenizer.decode(output_ids[0], skip_special_tokens=True)
69
- return generated_text
70
  # ===============================
71
  # 🎛️ رابط Gradio
72
  # ===============================
@@ -78,4 +89,4 @@ demo = gr.Interface(
78
  description="سؤالات خود را از آژانس دیجیتال مارکتینگ تیام بپرسید. سیستم از ترکیب جستجوی دقیق و تولید پاسخ طبیعی استفاده می‌کند."
79
  )
80
 
81
- demo.launch()
 
1
  import gradio as gr
2
  from sentence_transformers import SentenceTransformer
3
+ from transformers import GPT2Tokenizer, GPT2LMHeadModel
4
  from pinecone import Pinecone
5
 
6
  # ===============================
 
20
  # ===============================
21
  # 🧠 بارگذاری مدل GPT-2 برای تولید پاسخ
22
  # ===============================
23
+ gpt2_tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
24
+ gpt2_model = GPT2LMHeadModel.from_pretrained("gpt2")
25
 
26
  # ===============================
27
  # 🔍 بازیابی پاسخ از Pinecone
 
38
  # ===============================
39
  # ✨ تولید پاسخ با GPT-2
40
  # ===============================
41
+ def generate_response_with_gpt2(answer, user_question):
42
+ # ساخت پرامپت
43
+ prompt = f"پاسخ به سوال: {user_question} بر اساس اطلاعات: {answer}"
44
+
45
+ # تبدیل پرامپت به توکن‌ها
46
+ input_ids = gpt2_tokenizer.encode(prompt, return_tensors="pt", truncation=True)
47
+
48
+ # تولید خروجی از مدل GPT2
49
+ 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)
50
+
51
+ # تبدیل توکن‌ها به متن
52
+ generated_text = gpt2_tokenizer.decode(output_ids[0], skip_special_tokens=True)
53
+
54
+ return generated_text
55
+
56
+ # ===============================
57
+ # ✨ تولید پاسخ نهایی
58
+ # ===============================
59
  def final_answer(user_question):
60
  # ابتدا پاسخ را از Pinecone جستجو می‌کنیم
61
  answer = retrieve_answer(user_question)
 
78
  # اگر سوال عمومی نبود، از GPT2 برای تولید پاسخ طبیعی استفاده می‌کنیم
79
  return generate_response_with_gpt2("", user_question)
80
 
 
 
 
 
 
 
 
81
  # ===============================
82
  # 🎛️ رابط Gradio
83
  # ===============================
 
89
  description="سؤالات خود را از آژانس دیجیتال مارکتینگ تیام بپرسید. سیستم از ترکیب جستجوی دقیق و تولید پاسخ طبیعی استفاده می‌کند."
90
  )
91
 
92
+ demo.launch()