DeepSpeech / app.py
suhaib0edu's picture
Update app.py
c697462 verified
import os
import numpy as np
import gradio as gr
import deepspeech
import requests
import tarfile
import wave
# إعداد المسار للنموذج
MODEL_DIR = 'deepspeech_model'
MODEL_PATH = os.path.join(MODEL_DIR, 'deepspeech-0.9.3-models.pbmm')
SCORER_PATH = os.path.join(MODEL_DIR, 'deepspeech-0.9.3-models.scorer')
AUDIO_URL = 'https://github.com/mozilla/DeepSpeech/releases/download/v0.9.3/audio-0.9.3.tar.gz'
# تحميل النموذج إذا لم يكن موجودًا
def download_model():
if not os.path.exists(MODEL_DIR):
os.makedirs(MODEL_DIR)
if not os.path.exists(MODEL_PATH):
print("Downloading model...")
model_url = 'https://github.com/mozilla/DeepSpeech/releases/download/v0.9.3/deepspeech-0.9.3-models.pbmm'
response = requests.get(model_url)
with open(MODEL_PATH, 'wb') as f:
f.write(response.content)
if not os.path.exists(SCORER_PATH):
print("Downloading scorer...")
scorer_url = 'https://github.com/mozilla/DeepSpeech/releases/download/v0.9.3/deepspeech-0.9.3-models.scorer'
response = requests.get(scorer_url)
with open(SCORER_PATH, 'wb') as f:
f.write(response.content)
# تحميل ملفات الصوت الأمثلة
def download_audio_samples():
if not os.path.exists('audio'):
print("Downloading audio samples...")
response = requests.get(AUDIO_URL)
with open('audio-0.9.3.tar.gz', 'wb') as f:
f.write(response.content)
with tarfile.open('audio-0.9.3.tar.gz') as tar:
tar.extractall()
# تحميل النموذج وملفات الصوت
download_model()
download_audio_samples()
# تحميل النموذج
ds_model = deepspeech.Model(MODEL_PATH)
ds_model.enableExternalScorer(SCORER_PATH)
# دالة لتحويل الصوت إلى نص
def transcribe(audio):
with wave.open(audio.name, 'rb') as w:
audio_data = np.frombuffer(w.readframes(w.getnframes()), np.int16)
return ds_model.stt(audio_data)
# واجهة المستخدم
interface = gr.Interface(fn=transcribe, inputs="audio", outputs="text")
interface.launch()