Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,61 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
global context
|
| 10 |
-
if message.lower().startswith("context:") or message.startswith("متن:"):
|
| 11 |
-
context = message.split(":", 1)[1].strip()
|
| 12 |
-
return "متن جدید ثبت شد. حالا سوالتو بپرس."
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
if __name__ == "__main__":
|
| 31 |
-
|
|
|
|
| 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()
|