Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline, set_seed
|
| 3 |
+
import torch # لتحسين الأداء على GPU إذا متوفر
|
| 4 |
+
|
| 5 |
+
set_seed(42)
|
| 6 |
+
|
| 7 |
+
# 1️⃣ تهيئة النماذج مع معالجة الأخطاء
|
| 8 |
+
try:
|
| 9 |
+
# نموذج الدردشة العربي (أخف وأسرع)
|
| 10 |
+
chat_model = pipeline("text-generation",
|
| 11 |
+
model="akhooli/distilgpt2-arabic",
|
| 12 |
+
device="cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
+
|
| 14 |
+
# 2️⃣ نماذج الترجمة
|
| 15 |
+
translator_en_ar = pipeline("translation_en_to_ar", model="Helsinki-NLP/opus-mt-en-ar")
|
| 16 |
+
translator_ar_en = pipeline("translation_ar_to_en", model="Helsinki-NLP/opus-mt-ar-en")
|
| 17 |
+
|
| 18 |
+
# 3️⃣ نموذج التلخيص
|
| 19 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 20 |
+
|
| 21 |
+
# 4️⃣ نموذج سؤال وجواب (QA) العربي
|
| 22 |
+
qa_model = pipeline("question-answering",
|
| 23 |
+
model="deepset/arabert-base-cased-qa",
|
| 24 |
+
device="cuda" if torch.cuda.is_available() else "cpu")
|
| 25 |
+
|
| 26 |
+
except Exception as e:
|
| 27 |
+
raise gr.Error(f"خطأ في تحميل النماذج: {e}")
|
| 28 |
+
|
| 29 |
+
# ========== الدوال الأساسية ========== #
|
| 30 |
+
def chat(history, user_message):
|
| 31 |
+
"""وظيفة الدردشة مع الذكاء الاصطناعي"""
|
| 32 |
+
try:
|
| 33 |
+
if not user_message.strip():
|
| 34 |
+
return history, "⚠️ الرجاء كتابة رسالة صحيحة!"
|
| 35 |
+
|
| 36 |
+
# بناء تاريخ المحادثة
|
| 37 |
+
prompt = "\n".join([f"👤: {user}\n🤖: {bot}" for user, bot in history])
|
| 38 |
+
prompt += f"\n👤: {user_message}\n🤖:"
|
| 39 |
+
|
| 40 |
+
# توليد الرد
|
| 41 |
+
response = chat_model(
|
| 42 |
+
prompt,
|
| 43 |
+
max_length=150,
|
| 44 |
+
num_return_sequences=1,
|
| 45 |
+
do_sample=True,
|
| 46 |
+
temperature=0.7,
|
| 47 |
+
top_p=0.9
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
bot_reply = response[0]["generated_text"].split("🤖:")[-1].strip()
|
| 51 |
+
history.append((user_message, bot_reply))
|
| 52 |
+
return history, ""
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
error_msg = f"❌ حدث خطأ: {str(e)}"
|
| 56 |
+
return history, error_msg
|
| 57 |
+
|
| 58 |
+
def translate_en_to_ar(text):
|
| 59 |
+
"""الترجمة: الإنجليزية → العربية"""
|
| 60 |
+
try:
|
| 61 |
+
return translator_en_ar(text)[0]['translation_text']
|
| 62 |
+
except:
|
| 63 |
+
return "⚠️ فشل في الترجمة!"
|
| 64 |
+
|
| 65 |
+
def translate_ar_to_en(text):
|
| 66 |
+
"""الترجمة: العربية → الإنجليزية"""
|
| 67 |
+
try:
|
| 68 |
+
return translator_ar_en(text)[0]['translation_text']
|
| 69 |
+
except:
|
| 70 |
+
return "⚠️ فشل في الترجمة!"
|
| 71 |
+
|
| 72 |
+
def summarize_text(text):
|
| 73 |
+
"""تلخيص النصوص الطويلة"""
|
| 74 |
+
try:
|
| 75 |
+
summary = summarizer(text, max_length=150, min_length=30)
|
| 76 |
+
return summary[0]['summary_text']
|
| 77 |
+
except:
|
| 78 |
+
return "⚠️ لا يمكن تلخيص هذا النص!"
|
| 79 |
+
|
| 80 |
+
def ask_question(context, question):
|
| 81 |
+
"""الإجابة على الأسئلة من نص معين"""
|
| 82 |
+
try:
|
| 83 |
+
if not context.strip() or not question.strip():
|
| 84 |
+
return "⚠️ الرجاء إدخال النص والسؤال!"
|
| 85 |
+
|
| 86 |
+
answer = qa_model(question=question, context=context)
|
| 87 |
+
return answer["answer"]
|
| 88 |
+
except Exception as e:
|
| 89 |
+
return f"⚠️ خطأ: {str(e)}"
|
| 90 |
+
|
| 91 |
+
# ========== واجهة Gradio ========== #
|
| 92 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="المساعد العربي") as app:
|
| 93 |
+
gr.Markdown("<h1 style='text-align: center;'>🤖 المساعد الذكي متعدد المهام</h1>")
|
| 94 |
+
|
| 95 |
+
with gr.Tab("💬 دردشة عربية"):
|
| 96 |
+
chatbot = gr.Chatbot(height=350)
|
| 97 |
+
user_input = gr.Textbox(placeholder="اكتب رسالتك هنا...", label="المستخدم")
|
| 98 |
+
send_btn = gr.Button("إرسال")
|
| 99 |
+
clear_btn = gr.Button("مسح التاريخ")
|
| 100 |
+
|
| 101 |
+
send_btn.click(chat, [chatbot, user_input], [chatbot, user_input])
|
| 102 |
+
user_input.submit(chat, [chatbot, user_input], [chatbot, user_input])
|
| 103 |
+
clear_btn.click(lambda: ([], ""), outputs=[chatbot, user_input])
|
| 104 |
+
|
| 105 |
+
with gr.Tab("🌍 مترجم"):
|
| 106 |
+
with gr.Row():
|
| 107 |
+
with gr.Column():
|
| 108 |
+
en_input = gr.Textbox(label="النص الإنجليزي")
|
| 109 |
+
en_to_ar_btn = gr.Button("English → العربية")
|
| 110 |
+
with gr.Column():
|
| 111 |
+
ar_output = gr.Textbox(label="الترجمة")
|
| 112 |
+
|
| 113 |
+
with gr.Row():
|
| 114 |
+
with gr.Column():
|
| 115 |
+
ar_input = gr.Textbox(label="النص العربي")
|
| 116 |
+
ar_to_en_btn = gr.Button("العربية → English")
|
| 117 |
+
with gr.Column():
|
| 118 |
+
en_output = gr.Textbox(label="الترجمة")
|
| 119 |
+
|
| 120 |
+
en_to_ar_btn.click(translate_en_to_ar, en_input, ar_output)
|
| 121 |
+
ar_to_en_btn.click(translate_ar_to_en, ar_input, en_output)
|
| 122 |
+
|
| 123 |
+
with gr.Tab("📝 ملخص النصوص"):
|
| 124 |
+
text_input = gr.Textbox(label="النص الأصلي", lines=5)
|
| 125 |
+
summarize_btn = gr.Button("توليد الملخص")
|
| 126 |
+
summary_output = gr.Textbox(label="الملخص", lines=3)
|
| 127 |
+
summarize_btn.click(summarize_text, text_input, summary_output)
|
| 128 |
+
|
| 129 |
+
with gr.Tab("❓ سؤال وجواب"):
|
| 130 |
+
context_box = gr.Textbox(label="النص المرجعي", lines=4)
|
| 131 |
+
question_box = gr.Textbox(label="السؤال")
|
| 132 |
+
answer_btn = gr.Button("البحث عن الإجابة")
|
| 133 |
+
answer_box = gr.Textbox(label="الإجابة", interactive=False)
|
| 134 |
+
answer_btn.click(ask_question, [context_box, question_box], answer_box)
|
| 135 |
+
|
| 136 |
+
# التشغيل
|
| 137 |
+
if __name__ == "__main__":
|
| 138 |
+
app.launch(debug=True, share=False)
|