|
import gradio as gr |
|
from transformers import pipeline |
|
import torch |
|
import torchaudio |
|
|
|
|
|
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ar") |
|
|
|
|
|
tts_model = pipeline("text-to-speech", model="microsoft/speecht5_tts") |
|
|
|
def translate_and_speak(text, lang): |
|
|
|
if lang == "English → Arabic": |
|
translation = translator(text, max_length=200)[0]['translation_text'] |
|
else: |
|
translation = translator(text, max_length=200, src_lang="ar", tgt_lang="en")[0]['translation_text'] |
|
|
|
|
|
tts_audio = tts_model(translation) |
|
|
|
return translation, (tts_audio["sampling_rate"], tts_audio["audio"]) |
|
|
|
|
|
iface = gr.Interface( |
|
fn=translate_and_speak, |
|
inputs=[ |
|
gr.Textbox(label="أدخل النص"), |
|
gr.Radio(["English → Arabic", "Arabic → English"], label="اتجاه الترجمة"), |
|
], |
|
outputs=[ |
|
gr.Textbox(label="النص المترجم"), |
|
gr.Audio(label="النص المنطوق"), |
|
], |
|
title="مترجم ونطق النصوص", |
|
description="هذا النموذج يترجم النصوص بين العربية والإنجليزية ثم يحولها إلى صوت." |
|
) |
|
|
|
if __name__ == "__main__": |
|
iface.launch() |
|
|
|
|
|
|