ShukaNote / app.py
helvekami's picture
Updated Gradio App
1895fc7
raw
history blame
1.84 kB
import transformers
import gradio as gr
import librosa
import torch
import spaces
import numpy as np
@spaces.GPU(duration=60)
def transcribe_audio(audio_file):
try:
pipe = transformers.pipeline(
model='sarvamai/shuka_v1',
trust_remote_code=True,
device=0,
torch_dtype=torch.bfloat16
)
# Load the audio file at 16kHz
audio, sr = librosa.load(audio_file, sr=16000)
# Ensure audio is a floating-point numpy array
audio = np.array(audio, dtype=np.float32)
# Convert multi-channel audio to mono if needed
if audio.ndim > 1:
audio = np.mean(audio, axis=-1)
# Debug: Print audio properties for troubleshooting
print(f"Audio dtype: {audio.dtype}, Audio shape: {audio.shape}, Sample rate: {sr}")
# Change the conversation turns to instruct transcription
turns = [
{'role': 'system', 'content': 'Please transcribe the following audio exactly.'},
{'role': 'user', 'content': '<|audio|>'}
]
# Debug: Print the initial turns
print(f"Initial turns: {turns}")
# Call the model with the audio and transcription prompt
output = pipe({'audio': audio, 'turns': turns, 'sampling_rate': sr}, max_new_tokens=512)
# Debug: Print the final output from the model
print(f"Model output: {output}")
return output
except Exception as e:
return f"Error: {str(e)}"
iface = gr.Interface(
fn=transcribe_audio,
inputs=gr.Audio(sources="microphone", type="filepath"),
outputs="text",
title="Shuka ASR Demo",
description="Speak into your microphone, and the model will transcribe your speech.",
live=True
)
if __name__ == "__main__":
iface.launch()