Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline, set_seed
|
3 |
+
|
4 |
+
set_seed(42)
|
5 |
+
|
6 |
+
# 1️⃣ بوت محادثة بالعربية
|
7 |
+
chat_model = pipeline("text-generation", model="akhooli/gpt2-small-arabic")
|
8 |
+
|
9 |
+
def chat(history, user_message):
|
10 |
+
conversation_text = ""
|
11 |
+
for human, bot in history:
|
12 |
+
conversation_text += f"👤: {human}\n🤖: {bot}\n"
|
13 |
+
conversation_text += f"👤: {user_message}\n🤖:"
|
14 |
+
response = chat_model(conversation_text, max_length=200, num_return_sequences=1, do_sample=True, top_p=0.9)
|
15 |
+
bot_reply = response[0]["generated_text"].split("🤖:")[-1].strip()
|
16 |
+
history.append((user_message, bot_reply))
|
17 |
+
return history, ""
|
18 |
+
|
19 |
+
# 2️⃣ الترجمة (إنجليزي ↔ عربي)
|
20 |
+
translator_en_ar = pipeline("translation_en_to_ar", model="Helsinki-NLP/opus-mt-en-ar")
|
21 |
+
translator_ar_en = pipeline("translation_ar_to_en", model="Helsinki-NLP/opus-mt-ar-en")
|
22 |
+
|
23 |
+
def translate_en_to_ar(text):
|
24 |
+
return translator_en_ar(text)[0]['translation_text']
|
25 |
+
|
26 |
+
def translate_ar_to_en(text):
|
27 |
+
return translator_ar_en(text)[0]['translation_text']
|
28 |
+
|
29 |
+
# 3️⃣ التلخيص
|
30 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
31 |
+
|
32 |
+
def summarize_text(text):
|
33 |
+
summary = summarizer(text, max_length=150, min_length=30, do_sample=False)
|
34 |
+
return summary[0]['summary_text']
|
35 |
+
|
36 |
+
# 4️⃣ سؤال وجواب
|
37 |
+
qa = pipeline("question-answering", model="distilbert-base-multilingual-cased-distilled-squad")
|
38 |
+
|
39 |
+
def ask_question(context, question):
|
40 |
+
answer = qa(question=question, context=context)
|
41 |
+
return answer["answer"]
|
42 |
+
|
43 |
+
# 🌐 واجهة Gradio مع Tabs
|
44 |
+
with gr.Blocks() as demo:
|
45 |
+
gr.Markdown("# 🤖 المساعد العربي الخارق 🔥")
|
46 |
+
gr.Markdown("👨💻 المطوّر: **حسين محمد**")
|
47 |
+
|
48 |
+
with gr.Tab("💬 شات بوت"):
|
49 |
+
chatbot_ui = gr.Chatbot()
|
50 |
+
msg = gr.Textbox(label="✍️ اكتب رسالتك")
|
51 |
+
clear = gr.Button("🗑️ مسح المحادثة")
|
52 |
+
state = gr.State([])
|
53 |
+
msg.submit(chat, [state, msg], [chatbot_ui, msg])
|
54 |
+
clear.click(lambda: ([], ""), None, [chatbot_ui, msg])
|
55 |
+
|
56 |
+
with gr.Tab("🌍 ترجمة"):
|
57 |
+
with gr.Row():
|
58 |
+
input_en = gr.Textbox(label="اكتب نص بالإنجليزية")
|
59 |
+
output_ar = gr.Textbox(label="ترجمة إلى العربية")
|
60 |
+
btn1 = gr.Button("ترجم EN ➝ AR")
|
61 |
+
btn1.click(translate_en_to_ar, input_en, output_ar)
|
62 |
+
|
63 |
+
with gr.Row():
|
64 |
+
input_ar = gr.Textbox(label="اكتب نص بالعربية")
|
65 |
+
output_en = gr.Textbox(label="ترجمة إلى الإنجليزية")
|
66 |
+
btn2 = gr.Button("ترجم AR ➝ EN")
|
67 |
+
btn2.click(translate_ar_to_en, input_ar, output_en)
|
68 |
+
|
69 |
+
with gr.Tab("📝 تلخيص"):
|
70 |
+
text_input = gr.Textbox(label="الصق نص طويل هنا")
|
71 |
+
text_output = gr.Textbox(label="الملخص")
|
72 |
+
btn3 = gr.Button("لخّص النص")
|
73 |
+
btn3.click(summarize_text, text_input, text_output)
|
74 |
+
|
75 |
+
with gr.Tab("❓ سؤال وجواب"):
|
76 |
+
context = gr.Textbox(label="النص أو المقال")
|
77 |
+
question = gr.Textbox(label="اكتب سؤالك")
|
78 |
+
answer = gr.Textbox(label="الإجابة")
|
79 |
+
btn4 = gr.Button("أجب")
|
80 |
+
btn4.click(ask_question, [context, question], answer)
|
81 |
+
|
82 |
+
if __name__ == "__main__":
|
83 |
+
demo.launch()
|