File size: 2,993 Bytes
f79c89e
d5f394e
 
f79c89e
f4ac0b9
d5f394e
f79c89e
 
 
d5f394e
f79c89e
d5f394e
f79c89e
 
 
 
d5f394e
 
f79c89e
 
d5f394e
f79c89e
 
d5f394e
f79c89e
d5f394e
f79c89e
 
d5f394e
 
618c56b
d5f394e
f4ac0b9
 
 
f79c89e
d5f394e
 
 
f79c89e
 
 
 
 
618c56b
f79c89e
f4ac0b9
 
 
 
 
f79c89e
 
 
 
 
 
 
 
 
 
 
618c56b
f79c89e
 
618c56b
 
f4ac0b9
 
 
 
 
 
 
 
 
 
f79c89e
 
 
 
 
 
 
618c56b
d5f394e
f79c89e
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import os
import gradio as gr
import whisper
import torch
from transformers import pipeline

# تهيئة النماذج
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
whisper_model = whisper.load_model("base")

# قاموس للغات المدعومة
SUPPORTED_LANGUAGES = {
    "ar": "العربية",
    "en": "English",
    "fr": "Français",
    "es": "Español"
}

def transcribe_audio(audio_file, source_lang):
    """تحويل الصوت إلى نص باستخدام Whisper"""
    try:
        result = whisper_model.transcribe(audio_file, language=source_lang)
        return result["text"]
    except Exception as e:
        return f"خطأ في التحويل: {str(e)}"

def translate_text(text, source_lang, target_lang):
    """ترجمة النص بين اللغات"""
    if source_lang == target_lang:
        return text
    
    try:
        translator = pipeline("translation", 
                            model=f"Helsinki-NLP/opus-mt-{source_lang}-{target_lang}")
        result = translator(text)[0]['translation_text']
        return result
    except Exception as e:
        return f"خطأ في الترجمة: {str(e)}"

# إنشاء واجهة Gradio
with gr.Blocks(title="معالج الصوت والترجمة", theme=gr.themes.Soft()) as demo:
    gr.Markdown("# معالج الصوت والترجمة متعدد اللغات")
    
    with gr.Tab("تحويل الصوت إلى نص"):
        with gr.Row():
            audio_input = gr.Audio(type="filepath", label="الملف الصوتي")
            source_lang = gr.Dropdown(
                choices=list(SUPPORTED_LANGUAGES.keys()),
                value="ar",
                label="لغة الملف الصوتي"
            )
        
        transcribe_btn = gr.Button("تحويل إلى نص")
        transcribed_text = gr.Textbox(label="النص المستخرج", lines=5)
        
        transcribe_btn.click(
            fn=transcribe_audio,
            inputs=[audio_input, source_lang],
            outputs=transcribed_text
        )
    
    with gr.Tab("ترجمة النص"):
        with gr.Row():
            input_text = gr.Textbox(label="النص المراد ترجمته", lines=5)
            translated_text = gr.Textbox(label="النص المترجم", lines=5)
        
        with gr.Row():
            trans_source_lang = gr.Dropdown(
                choices=list(SUPPORTED_LANGUAGES.keys()),
                value="ar",
                label="اللغة المصدر"
            )
            trans_target_lang = gr.Dropdown(
                choices=list(SUPPORTED_LANGUAGES.keys()),
                value="en",
                label="اللغة الهدف"
            )
        
        translate_btn = gr.Button("ترجمة")
        
        translate_btn.click(
            fn=translate_text,
            inputs=[input_text, trans_source_lang, trans_target_lang],
            outputs=translated_text
        )

# تشغيل التطبيق
demo.launch()