Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import torch
|
4 |
+
import torchaudio
|
5 |
+
|
6 |
+
# تحميل نموذج الترجمة
|
7 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ar")
|
8 |
+
|
9 |
+
# تحميل نموذج تحويل النص إلى صوت
|
10 |
+
tts_model = pipeline("text-to-speech", model="microsoft/speecht5_tts")
|
11 |
+
|
12 |
+
def translate_and_speak(text, lang):
|
13 |
+
# تحديد اتجاه الترجمة
|
14 |
+
if lang == "English → Arabic":
|
15 |
+
translation = translator(text, max_length=200)[0]['translation_text']
|
16 |
+
else:
|
17 |
+
translation = translator(text, max_length=200, src_lang="ar", tgt_lang="en")[0]['translation_text']
|
18 |
+
|
19 |
+
# تحويل النص إلى صوت
|
20 |
+
tts_audio = tts_model(translation)
|
21 |
+
|
22 |
+
return translation, (tts_audio["sampling_rate"], tts_audio["audio"])
|
23 |
+
|
24 |
+
# إنشاء واجهة
|
25 |
+
iface = gr.Interface(
|
26 |
+
fn=translate_and_speak,
|
27 |
+
inputs=[
|
28 |
+
gr.Textbox(label="أدخل النص"),
|
29 |
+
gr.Radio(["English → Arabic", "Arabic → English"], label="اتجاه الترجمة"),
|
30 |
+
],
|
31 |
+
outputs=[
|
32 |
+
gr.Textbox(label="النص المترجم"),
|
33 |
+
gr.Audio(label="النص المنطوق"),
|
34 |
+
],
|
35 |
+
title="مترجم ونطق النصوص",
|
36 |
+
description="هذا النموذج يترجم النصوص بين العربية والإنجليزية ثم يحولها إلى صوت."
|
37 |
+
)
|
38 |
+
|
39 |
+
if __name__ == "__main__":
|
40 |
+
iface.launch()
|