amyakir's picture
Update app.py
4f8a704 verified
raw
history blame
2.01 kB
import gradio as gr
from transformers import pipeline
import torch
from TTS.api import TTS
import tempfile
import os
import speech_recognition as sr
# Load question generation model
qg_pipeline = pipeline("text2text-generation", model="valhalla/t5-small-e2e-qg")
# Load TTS model
tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False, gpu=False)
def generate_question(text):
# Prompt for question generation
input_text = f"generate question: {text.strip()}"
question = qg_pipeline(input_text)[0]["generated_text"]
# Save spoken question as audio
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as fp:
tts.tts_to_file(text=question, file_path=fp.name)
audio_path = fp.name
return question, audio_path
def transcribe_audio(audio_path):
recognizer = sr.Recognizer()
with sr.AudioFile(audio_path) as source:
audio_data = recognizer.record(source)
try:
return recognizer.recognize_google(audio_data)
except sr.UnknownValueError:
return "Sorry, I could not understand your answer."
except sr.RequestError:
return "Sorry, there was an error with the speech recognition service."
with gr.Blocks() as app:
gr.Markdown("### πŸ“˜ Enter your coursebook text below:")
course_text = gr.Textbox(lines=6, label="Coursebook Text")
generate_btn = gr.Button("🎀 Generate Question and Speak")
question_output = gr.Textbox(label="Generated Question")
audio_output = gr.Audio(label="Question Audio", type="filepath")
user_audio = gr.Audio(label="Your Answer", type="filepath", sources=["microphone"])
transcribe_btn = gr.Button("πŸ“ Transcribe Answer")
transcription_output = gr.Textbox(label="Transcribed Answer")
generate_btn.click(fn=generate_question, inputs=course_text, outputs=[question_output, audio_output])
transcribe_btn.click(fn=transcribe_audio, inputs=user_audio, outputs=transcription_output)
app.launch()