testpro / app.py
renad0's picture
Update app.py
9e90c62 verified
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()