Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import os
|
|
| 5 |
|
| 6 |
print("π Starting Kurmanji ASR application...")
|
| 7 |
|
| 8 |
-
# Define MODEL_ID
|
| 9 |
MODEL_ID = "."
|
| 10 |
|
| 11 |
# --- MODEL LOADING FUNCTION ---
|
|
@@ -15,16 +15,18 @@ def load_asr_model():
|
|
| 15 |
try:
|
| 16 |
print("π₯ Loading ASR pipeline...")
|
| 17 |
|
|
|
|
| 18 |
asr = pipeline(
|
| 19 |
"automatic-speech-recognition",
|
| 20 |
model=MODEL_ID,
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
print("β
ASR pipeline created successfully!")
|
| 29 |
except Exception as e:
|
| 30 |
print(f"β Error loading ASR model: {e}")
|
|
@@ -36,7 +38,6 @@ load_asr_model() # Load the model at start
|
|
| 36 |
# --- TRANSCRIPTION FUNCTION ---
|
| 37 |
def transcribe(audio_file):
|
| 38 |
print("=== ASR Function Called ===")
|
| 39 |
-
print(f"Audio file: {audio_file}")
|
| 40 |
|
| 41 |
try:
|
| 42 |
if audio_file is None:
|
|
@@ -45,10 +46,13 @@ def transcribe(audio_file):
|
|
| 45 |
if asr is None:
|
| 46 |
return "Model nehatiye barkirin. / ASR model not loaded properly."
|
| 47 |
|
| 48 |
-
print("π΅ Processing audio file
|
| 49 |
|
|
|
|
| 50 |
result = asr(audio_file)
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
|
| 53 |
print(f"β
Transcription completed: {transcription}")
|
| 54 |
return transcription
|
|
@@ -86,5 +90,4 @@ demo = gr.Interface(
|
|
| 86 |
# --- LAUNCH ---
|
| 87 |
print("π Launching Gradio app...")
|
| 88 |
if __name__ == "__main__":
|
| 89 |
-
# FIX APPLIED HERE: Removed 'ssr_mode=False'
|
| 90 |
demo.launch()
|
|
|
|
| 5 |
|
| 6 |
print("π Starting Kurmanji ASR application...")
|
| 7 |
|
| 8 |
+
# Define MODEL_ID: Set to '.' because model files are in the same Space repository.
|
| 9 |
MODEL_ID = "."
|
| 10 |
|
| 11 |
# --- MODEL LOADING FUNCTION ---
|
|
|
|
| 15 |
try:
|
| 16 |
print("π₯ Loading ASR pipeline...")
|
| 17 |
|
| 18 |
+
# Use the pipeline's auto-loading feature
|
| 19 |
asr = pipeline(
|
| 20 |
"automatic-speech-recognition",
|
| 21 |
model=MODEL_ID,
|
| 22 |
+
# Optimized for limited resources/speedβyou may need to adjust these values
|
| 23 |
+
chunk_length_s=5, # Smaller chunks require less memory
|
| 24 |
+
stride_length_s=(1, 1), # Reduced stride for less overlap
|
| 25 |
+
device="cpu", # Explicitly use CPU (important for free tier)
|
| 26 |
+
low_cpu_mem_usage=True, # Saves RAM during model initialization
|
| 27 |
+
# VITAL FOR QUALITY: Force the model to use the Kurdish language (ku)
|
| 28 |
+
generate_kwargs={"language": "ku", "task": "transcribe"}
|
| 29 |
+
)
|
| 30 |
print("β
ASR pipeline created successfully!")
|
| 31 |
except Exception as e:
|
| 32 |
print(f"β Error loading ASR model: {e}")
|
|
|
|
| 38 |
# --- TRANSCRIPTION FUNCTION ---
|
| 39 |
def transcribe(audio_file):
|
| 40 |
print("=== ASR Function Called ===")
|
|
|
|
| 41 |
|
| 42 |
try:
|
| 43 |
if audio_file is None:
|
|
|
|
| 46 |
if asr is None:
|
| 47 |
return "Model nehatiye barkirin. / ASR model not loaded properly."
|
| 48 |
|
| 49 |
+
print(f"π΅ Processing audio file: {audio_file}")
|
| 50 |
|
| 51 |
+
# Transcribe the audio
|
| 52 |
result = asr(audio_file)
|
| 53 |
+
|
| 54 |
+
# Post-processing: clean output and remove leading/trailing spaces
|
| 55 |
+
transcription = result["text"].strip()
|
| 56 |
|
| 57 |
print(f"β
Transcription completed: {transcription}")
|
| 58 |
return transcription
|
|
|
|
| 90 |
# --- LAUNCH ---
|
| 91 |
print("π Launching Gradio app...")
|
| 92 |
if __name__ == "__main__":
|
|
|
|
| 93 |
demo.launch()
|