import gradio as gr from transformers import pipeline from gtts import gTTS import os from tempfile import gettempdir # 1. إعداد نموذج تلخيص النصوص باللغة الإنجليزية summarization_pipeline_en = pipeline("summarization", model="facebook/bart-large-cnn") # 2. إعداد نموذج الترجمة من الإنجليزية إلى العربية translation_pipeline = pipeline("translation_en_to_ar", model="Helsinki-NLP/opus-mt-en-ar") # 3. دالة تلخيص النصوص باللغة الإنجليزية def summarize_text_en(text): summary = summarization_pipeline_en( text, max_length=60, # Reduce the max length further min_length=25, # Reduce the min length length_penalty=2.0, num_beams=4, early_stopping=True )[0]["summary_text"] return summary # 4. تحويل النص إلى صوت باستخدام gTTS def text_to_speech(text, lang_code): tts = gTTS(text=text, lang=lang_code) temp_dir = gettempdir() audio_path = os.path.join(temp_dir, f"summary_audio_{lang_code}.mp3") tts.save(audio_path) return audio_path # 5. دالة معالجة النصوص بناءً على اللغة المختارة def process_text(text, language_option): if language_option == "English": # تلخيص النص باللغة الإنجليزية summary_en = summarize_text_en(text) # تحويل النص الملخص باللغة الإنجليزية إلى صوت audio_file_en = text_to_speech(summary_en, 'en') return summary_en, audio_file_en, None, None elif language_option == "English to Arabic": # تلخيص النص باللغة الإنجليزية summary_en = summarize_text_en(text) # ترجمة الملخص إلى العربية summary_ar = translation_pipeline(summary_en)[0]["translation_text"] # تحويل النص الملخص باللغة العربية إلى صوت audio_file_ar = text_to_speech(summary_ar, 'ar') return None, None, summary_ar, audio_file_ar # 6. دالة استخراج النص من الملف الصوتي def extract_text_from_audio(audio, audio_language): if audio_language == "Arabic": # استخدام نموذج wav2vec2 لاستخراج النصوص العربية asr_pipeline = pipeline("automatic-speech-recognition", model="jonatasgrosman/wav2vec2-large-xlsr-53-arabic") text = asr_pipeline(audio)["text"] else: # Assume English by default asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-base") text = asr_pipeline(audio)["text"] return text # 7. دالة تحديث المخرجات بناءً على اللغة المختارة def update_outputs(language_option): if language_option == "English": return [gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)] elif language_option == "English to Arabic": return [gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)] with gr.Blocks() as iface: # إضافة عنوان gr.Markdown("