Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from transformers import pipeline | |
| import numpy as np | |
| transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en") | |
| qa_model = pipeline("question-answering", model="distilbert-base-cased-distilled-squad") | |
| def transcribe(audio): | |
| if audio is None: | |
| return "No audio recorded." | |
| sr, y = audio | |
| y = y.astype(np.float32) | |
| y /= np.max(np.abs(y)) | |
| return transcriber({"sampling_rate": sr, "raw": y})["text"] | |
| def answer(transcription): | |
| context = "You are chatbot answering general questions" | |
| print(transcription) | |
| result = qa_model(question=transcription, context=context) | |
| print(result) | |
| return result['answer'] | |
| def process_audio(audio): | |
| if audio is None: | |
| return "No audio recorded.", "" | |
| transcription = transcribe(audio) | |
| answer_result = answer(transcription) | |
| return transcription, answer_result | |
| def clear_all(): | |
| return None, "", "" | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Audio Transcription and Question Answering") | |
| audio_input = gr.Audio(label="Audio Input", sources=["microphone"], type="numpy") | |
| transcription_output = gr.Textbox(label="Transcription") | |
| answer_output = gr.Textbox(label="Answer Result") | |
| clear_button = gr.Button("Clear") | |
| audio_input.stop_recording( | |
| fn=process_audio, | |
| inputs=[audio_input], | |
| outputs=[transcription_output, answer_output] | |
| ) | |
| clear_button.click( | |
| fn=clear_all, | |
| inputs=[], | |
| outputs=[audio_input, transcription_output, answer_output] | |
| ) | |
| demo.launch() |