interview_copilot / app copy 2.py
alex buz
final
e6868fd
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()