Spaces:
Sleeping
Sleeping
File size: 4,111 Bytes
f79c89e b0e29e7 d5f394e f79c89e d5f394e f79c89e d5f394e b0e29e7 f79c89e d5f394e f79c89e d5f394e f79c89e d5f394e f79c89e d5f394e f79c89e d5f394e f79c89e b0e29e7 d5f394e 618c56b d5f394e b0e29e7 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
import os
import json
import requests
import gradio as gr
import whisper
import torch
# تهيئة النماذج
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
whisper_model = whisper.load_model("base")
# مفتاح API لـ Gemini
GEMINI_API_KEY = "AIzaSyDrHCW4FxrDt6amCTQvYPTdh2NE06p9YlQ"
GEMINI_API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
# قاموس للغات المدعومة
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):
"""ترجمة النص باستخدام Gemini API"""
if source_lang == target_lang:
return text
try:
# تحضير الطلب للترجمة
prompt = f"Translate the following text from {SUPPORTED_LANGUAGES[source_lang]} to {SUPPORTED_LANGUAGES[target_lang]}. Only provide the translation without any additional text or explanation:\n\n{text}"
payload = {
"contents": [{
"parts": [{
"text": prompt
}]
}]
}
# إضافة مفتاح API كمعامل URL
url = f"{GEMINI_API_URL}?key={GEMINI_API_KEY}"
# إرسال الطلب
response = requests.post(
url,
headers={"Content-Type": "application/json"},
json=payload
)
if response.status_code == 200:
result = response.json()
# استخراج النص المترجم من الاستجابة
translated_text = result['candidates'][0]['content']['parts'][0]['text']
return translated_text
else:
return f"خطأ في الترجمة: {response.status_code} - {response.text}"
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() |