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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -12
app.py CHANGED
@@ -38,24 +38,35 @@ def retrieve_answer(query, threshold=0.65, top_k=1):
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_new_tokens=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
- # ===============================
48
- # 💬 منطق نهایی پاسخ‌دهی
49
- # ===============================
50
  def final_answer(user_question):
 
51
  answer = retrieve_answer(user_question)
52
 
53
  if answer:
54
- # فقط پاسخ را ارسال کنید، نه سوال
55
- return answer
56
  else:
57
- return "پاسخ دقیقی برای این سوال نداریم."
 
 
 
 
 
 
 
 
 
 
 
 
58
 
 
 
 
 
 
 
 
59
  # ===============================
60
  # 🎛️ رابط Gradio
61
  # ===============================
 
38
  # ===============================
39
  # ✨ تولید پاسخ با GPT-2
40
  # ===============================
 
 
 
 
 
 
 
 
 
41
  def final_answer(user_question):
42
+ # ابتدا پاسخ را از Pinecone جستجو می‌کنیم
43
  answer = retrieve_answer(user_question)
44
 
45
  if answer:
46
+ # اگر پاسخی از Pinecone پیدا شد، آن را به GPT2 بدهیم برای تولید پاسخ طبیعی
47
+ return generate_response_with_gpt2(answer, user_question)
48
  else:
49
+ # اگر پاسخی از Pinecone یافت نشد، برای سوالات عمومی از GPT2 استفاده می‌کنیم
50
+ general_responses = {
51
+ "سلام": "سلام! خوش آمدید 😊",
52
+ "خوبی؟": "مرسی! حال شما چطور است؟",
53
+ "مرسی": "خواهش می‌کنم، در خدمت شما هستیم."
54
+ }
55
+
56
+ for key, value in general_responses.items():
57
+ if key in user_question:
58
+ return value
59
+
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
  # ===============================