Spaces:
Runtime error
Runtime error
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr # استدعاء المكتبات المطلوبه
|
2 |
+
from transformers import pipeline
|
3 |
+
from gtts import gTTS
|
4 |
+
|
5 |
+
# 1. إعداد نموذج تلخيص النصوص باللغة الإنجليزية
|
6 |
+
summarization_pipeline_en = pipeline("summarization", model="facebook/bart-large-cnn")
|
7 |
+
|
8 |
+
# 2. إعداد نموذج الترجمة من الإنجليزية إلى العربية
|
9 |
+
translation_pipeline = pipeline("translation_en_to_ar", model="Helsinki-NLP/opus-mt-en-ar")
|
10 |
+
|
11 |
+
# 3. دالة تلخيص النصوص باللغة الإنجليزية
|
12 |
+
def summarize_text_en(text):
|
13 |
+
summary = summarization_pipeline_en(text, max_length=150, min_length=40, length_penalty=2.0, num_beams=4, early_stopping=True)[0]["summary_text"]
|
14 |
+
return summary
|
15 |
+
|
16 |
+
# 4. تحويل النص إلى صوت باستخدام gTTS
|
17 |
+
def text_to_speech(text, lang_code):
|
18 |
+
tts = gTTS(text=text, lang=lang_code)
|
19 |
+
audio_path = f"summary_audio_{lang_code}.mp3"
|
20 |
+
tts.save(audio_path)
|
21 |
+
return audio_path
|
22 |
+
|
23 |
+
# 5. دالة معالجة النصوص بناءً على اللغة المختارة
|
24 |
+
def process_text(text, language_option):
|
25 |
+
if language_option == "English":
|
26 |
+
# تلخيص النص باللغة الإنجليزية
|
27 |
+
summary_en = summarize_text_en(text)
|
28 |
+
# تحويل النص الملخص باللغة الإنجليزية إلى صوت
|
29 |
+
audio_file_en = text_to_speech(summary_en, 'en')
|
30 |
+
return summary_en, audio_file_en, None, None
|
31 |
+
|
32 |
+
elif language_option == "English to Arabic":
|
33 |
+
# تلخيص النص باللغة الإنجليزية
|
34 |
+
summary_en = summarize_text_en(text)
|
35 |
+
# ترجمة الملخص إلى العربية
|
36 |
+
summary_ar = translation_pipeline(summary_en)[0]["translation_text"]
|
37 |
+
# تحويل النص الملخص باللغة العربية إلى صوت
|
38 |
+
audio_file_ar = text_to_speech(summary_ar, 'ar')
|
39 |
+
return None, None, summary_ar, audio_file_ar
|
40 |
+
|
41 |
+
# 6. دالة استخراج النص من الملف الصوتي
|
42 |
+
def extract_text_from_audio(audio, audio_language):
|
43 |
+
if audio_language == "Arabic":
|
44 |
+
# استخدام نموذج wav2vec2 لاستخراج النصوص العربية
|
45 |
+
asr_pipeline = pipeline("automatic-speech-recognition", model="jonatasgrosman/wav2vec2-large-xlsr-53-arabic")
|
46 |
+
text = asr_pipeline(audio)["text"]
|
47 |
+
else: # Assume English by default
|
48 |
+
asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-base")
|
49 |
+
text = asr_pipeline(audio)["text"]
|
50 |
+
return text
|
51 |
+
|
52 |
+
# 7. دالة تحديث المخرجات بناءً على اللغة المختارة
|
53 |
+
def update_outputs(language_option):
|
54 |
+
if language_option == "English":
|
55 |
+
return [gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)]
|
56 |
+
elif language_option == "English to Arabic":
|
57 |
+
return [gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)]
|
58 |
+
|
59 |
+
with gr.Blocks() as iface:
|
60 |
+
# إضافة عنوان
|
61 |
+
gr.Markdown("<h1 style='text-align:center;'>A Tool for Text Extraction and Reading</h1>")
|
62 |
+
|
63 |
+
with gr.Tab("Text Summarization & Speech"):
|
64 |
+
with gr.Row():
|
65 |
+
with gr.Column(scale=1): # الجانب الأيسر (المدخلات)
|
66 |
+
text_input = gr.Textbox(label="Enter Text")
|
67 |
+
language_option = gr.Radio(["English", "English to Arabic"], label="Choose Summary Language")
|
68 |
+
summarize_btn = gr.Button("Summarize")
|
69 |
+
with gr.Column(scale=1): # الجانب الأيمن (المخرجات)
|
70 |
+
english_summary = gr.Textbox(label="English Summary", visible=False)
|
71 |
+
english_audio = gr.Audio(label="English Summary Audio", type="filepath", visible=False)
|
72 |
+
arabic_summary = gr.Textbox(label="Translated Arabic Summary", visible=False)
|
73 |
+
arabic_audio = gr.Audio(label="Arabic Summary Audio", type="filepath", visible=False)
|
74 |
+
|
75 |
+
# إظهار المخرجات المناسبة بناءً على اللغة المختارة
|
76 |
+
language_option.change(
|
77 |
+
update_outputs,
|
78 |
+
inputs=language_option,
|
79 |
+
outputs=[english_summary, english_audio, arabic_summary, arabic_audio]
|
80 |
+
)
|
81 |
+
|
82 |
+
# تشغيل عملية التلخيص والنطق
|
83 |
+
summarize_btn.click(
|
84 |
+
process_text,
|
85 |
+
inputs=[text_input, language_option],
|
86 |
+
outputs=[english_summary, english_audio, arabic_summary, arabic_audio]
|
87 |
+
)
|
88 |
+
|
89 |
+
with gr.Tab("Audio Transcription"):
|
90 |
+
with gr.Row():
|
91 |
+
with gr.Column(scale=1): # الجانب الأيسر (المدخلات)
|
92 |
+
audio_input = gr.Audio(label="Upload Audio", type="filepath")
|
93 |
+
audio_language = gr.Radio(["Arabic", "English"], label="Audio Language")
|
94 |
+
transcribe_btn = gr.Button("Transcribe Audio")
|
95 |
+
with gr.Column(scale=1): # الجانب الأيمن (المخرجات)
|
96 |
+
transcribed_text = gr.Textbox(label="Transcribed Text")
|
97 |
+
|
98 |
+
# تشغيل عملية استخراج النص من الصوت
|
99 |
+
transcribe_btn.click(
|
100 |
+
extract_text_from_audio,
|
101 |
+
inputs=[audio_input, audio_language],
|
102 |
+
outputs=[transcribed_text]
|
103 |
+
)
|
104 |
+
|
105 |
+
# 9. تشغيل الواجهة
|
106 |
+
iface.launch()
|