File size: 1,414 Bytes
4cb4f3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e90c62
9e0ed23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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()