File size: 2,147 Bytes
f2ed850
 
 
 
 
 
c697462
f2ed850
 
 
 
 
 
 
 
 
 
 
c697462
f2ed850
 
 
 
 
 
c697462
f2ed850
 
 
 
 
 
 
 
 
 
 
 
 
 
c697462
f2ed850
 
 
 
 
 
 
 
 
 
 
 
 
c697462
 
f2ed850
 
 
 
 
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
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()