NCAIR1-Igbo-ASR / script.py
panamabananaman's picture
Upload script.py
5999e0b verified
# Let's create a sample code for deploying NCAIR1/Igbo-ASR to Hugging Face Space
gradio_code = '''import gradio as gr
import torch
from transformers import WhisperProcessor, WhisperForConditionalGeneration
import librosa
import numpy as np
# Load model and processor
model_name = "NCAIR1/Igbo-ASR"
processor = WhisperProcessor.from_pretrained(model_name)
model = WhisperForConditionalGeneration.from_pretrained(model_name)
def transcribe_audio(audio):
"""
Transcribe audio to Igbo text using NCAIR1/Igbo-ASR model
"""
if audio is None:
return "Please upload an audio file or record audio."
try:
# Handle audio input (audio is a tuple: (sample_rate, audio_data))
sample_rate, audio_data = audio
# Convert to float32 if needed
if audio_data.dtype != np.float32:
audio_data = audio_data.astype(np.float32)
# Normalize audio if needed
if np.max(np.abs(audio_data)) > 1.0:
audio_data = audio_data / np.max(np.abs(audio_data))
# Resample to 16kHz if needed
if sample_rate != 16000:
audio_data = librosa.resample(audio_data, orig_sr=sample_rate, target_sr=16000)
# Process audio
input_features = processor(audio_data, sampling_rate=16000, return_tensors="pt").input_features
# Generate transcription
with torch.no_grad():
predicted_ids = model.generate(input_features)
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
return transcription[0] if transcription else "No transcription generated."
except Exception as e:
return f"Error processing audio: {str(e)}"
# Create Gradio interface
interface = gr.Interface(
fn=transcribe_audio,
inputs=gr.Audio(sources=["microphone", "upload"], type="numpy"),
outputs=gr.Textbox(label="Igbo Transcription", placeholder="Transcribed text will appear here..."),
title="🎙️ Igbo Speech Recognition",
description="Upload an audio file or record your voice speaking in Igbo to get the transcription using NCAIR1/Igbo-ASR model.",
examples=None, # You can add example audio files here
cache_examples=False
)
if __name__ == "__main__":
interface.launch()
'''
# Save the code to a file for reference
with open("igbo_asr_gradio_app.py", "w") as f:
f.write(gradio_code)
print("Sample Gradio app code created successfully!")
print("File: igbo_asr_gradio_app.py")
print("\nNext steps:")
print("1. Create a new Hugging Face Space")
print("2. Upload this code as app.py")
print("3. Add requirements.txt with dependencies")
print("4. Configure the Space settings")