amyakir's picture
Update app.py
be65152 verified
raw
history blame
2.63 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)
# Global storage
last_answer = ""
def generate_question(text):
global last_answer
# Extract a possible answer from the text (you can improve this logic)
last_answer = "They are Aladdin lamps" # You can auto-extract later
# Prompt for question generation
input_text = f"generate question: {text.strip()}"
generated = qg_pipeline(input_text, max_length=64)[0]["generated_text"]
# Keep only the first question
question = generated.split("<sep>")[0].strip()
# Generate 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_and_check(audio_path):
global last_answer
recognizer = sr.Recognizer()
with sr.AudioFile(audio_path) as source:
audio_data = recognizer.record(source)
try:
response = recognizer.recognize_google(audio_data)
except sr.UnknownValueError:
return "Sorry, I could not understand your answer.", ""
except sr.RequestError:
return "Speech recognition service error.", ""
# Check match (very basic – can be made smarter)
if last_answer.lower() in response.lower():
feedback = "βœ”οΈ Correct!"
else:
feedback = "❌ Try again."
return response, feedback
with gr.Blocks() as app:
gr.Markdown("### πŸŽ“ Interactive Coursebook Q&A")
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")
feedback_output = gr.Textbox(label="πŸ§ͺ Feedback")
generate_btn.click(fn=generate_question, inputs=course_text, outputs=[question_output, audio_output])
transcribe_btn.click(fn=transcribe_and_check, inputs=user_audio, outputs=[transcription_output, feedback_output])
app.launch()