|
import gradio as gr |
|
from transformers import pipeline, set_seed |
|
|
|
set_seed(42) |
|
|
|
|
|
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, "" |
|
|
|
|
|
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'] |
|
|
|
|
|
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'] |
|
|
|
|
|
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"] |
|
|
|
|
|
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() |