|
|
import gradio as gr |
|
|
from transformers import pipeline, set_seed |
|
|
import torch |
|
|
|
|
|
set_seed(42) |
|
|
|
|
|
|
|
|
try: |
|
|
|
|
|
chat_model = pipeline("text-generation", |
|
|
model="akhooli/distilgpt2-arabic", |
|
|
device="cuda" if torch.cuda.is_available() else "cpu") |
|
|
|
|
|
|
|
|
translator_en_ar = pipeline("translation_en_to_ar", model="Helsinki-NLP/opus-mt-en-ar") |
|
|
translator_ar_en = pipeline("translation_ar_to_en", model="Helsinki-NLP/opus-mt-ar-en") |
|
|
|
|
|
|
|
|
summarizer = pipeline("summarization", model="facebook/bart-large-cnn") |
|
|
|
|
|
|
|
|
qa_model = pipeline("question-answering", |
|
|
model="deepset/arabert-base-cased-qa", |
|
|
device="cuda" if torch.cuda.is_available() else "cpu") |
|
|
|
|
|
except Exception as e: |
|
|
raise gr.Error(f"خطأ في تحميل النماذج: {e}") |
|
|
|
|
|
|
|
|
def chat(history, user_message): |
|
|
"""وظيفة الدردشة مع الذكاء الاصطناعي""" |
|
|
try: |
|
|
if not user_message.strip(): |
|
|
return history, "⚠️ الرجاء كتابة رسالة صحيحة!" |
|
|
|
|
|
|
|
|
prompt = "\n".join([f"👤: {user}\n🤖: {bot}" for user, bot in history]) |
|
|
prompt += f"\n👤: {user_message}\n🤖:" |
|
|
|
|
|
|
|
|
response = chat_model( |
|
|
prompt, |
|
|
max_length=150, |
|
|
num_return_sequences=1, |
|
|
do_sample=True, |
|
|
temperature=0.7, |
|
|
top_p=0.9 |
|
|
) |
|
|
|
|
|
bot_reply = response[0]["generated_text"].split("🤖:")[-1].strip() |
|
|
history.append((user_message, bot_reply)) |
|
|
return history, "" |
|
|
|
|
|
except Exception as e: |
|
|
error_msg = f"❌ حدث خطأ: {str(e)}" |
|
|
return history, error_msg |
|
|
|
|
|
def translate_en_to_ar(text): |
|
|
"""الترجمة: الإنجليزية → العربية""" |
|
|
try: |
|
|
return translator_en_ar(text)[0]['translation_text'] |
|
|
except: |
|
|
return "⚠️ فشل في الترجمة!" |
|
|
|
|
|
def translate_ar_to_en(text): |
|
|
"""الترجمة: العربية → الإنجليزية""" |
|
|
try: |
|
|
return translator_ar_en(text)[0]['translation_text'] |
|
|
except: |
|
|
return "⚠️ فشل في الترجمة!" |
|
|
|
|
|
def summarize_text(text): |
|
|
"""تلخيص النصوص الطويلة""" |
|
|
try: |
|
|
summary = summarizer(text, max_length=150, min_length=30) |
|
|
return summary[0]['summary_text'] |
|
|
except: |
|
|
return "⚠️ لا يمكن تلخيص هذا النص!" |
|
|
|
|
|
def ask_question(context, question): |
|
|
"""الإجابة على الأسئلة من نص معين""" |
|
|
try: |
|
|
if not context.strip() or not question.strip(): |
|
|
return "⚠️ الرجاء إدخال النص والسؤال!" |
|
|
|
|
|
answer = qa_model(question=question, context=context) |
|
|
return answer["answer"] |
|
|
except Exception as e: |
|
|
return f"⚠️ خطأ: {str(e)}" |
|
|
|
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Soft(), title="المساعد العربي") as app: |
|
|
gr.Markdown("<h1 style='text-align: center;'>🤖 المساعد الذكي متعدد المهام</h1>") |
|
|
|
|
|
with gr.Tab("💬 دردشة عربية"): |
|
|
chatbot = gr.Chatbot(height=350) |
|
|
user_input = gr.Textbox(placeholder="اكتب رسالتك هنا...", label="المستخدم") |
|
|
send_btn = gr.Button("إرسال") |
|
|
clear_btn = gr.Button("مسح التاريخ") |
|
|
|
|
|
send_btn.click(chat, [chatbot, user_input], [chatbot, user_input]) |
|
|
user_input.submit(chat, [chatbot, user_input], [chatbot, user_input]) |
|
|
clear_btn.click(lambda: ([], ""), outputs=[chatbot, user_input]) |
|
|
|
|
|
with gr.Tab("🌍 مترجم"): |
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
en_input = gr.Textbox(label="النص الإنجليزي") |
|
|
en_to_ar_btn = gr.Button("English → العربية") |
|
|
with gr.Column(): |
|
|
ar_output = gr.Textbox(label="الترجمة") |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
ar_input = gr.Textbox(label="النص العربي") |
|
|
ar_to_en_btn = gr.Button("العربية → English") |
|
|
with gr.Column(): |
|
|
en_output = gr.Textbox(label="الترجمة") |
|
|
|
|
|
en_to_ar_btn.click(translate_en_to_ar, en_input, ar_output) |
|
|
ar_to_en_btn.click(translate_ar_to_en, ar_input, en_output) |
|
|
|
|
|
with gr.Tab("📝 ملخص النصوص"): |
|
|
text_input = gr.Textbox(label="النص الأصلي", lines=5) |
|
|
summarize_btn = gr.Button("توليد الملخص") |
|
|
summary_output = gr.Textbox(label="الملخص", lines=3) |
|
|
summarize_btn.click(summarize_text, text_input, summary_output) |
|
|
|
|
|
with gr.Tab("❓ سؤال وجواب"): |
|
|
context_box = gr.Textbox(label="النص المرجعي", lines=4) |
|
|
question_box = gr.Textbox(label="السؤال") |
|
|
answer_btn = gr.Button("البحث عن الإجابة") |
|
|
answer_box = gr.Textbox(label="الإجابة", interactive=False) |
|
|
answer_btn.click(ask_question, [context_box, question_box], answer_box) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
app.launch(debug=True, share=False) |