Update app.py
Browse files
app.py
CHANGED
|
@@ -3,21 +3,13 @@ from transformers import pipeline
|
|
| 3 |
import soundfile as sf
|
| 4 |
import librosa
|
| 5 |
import numpy as np
|
| 6 |
-
import urllib.request
|
| 7 |
from fastapi import FastAPI
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
|
| 10 |
# --- تعريف النموذج ودالة المعالجة (لا تغيير هنا) ---
|
| 11 |
MODEL_ID = "svu22/arabic_model"
|
| 12 |
-
EXAMPLE_FILE = "example.wav"
|
| 13 |
-
EXAMPLE_URL = "https://raw.githubusercontent.com/zaidalyafeai/ARBML/master/datasets/Speech_Recognition/audio_samples/arabic_speech_sample.wav"
|
| 14 |
|
| 15 |
-
|
| 16 |
-
urllib.request.urlretrieve(EXAMPLE_URL, EXAMPLE_FILE)
|
| 17 |
-
example_list = [[EXAMPLE_FILE]]
|
| 18 |
-
except Exception as e:
|
| 19 |
-
print(f"Could not download example file: {e}")
|
| 20 |
-
example_list = []
|
| 21 |
|
| 22 |
try:
|
| 23 |
pipe = pipeline("automatic-speech-recognition", model=MODEL_ID)
|
|
@@ -48,7 +40,7 @@ def transcribe_audio(audio_input):
|
|
| 48 |
return f"An error occurred: {str(e)}"
|
| 49 |
|
| 50 |
# =============================================================================
|
| 51 |
-
# [تغيير
|
| 52 |
# =============================================================================
|
| 53 |
with gr.Blocks() as interface:
|
| 54 |
gr.Markdown("## Automatic Speech Recognition for Arabic")
|
|
@@ -58,26 +50,19 @@ with gr.Blocks() as interface:
|
|
| 58 |
audio_input = gr.Audio(sources=["upload", "microphone"], type="filepath", label="Audio File")
|
| 59 |
transcribed_text = gr.Textbox(label="Result", interactive=False)
|
| 60 |
|
| 61 |
-
# هذا الزر هو ما سيستدعي الدالة
|
| 62 |
submit_btn = gr.Button("Transcribe")
|
| 63 |
|
| 64 |
-
# عند الضغط على الزر، يتم استدعاء الدالة
|
| 65 |
-
# السطر الأهم هو 'api_name="predict"' الذي يجبر نقطة النهاية على أن تكون /run/predict
|
| 66 |
submit_btn.click(
|
| 67 |
fn=transcribe_audio,
|
| 68 |
inputs=audio_input,
|
| 69 |
outputs=transcribed_text,
|
| 70 |
-
api_name="predict"
|
| 71 |
)
|
| 72 |
|
| 73 |
-
gr.Examples
|
| 74 |
-
examples=example_list,
|
| 75 |
-
inputs=audio_input,
|
| 76 |
-
label="Examples"
|
| 77 |
-
)
|
| 78 |
|
| 79 |
# =============================================================================
|
| 80 |
-
# [
|
| 81 |
# =============================================================================
|
| 82 |
app = FastAPI()
|
| 83 |
|
|
@@ -89,6 +74,5 @@ app.add_middleware(
|
|
| 89 |
allow_headers=["*"],
|
| 90 |
)
|
| 91 |
|
| 92 |
-
# تحميل تطبيق Gradio داخل FastAPI
|
| 93 |
app = gr.mount_gradio_app(app, interface, path="/")
|
| 94 |
|
|
|
|
| 3 |
import soundfile as sf
|
| 4 |
import librosa
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
from fastapi import FastAPI
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
|
| 9 |
# --- تعريف النموذج ودالة المعالجة (لا تغيير هنا) ---
|
| 10 |
MODEL_ID = "svu22/arabic_model"
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# --- تم حذف قسم الأمثلة بالكامل لأنه كان يسبب خطأ في بدء التشغيل ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
try:
|
| 15 |
pipe = pipeline("automatic-speech-recognition", model=MODEL_ID)
|
|
|
|
| 40 |
return f"An error occurred: {str(e)}"
|
| 41 |
|
| 42 |
# =============================================================================
|
| 43 |
+
# [لا تغيير هنا] - بناء الواجهة باستخدام gr.Blocks
|
| 44 |
# =============================================================================
|
| 45 |
with gr.Blocks() as interface:
|
| 46 |
gr.Markdown("## Automatic Speech Recognition for Arabic")
|
|
|
|
| 50 |
audio_input = gr.Audio(sources=["upload", "microphone"], type="filepath", label="Audio File")
|
| 51 |
transcribed_text = gr.Textbox(label="Result", interactive=False)
|
| 52 |
|
|
|
|
| 53 |
submit_btn = gr.Button("Transcribe")
|
| 54 |
|
|
|
|
|
|
|
| 55 |
submit_btn.click(
|
| 56 |
fn=transcribe_audio,
|
| 57 |
inputs=audio_input,
|
| 58 |
outputs=transcribed_text,
|
| 59 |
+
api_name="predict"
|
| 60 |
)
|
| 61 |
|
| 62 |
+
# --- تم حذف gr.Examples من هنا أيضاً ---
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# =============================================================================
|
| 65 |
+
# [لا تغيير هنا] - إضافة إعدادات CORS باستخدام FastAPI
|
| 66 |
# =============================================================================
|
| 67 |
app = FastAPI()
|
| 68 |
|
|
|
|
| 74 |
allow_headers=["*"],
|
| 75 |
)
|
| 76 |
|
|
|
|
| 77 |
app = gr.mount_gradio_app(app, interface, path="/")
|
| 78 |
|