hosseinfarahi commited on
Commit
083e5f6
·
verified ·
1 Parent(s): 4bc2060

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -20
app.py CHANGED
@@ -1,31 +1,61 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
- qa_pipeline = pipeline("question-answering", model="hosseinfarahi/squad-roberta-large")
 
 
5
 
6
- context = ""
 
 
 
 
 
 
 
7
 
8
- def chat_fn(message, history):
9
- global context
10
- if message.lower().startswith("context:") or message.startswith("متن:"):
11
- context = message.split(":", 1)[1].strip()
12
- return "متن جدید ثبت شد. حالا سوالتو بپرس."
13
 
14
- if not context:
15
- return "لطفاً ابتدا یک متن وارد کن. مثلا: context: فلان پاراگراف..."
 
 
16
 
17
- try:
18
- result = qa_pipeline(question=message, context=context)
19
- return result["answer"]
20
- except Exception as e:
21
- return f"خطا در پاسخ‌دهی: {str(e)}"
 
 
 
 
22
 
23
- chatbot = gr.ChatInterface(
24
- fn=chat_fn,
25
- title="🤖 چت‌بات هوشمند پرسش و پاسخ",
26
- description="اول یک متن بده (مثلاً: context: پاستا را ۱۰ دقیقه بجوشانید...)، بعد سوال بپرس.",
27
- examples=["context: Python is a programming language...", "What is Python?", "How long to cook pasta?"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  )
29
 
30
  if __name__ == "__main__":
31
- chatbot.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import requests
4
 
5
+ qa = pipeline("question-answering", model="hosseinfarahi/squad-roberta-large")
6
+ fa2en = pipeline("translation", model="Helsinki-NLP/opus-mt-fa-en")
7
+ en2fa = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fa")
8
 
9
+ CONTEXTS = {
10
+ "🍝 پاستا": "To cook pasta, boil water, add salt and oil. Cook dry pasta for 8 to 10 minutes, then drain and serve.",
11
+ "💰 بیت‌کوین": "Bitcoin is a decentralized digital currency launched in 2009 by a mysterious person or group called Satoshi Nakamoto.",
12
+ "🐍 پایتون": "Python is a versatile programming language often used for web development, data science, AI, and scripting.",
13
+ "🧠 پزشکی": "Medical advice should always be validated by a doctor. Common cold symptoms include sore throat, runny nose, and fever.",
14
+ "🥗 تغذیه": "A healthy diet includes vegetables, fruits, whole grains, and lean proteins. Drinking water is essential.",
15
+ "🇬🇧 زبان انگلیسی": "English grammar includes verb tenses, articles, and sentence structure. Practice improves fluency."
16
+ }
17
 
18
+ selected_topic = "🍝 پاستا"
 
 
 
 
19
 
20
+ def set_topic(choice):
21
+ global selected_topic
22
+ selected_topic = choice
23
+ return f"✅ موضوع انتخاب‌شده: {choice}"
24
 
25
+ def smart_chat(message, history):
26
+ context = CONTEXTS[selected_topic]
27
+ if "bitcoin" in selected_topic.lower():
28
+ try:
29
+ res = requests.get("https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd")
30
+ price = res.json()["bitcoin"]["usd"]
31
+ context += f"\nCurrent Bitcoin price is ${price}"
32
+ except:
33
+ context += "\n(خطا در دریافت قیمت لحظه‌ای بیت‌کوین)"
34
 
35
+ question_en = fa2en(message)[0]['translation_text']
36
+ result = qa(question=question_en, context=context)
37
+ answer_en = result["answer"]
38
+ answer_fa = en2fa(answer_en)[0]['translation_text']
39
+ return answer_fa
40
+
41
+ topic_selector = gr.Dropdown(
42
+ label="📚 انتخاب موضوع",
43
+ choices=list(CONTEXTS.keys()),
44
+ value="🍝 پاستا",
45
+ interactive=True,
46
+ info="یکی از موضوعات را انتخاب کن",
47
+ type="value"
48
+ )
49
+
50
+ chat = gr.ChatInterface(
51
+ fn=smart_chat,
52
+ chatbot=gr.Chatbot(show_label=False),
53
+ textbox=gr.Textbox(placeholder="سوالت رو بنویس و اینتر بزن...", container=False),
54
+ title="🤖 چت‌بات فارسی چندمنظوره",
55
+ description="✅ اول از لیست بالا موضوع انتخاب کن بعد شروع کن به سوال پرسیدن...",
56
+ additional_inputs=[topic_selector],
57
+ additional_inputs_placement="sidebar"
58
  )
59
 
60
  if __name__ == "__main__":
61
+ chat.launch()