Spaces:
Sleeping
Sleeping
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() |