Spaces:
Sleeping
Sleeping
Enhance audio input functionality to support both recording and file uploads, and update related status messages accordingly.
e040356
| import gradio as gr | |
| from typhoon_asr import transcribe | |
| # Global variable to store transcription results | |
| last_transcription = None | |
| def transcribe_audio(audio_path): | |
| """Transcribe the audio using typhoon_asr""" | |
| global last_transcription | |
| if not audio_path: | |
| return "β No audio to transcribe. Please record audio first.", "" | |
| try: | |
| # Show loading message | |
| status_msg = "π Transcribing audio..." | |
| # Perform transcription (basic only) | |
| result = transcribe(audio_path) | |
| last_transcription = result | |
| transcription_text = result['text'].text | |
| status_msg = "β Transcription completed!" | |
| return status_msg, transcription_text | |
| except Exception as e: | |
| error_msg = f"β Transcription failed: {str(e)}" | |
| return error_msg, "" | |
| def clear_transcription(): | |
| """Clear the transcription""" | |
| global last_transcription | |
| last_transcription = None | |
| return "ποΈ Transcription cleared", "" | |
| def audio_recorded(audio_path): | |
| """Called when audio is recorded or uploaded - update status and enable button""" | |
| if audio_path: | |
| return f"β Audio ready! Ready to transcribe.", gr.Button(interactive=True) | |
| else: | |
| return "β No audio available", gr.Button(interactive=False) | |
| # Create the Gradio interface | |
| with gr.Blocks(title="Direct Audio Transcription") as demo: | |
| gr.Markdown("# ποΈ Thai Audio Transcription with Typhoon ASR") | |
| # Audio input component (supports both recording and file upload) | |
| audio_input = gr.Audio( | |
| sources=["microphone", "upload"], | |
| type="filepath", | |
| label="Record Audio or Upload File" | |
| ) | |
| # Status display | |
| status_text = gr.Textbox( | |
| label="Status", | |
| value="Record audio or upload a file to get started", | |
| interactive=False | |
| ) | |
| # Transcription buttons | |
| gr.Markdown("### Transcription") | |
| with gr.Row(): | |
| transcribe_btn = gr.Button("Transcribe", variant="primary", interactive=False) | |
| clear_btn = gr.Button("Clear Result", variant="secondary") | |
| # Transcription result | |
| transcription_output = gr.Textbox( | |
| label="Transcription Result", | |
| lines=10, | |
| placeholder="Transcription will appear here after recording/uploading audio and clicking transcribe...", | |
| interactive=False | |
| ) | |
| # Event handlers | |
| # When audio changes (recorded or uploaded), update status and enable button | |
| audio_input.change( | |
| fn=audio_recorded, | |
| inputs=[audio_input], | |
| outputs=[status_text, transcribe_btn] | |
| ) | |
| # Transcription button click | |
| transcribe_btn.click( | |
| fn=transcribe_audio, | |
| inputs=[audio_input], | |
| outputs=[status_text, transcription_output] | |
| ) | |
| clear_btn.click( | |
| fn=clear_transcription, | |
| outputs=[status_text, transcription_output] | |
| ) | |
| if __name__ == "__main__": | |
| print("Launching Direct Audio Transcription...") | |
| demo.launch() | |
| # Helper functions to access transcription results | |
| def get_last_transcription(): | |
| """Get the last transcription result""" | |
| global last_transcription | |
| return last_transcription | |
| def use_transcription(): | |
| """Example function showing how to use the transcription""" | |
| if last_transcription: | |
| print(f"Last transcription: {last_transcription}") | |
| return last_transcription | |
| else: | |
| print("No transcription available") | |
| return None |