File size: 3,363 Bytes
d4e8dab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import gradio as gr
from transformers import pipeline, set_seed

set_seed(42)

# 1️⃣ بوت محادثة بالعربية
chat_model = pipeline("text-generation", model="akhooli/gpt2-small-arabic")

def chat(history, user_message):
    conversation_text = ""
    for human, bot in history:
        conversation_text += f"👤: {human}\n🤖: {bot}\n"
    conversation_text += f"👤: {user_message}\n🤖:"
    response = chat_model(conversation_text, max_length=200, num_return_sequences=1, do_sample=True, top_p=0.9)
    bot_reply = response[0]["generated_text"].split("🤖:")[-1].strip()
    history.append((user_message, bot_reply))
    return history, ""

# 2️⃣ الترجمة (إنجليزي ↔ عربي)
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")

def translate_en_to_ar(text):
    return translator_en_ar(text)[0]['translation_text']

def translate_ar_to_en(text):
    return translator_ar_en(text)[0]['translation_text']

# 3️⃣ التلخيص
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")

def summarize_text(text):
    summary = summarizer(text, max_length=150, min_length=30, do_sample=False)
    return summary[0]['summary_text']

# 4️⃣ سؤال وجواب
qa = pipeline("question-answering", model="distilbert-base-multilingual-cased-distilled-squad")

def ask_question(context, question):
    answer = qa(question=question, context=context)
    return answer["answer"]

# 🌐 واجهة Gradio مع Tabs
with gr.Blocks() as demo:
    gr.Markdown("# 🤖 المساعد العربي الخارق 🔥")
    gr.Markdown("👨‍💻 المطوّر: **حسين محمد**")

    with gr.Tab("💬 شات بوت"):
        chatbot_ui = gr.Chatbot()
        msg = gr.Textbox(label="✍️ اكتب رسالتك")
        clear = gr.Button("🗑️ مسح المحادثة")
        state = gr.State([])
        msg.submit(chat, [state, msg], [chatbot_ui, msg])
        clear.click(lambda: ([], ""), None, [chatbot_ui, msg])

    with gr.Tab("🌍 ترجمة"):
        with gr.Row():
            input_en = gr.Textbox(label="اكتب نص بالإنجليزية")
            output_ar = gr.Textbox(label="ترجمة إلى العربية")
            btn1 = gr.Button("ترجم EN ➝ AR")
        btn1.click(translate_en_to_ar, input_en, output_ar)

        with gr.Row():
            input_ar = gr.Textbox(label="اكتب نص بالعربية")
            output_en = gr.Textbox(label="ترجمة إلى الإنجليزية")
            btn2 = gr.Button("ترجم AR ➝ EN")
        btn2.click(translate_ar_to_en, input_ar, output_en)

    with gr.Tab("📝 تلخيص"):
        text_input = gr.Textbox(label="الصق نص طويل هنا")
        text_output = gr.Textbox(label="الملخص")
        btn3 = gr.Button("لخّص النص")
        btn3.click(summarize_text, text_input, text_output)

    with gr.Tab("❓ سؤال وجواب"):
        context = gr.Textbox(label="النص أو المقال")
        question = gr.Textbox(label="اكتب سؤالك")
        answer = gr.Textbox(label="الإجابة")
        btn4 = gr.Button("أجب")
        btn4.click(ask_question, [context, question], answer)

if __name__ == "__main__":
    demo.launch()