|
import gradio as gr |
|
from transformers import pipeline |
|
from gtts import gTTS |
|
import os |
|
from tempfile import gettempdir |
|
|
|
|
|
summarization_pipeline_en = pipeline("summarization", model="facebook/bart-large-cnn") |
|
|
|
|
|
translation_pipeline = pipeline("translation_en_to_ar", model="Helsinki-NLP/opus-mt-en-ar") |
|
|
|
|
|
def summarize_text_en(text): |
|
summary = summarization_pipeline_en( |
|
text, |
|
max_length=60, |
|
min_length=25, |
|
length_penalty=2.0, |
|
num_beams=4, |
|
early_stopping=True |
|
)[0]["summary_text"] |
|
return summary |
|
|
|
|
|
|
|
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 |
|
|
|
|
|
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 |
|
|
|
|
|
def extract_text_from_audio(audio, audio_language): |
|
if audio_language == "Arabic": |
|
|
|
asr_pipeline = pipeline("automatic-speech-recognition", model="jonatasgrosman/wav2vec2-large-xlsr-53-arabic") |
|
text = asr_pipeline(audio)["text"] |
|
else: |
|
asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-base") |
|
text = asr_pipeline(audio)["text"] |
|
return text |
|
|
|
|
|
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("<h1 style='text-align:center;'>A Tool for Text Extraction and Reading</h1>") |
|
|
|
with gr.Tab("Text Summarization & Speech"): |
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
text_input = gr.Textbox(label="Enter Text") |
|
language_option = gr.Radio(["English", "English to Arabic"], label="Choose Summary Language") |
|
summarize_btn = gr.Button("Summarize") |
|
with gr.Column(scale=1): |
|
english_summary = gr.Textbox(label="English Summary", visible=False) |
|
english_audio = gr.Audio(label="English Summary Audio", type="filepath", visible=False) |
|
arabic_summary = gr.Textbox(label="Translated Arabic Summary", visible=False) |
|
arabic_audio = gr.Audio(label="Arabic Summary Audio", type="filepath", visible=False) |
|
|
|
|
|
language_option.change( |
|
update_outputs, |
|
inputs=language_option, |
|
outputs=[english_summary, english_audio, arabic_summary, arabic_audio] |
|
) |
|
|
|
|
|
summarize_btn.click( |
|
process_text, |
|
inputs=[text_input, language_option], |
|
outputs=[english_summary, english_audio, arabic_summary, arabic_audio] |
|
) |
|
|
|
with gr.Tab("Audio Transcription"): |
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
audio_input = gr.Audio(label="Upload Audio", type="filepath") |
|
audio_language = gr.Radio(["Arabic", "English"], label="Audio Language") |
|
transcribe_btn = gr.Button("Transcribe Audio") |
|
with gr.Column(scale=1): |
|
transcribed_text = gr.Textbox(label="Transcribed Text") |
|
|
|
|
|
transcribe_btn.click( |
|
extract_text_from_audio, |
|
inputs=[audio_input, audio_language], |
|
outputs=[transcribed_text] |
|
) |
|
|
|
|
|
iface.launch() |
|
|