File size: 2,009 Bytes
19f74b2
548a255
 
4f8a704
548a255
 
4f8a704
548a255
4f8a704
548a255
 
 
4f8a704
548a255
 
4f8a704
 
 
 
 
548a255
4f8a704
548a255
 
4f8a704
 
 
 
 
 
 
 
 
 
 
 
548a255
4f8a704
 
548a255
4f8a704
548a255
4f8a704
 
 
e34be93
4f8a704
 
 
e34be93
4f8a704
 
e34be93
4f8a704
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
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()