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()