Update app.py
Browse files
app.py
CHANGED
|
@@ -1,93 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
-
import traceback
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
# --- MODEL LOADING FUNCTION ---
|
| 12 |
-
asr = None
|
| 13 |
-
def load_asr_model():
|
| 14 |
-
global asr
|
| 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}")
|
| 33 |
-
traceback.print_exc()
|
| 34 |
-
asr = None
|
| 35 |
-
|
| 36 |
-
load_asr_model() # Load the model at start
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
def
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
try:
|
| 43 |
-
if audio_file is None:
|
| 44 |
-
return "Ji kerema xwe dosyeyek deng bar bike. / Please upload an audio file."
|
| 45 |
-
|
| 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 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
except Exception as e:
|
| 61 |
-
error_msg = f"Çewtî: {str(e)} / Error: {str(e)}"
|
| 62 |
-
print(f"❌ Error in transcription: {e}")
|
| 63 |
-
traceback.print_exc()
|
| 64 |
-
return error_msg
|
| 65 |
|
| 66 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
demo = gr.Interface(
|
| 68 |
-
fn=
|
| 69 |
inputs=gr.Audio(
|
| 70 |
-
sources=["microphone", "upload"],
|
| 71 |
type="filepath",
|
| 72 |
-
label="
|
| 73 |
-
),
|
| 74 |
-
outputs=gr.Textbox(
|
| 75 |
-
label="📝 Encam / Result",
|
| 76 |
-
placeholder="Li vir nivîsa wergerandî dê xuya bibe... / Transcribed text will appear here...",
|
| 77 |
-
lines=5,
|
| 78 |
-
show_copy_button=True
|
| 79 |
),
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
**Formatên çêdibin:** WAV, MP3, M4A, FLAC
|
| 84 |
-
""",
|
| 85 |
-
submit_btn="Wergerîne / Transcribe",
|
| 86 |
-
clear_btn="Paqij Bike / Clear",
|
| 87 |
-
cache_examples=False
|
| 88 |
)
|
| 89 |
|
| 90 |
-
#
|
| 91 |
-
|
| 92 |
-
if __name__ == "__main__":
|
| 93 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# IMPORTANT: Replace this with the exact ID of your uploaded model
|
| 5 |
+
MODEL_ID = "amedcj/kurmanji-asr-model" # Assuming your model ID uses your Space's username
|
| 6 |
|
| 7 |
+
# Load the ASR model pipeline
|
| 8 |
+
# The pipeline handles downloading the weights and configuration.
|
| 9 |
+
try:
|
| 10 |
+
transcriber = pipeline(
|
| 11 |
+
"automatic-speech-recognition",
|
| 12 |
+
model=MODEL_ID,
|
| 13 |
+
# device=0 # Uncomment this if you upgrade your Space to a GPU
|
| 14 |
+
)
|
| 15 |
+
except Exception as e:
|
| 16 |
+
# Fallback for error handling if the model fails to load
|
| 17 |
+
gr.Warning(f"Failed to load model: {e}")
|
| 18 |
+
transcriber = None
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Define the prediction function
|
| 22 |
+
def transcribe_audio(audio_file_path):
|
| 23 |
+
if audio_file_path is None:
|
| 24 |
+
return "Please provide an audio input."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
if transcriber is None:
|
| 27 |
+
return "Error: Model failed to initialize."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Pass the local file path provided by Gradio to the pipeline
|
| 30 |
+
result = transcriber(audio_file_path)
|
| 31 |
+
return result["text"]
|
| 32 |
+
|
| 33 |
+
# Create the Gradio interface
|
| 34 |
demo = gr.Interface(
|
| 35 |
+
fn=transcribe_audio,
|
| 36 |
inputs=gr.Audio(
|
| 37 |
+
sources=["microphone", "upload"],
|
| 38 |
type="filepath",
|
| 39 |
+
label="Kurmanji Audio Input"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
),
|
| 41 |
+
outputs=gr.Textbox(label="Kurmanji Transcription Result"),
|
| 42 |
+
title="Kurmanji ASR Demo",
|
| 43 |
+
description="Automatic Speech Recognition for Kurmanji using a fine-tuned Hugging Face Transformer model."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
)
|
| 45 |
|
| 46 |
+
# Launch the application
|
| 47 |
+
demo.launch()
|
|
|
|
|
|