File size: 2,068 Bytes
e6868fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
import gradio as gr
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
import numpy as np
import torch

transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en")

# Load a GPT-2 model for general question answering
tokenizer = AutoTokenizer.from_pretrained("gpt2-medium", cache_dir="./cache")
model = AutoModelForCausalLM.from_pretrained("gpt2-medium", cache_dir="./cache")

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(question):
    input_ids = tokenizer.encode(f"Q: {question}\nA:", return_tensors="pt")
    
    # Generate a response
    with torch.no_grad():
        output = model.generate(input_ids, max_length=150, num_return_sequences=1, 
                                temperature=0.7, top_k=50, top_p=0.95)
    
    response = tokenizer.decode(output[0], skip_special_tokens=True)
    
    # Extract only the answer part
    answer = response.split("A:")[-1].strip()
    print(answer)
    return response

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", lines=10)
    
    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()