Update app.py
Browse filesUpdated app.py
app.py
CHANGED
|
@@ -1,56 +1,62 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
# Force upgrade gradio
|
| 5 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "--upgrade", "gradio>=4.44.0"])
|
| 6 |
+
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
from transformers import (
|
| 11 |
+
pipeline,
|
| 12 |
+
WhisperForConditionalGeneration,
|
| 13 |
+
AutoTokenizer,
|
| 14 |
+
WhisperFeatureExtractor,
|
| 15 |
+
GenerationConfig
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
print("π§ Loading ASR components...")
|
| 19 |
+
|
| 20 |
+
# Load generation config and remove forced_decoder_ids
|
| 21 |
+
gen_config = GenerationConfig.from_pretrained("amedcj/whisper-kurmanji")
|
| 22 |
+
gen_config.forced_decoder_ids = None
|
| 23 |
+
|
| 24 |
+
# Load model and set generation config directly
|
| 25 |
+
model = WhisperForConditionalGeneration.from_pretrained("amedcj/whisper-kurmanji")
|
| 26 |
+
model.generation_config = gen_config
|
| 27 |
+
|
| 28 |
+
# Load tokenizer and feature extractor
|
| 29 |
+
tokenizer = AutoTokenizer.from_pretrained("amedcj/whisper-kurmanji")
|
| 30 |
+
feature_extractor = WhisperFeatureExtractor.from_pretrained("amedcj/whisper-kurmanji")
|
| 31 |
+
|
| 32 |
+
# Create the ASR pipeline
|
| 33 |
+
asr = pipeline(
|
| 34 |
+
"automatic-speech-recognition",
|
| 35 |
+
model=model,
|
| 36 |
+
tokenizer=tokenizer,
|
| 37 |
+
feature_extractor=feature_extractor,
|
| 38 |
+
device=-1 # CPU
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
def transcribe(audio_path):
|
| 42 |
+
print("π₯ Transcription triggered")
|
| 43 |
+
if audio_path is None:
|
| 44 |
+
return "Please upload an audio file."
|
| 45 |
+
|
| 46 |
+
array, sampling_rate = librosa.load(audio_path, sr=None)
|
| 47 |
+
result = asr({"array": array, "sampling_rate": sampling_rate})
|
| 48 |
+
return result["text"]
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# Gradio Interface using Blocks with a Submit button (compatible with HF Spaces)
|
| 52 |
+
with gr.Blocks() as demo:
|
| 53 |
+
gr.Markdown("## π£οΈ Kurdish ASR Demo")
|
| 54 |
+
|
| 55 |
+
audio_input = gr.Audio(type="filepath", label="π€ Upload Kurdish Audio")
|
| 56 |
+
submit_btn = gr.Button("Submit")
|
| 57 |
+
output_text = gr.Textbox(label="π Transcription", interactive=False)
|
| 58 |
+
|
| 59 |
+
submit_btn.click(fn=transcribe, inputs=audio_input, outputs=output_text)
|
| 60 |
+
|
| 61 |
+
print("π Launching Gradio app...")
|
| 62 |
+
demo.launch()
|