File size: 1,598 Bytes
a7368c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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()