Spaces:
Sleeping
Sleeping
Download app.py from theuserme/twi_speech_model: direct link, hf CLI and curl.
- Browser
- Download file 1.86 kB
-
https://huggingface.co/spaces/theuserme/twi_speech_model/resolve/main/app.py
- Command line
-
hf download hf://spaces/theuserme/twi_speech_model/app.py
-
curl -L -o app.py https://huggingface.co/spaces/theuserme/twi_speech_model/resolve/main/app.py
1.86 kB
| import gradio as gr | |
| import torch | |
| import librosa | |
| from transformers import WhisperProcessor, WhisperForConditionalGeneration | |
| from peft import PeftModel | |
| print("Loading models... this takes about a minute on a free CPU...") | |
| MODEL_ID = "openai/whisper-small" | |
| ADAPTER_ID = "theuserme/twi-whisper-qlora" | |
| processor = WhisperProcessor.from_pretrained(MODEL_ID) | |
| base_model = WhisperForConditionalGeneration.from_pretrained(MODEL_ID) | |
| model = PeftModel.from_pretrained(base_model, ADAPTER_ID) | |
| print("Model loaded successfully!") | |
| def transcribe_audio(audio_path): | |
| if audio_path is None: | |
| return "❌ Please record or upload an audio file." | |
| try: | |
| y, sr = librosa.load(audio_path, sr=16000) | |
| inputs = processor(y, sampling_rate=16000, return_tensors="pt") | |
| with torch.no_grad(): | |
| # THE FIX: Removed the '**' so the Tensor is passed correctly | |
| predicted_ids = model.generate(inputs.input_features, max_new_tokens=225) | |
| transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0] | |
| return transcription | |
| except Exception as e: | |
| return f"Error processing audio: {str(e)}" | |
| # Build the Professional Web Interface | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# 🇬🇭 Twi Speech Recognition (AI)") | |
| gr.Markdown("Speak Twi into your microphone, or upload an audio file.") | |
| with gr.Row(): | |
| audio_input = gr.Audio(sources=["microphone", "upload"], type="filepath", label="Audio Input") | |
| with gr.Row(): | |
| transcribe_btn = gr.Button("Transcribe Audio", variant="primary") | |
| with gr.Row(): | |
| text_output = gr.Textbox(label="Twi Transcription", lines=4) | |
| transcribe_btn.click(fn=transcribe_audio, inputs=audio_input, outputs=text_output) | |
| demo.launch() |