helvekami/ShukaNote
0
1import transformers2import gradio as gr3import librosa4import torch5import spaces6import numpy as np7import tempfile8 9@spaces.GPU(duration=60)10def transcribe_and_respond(audio_file, email):11 try:12 pipe = transformers.pipeline(13 model='sarvamai/shuka_v1',14 trust_remote_code=True,15 device=0,16 torch_dtype=torch.bfloat1617 )18 19 # Load the audio file at 16kHz20 audio, sr = librosa.load(audio_file, sr=16000)21 # Convert the audio to a contiguous float32 array22 audio = np.ascontiguousarray(audio, dtype=np.float32)23 # If audio is multi-channel, convert to mono by averaging channels24 if audio.ndim > 1:25 audio = np.mean(audio, axis=-1)26 27 # Debug: Print audio properties28 print(f"Audio dtype: {audio.dtype}, Audio shape: {audio.shape}, Sample rate: {sr}")29 30 # Set up the prompt to get key takeaways31 turns = [32 {'role': 'system', 'content': 'You are an exact echo assistant. Output the previous text exactly as given, without any modifications.'},33 {'role': 'user', 'content': '<|audio|>'}34 ]35 print(f"Initial turns: {turns}")36 37 # Run the model inference (this call is synchronous)38 output = pipe({'audio': audio, 'turns': turns, 'sampling_rate': sr}, max_new_tokens=10000)39 print(f"Model output: {output}")40 41 # Extract transcript text from the output42 transcript = str(output)43 if email and email.strip():44 transcript = f"Email provided: {email}\n\n{transcript}"45 46 # Write the transcript to a temporary file for download47 with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as tmp:48 tmp.write(transcript)49 transcript_file = tmp.name50 51 # Return transcript text and file download path52 return transcript, transcript_file53 54 except Exception as e:55 return f"Error: {str(e)}", ""56 57iface = gr.Interface(58 fn=transcribe_and_respond,59 inputs=[60 gr.Audio(sources=["upload", "microphone"], type="filepath"),61 # gr.Textbox(label="Email", placeholder="Enter your email address (optional)")62 ],63 outputs=[64 gr.Textbox(label="Transcript"),65 gr.File(label="Download Transcript")66 ],67 title="ShukaNotesApp",68 description="Upload or record your meeting audio, and download the transcript."69)70 71if __name__ == "__main__":72 iface.launch()73 