Faisalaldwaish1 commited on
Commit
2f012ac
1 Parent(s): b2c528d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -0
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # 8. إنشاء واجهة Gradio مع تبويبين
60
+ with gr.Blocks() as iface:
61
+ # إضافة عنوان في الوسط
62
+ gr.Markdown("### A Tool for Text Extraction and Reading")
63
+
64
+ with gr.Tab("Text Summarization & Speech"):
65
+ with gr.Row():
66
+ with gr.Column(scale=1): # الجانب الأيسر (المدخلات)
67
+ text_input = gr.Textbox(label="Enter Text")
68
+ language_option = gr.Radio(["English", "English to Arabic"], label="Choose Summary Language")
69
+ summarize_btn = gr.Button("Summarize")
70
+ with gr.Column(scale=1): # الجانب الأيمن (المخرجات)
71
+ english_summary = gr.Textbox(label="English Summary", visible=False)
72
+ english_audio = gr.Audio(label="English Summary Audio", type="filepath", visible=False)
73
+ arabic_summary = gr.Textbox(label="Translated Arabic Summary", visible=False)
74
+ arabic_audio = gr.Audio(label="Arabic Summary Audio", type="filepath", visible=False)
75
+
76
+ # إظهار المخرجات المناسبة بناءً على اللغة المختارة
77
+ language_option.change(
78
+ update_outputs,
79
+ inputs=language_option,
80
+ outputs=[english_summary, english_audio, arabic_summary, arabic_audio]
81
+ )
82
+
83
+ # تشغيل عملية التلخيص والنطق
84
+ summarize_btn.click(
85
+ process_text,
86
+ inputs=[text_input, language_option],
87
+ outputs=[english_summary, english_audio, arabic_summary, arabic_audio]
88
+ )
89
+
90
+ with gr.Tab("Audio Transcription"):
91
+ with gr.Row():
92
+ with gr.Column(scale=1): # الجانب الأيسر (المدخلات)
93
+ audio_input = gr.Audio(label="Upload Audio", type="filepath")
94
+ audio_language = gr.Radio(["Arabic", "English"], label="Audio Language")
95
+ transcribe_btn = gr.Button("Transcribe Audio")
96
+ with gr.Column(scale=1): # الجانب الأيمن (المخرجات)
97
+ transcribed_text = gr.Textbox(label="Transcribed Text")
98
+
99
+ # تشغيل عملية استخراج النص من الصوت
100
+ transcribe_btn.click(
101
+ extract_text_from_audio,
102
+ inputs=[audio_input, audio_language],
103
+ outputs=[transcribed_text]
104
+ )
105
+
106
+ # 9. تشغيل الواجهة
107
+ iface.launch()