voice_transcriber / app copy 2.py
alex buz
fix
1cd886e
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):
# This is a placeholder. In a real scenario, you'd have a predefined context or retrieve it based on the transcription.
context = "Gradio is a Python library for building machine learning web apps. It was created to make it easy for machine learning developers to demo their work."
result = qa_model(question=transcription, context=context)
return result['answer']
def process_audio(audio):
transcription = transcribe(audio)
answer_result = answer(transcription)
return transcription, answer_result
with gr.Blocks() as demo:
gr.Markdown("# Audio Transcription and Question Answering")
audio_input = gr.Audio(label="Audio Input", sources=["microphone"])
transcription_output = gr.Textbox(label="Transcription")
answer_output = gr.Textbox(label="Answer Result")
submit_button = gr.Button("Submit")
submit_button.click(
fn=process_audio,
inputs=[audio_input],
outputs=[transcription_output, answer_output]
)
demo.launch()