Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,43 +12,64 @@ qg_pipeline = pipeline("text2text-generation", model="valhalla/t5-small-e2e-qg")
|
|
12 |
# Load TTS model
|
13 |
tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False, gpu=False)
|
14 |
|
|
|
|
|
|
|
15 |
def generate_question(text):
|
|
|
|
|
|
|
|
|
|
|
16 |
# Prompt for question generation
|
17 |
input_text = f"generate question: {text.strip()}"
|
18 |
-
|
19 |
|
20 |
-
#
|
|
|
|
|
|
|
21 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as fp:
|
22 |
tts.tts_to_file(text=question, file_path=fp.name)
|
23 |
audio_path = fp.name
|
24 |
|
25 |
return question, audio_path
|
26 |
|
27 |
-
def
|
|
|
28 |
recognizer = sr.Recognizer()
|
29 |
with sr.AudioFile(audio_path) as source:
|
30 |
audio_data = recognizer.record(source)
|
31 |
try:
|
32 |
-
|
33 |
except sr.UnknownValueError:
|
34 |
-
return "Sorry, I could not understand your answer."
|
35 |
except sr.RequestError:
|
36 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
with gr.Blocks() as app:
|
39 |
-
gr.Markdown("###
|
40 |
|
41 |
-
course_text = gr.Textbox(lines=6, label="Coursebook Text")
|
42 |
|
43 |
-
generate_btn = gr.Button("
|
44 |
-
question_output = gr.Textbox(label="Generated Question")
|
45 |
-
audio_output = gr.Audio(label="Question Audio", type="filepath")
|
46 |
|
47 |
-
user_audio = gr.Audio(label="Your Answer", type="filepath", sources=["microphone"])
|
48 |
transcribe_btn = gr.Button("π Transcribe Answer")
|
49 |
-
transcription_output = gr.Textbox(label="Transcribed Answer")
|
|
|
50 |
|
51 |
generate_btn.click(fn=generate_question, inputs=course_text, outputs=[question_output, audio_output])
|
52 |
-
transcribe_btn.click(fn=
|
53 |
|
54 |
app.launch()
|
|
|
12 |
# Load TTS model
|
13 |
tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False, gpu=False)
|
14 |
|
15 |
+
# Global storage
|
16 |
+
last_answer = ""
|
17 |
+
|
18 |
def generate_question(text):
|
19 |
+
global last_answer
|
20 |
+
|
21 |
+
# Extract a possible answer from the text (you can improve this logic)
|
22 |
+
last_answer = "They are Aladdin lamps" # You can auto-extract later
|
23 |
+
|
24 |
# Prompt for question generation
|
25 |
input_text = f"generate question: {text.strip()}"
|
26 |
+
generated = qg_pipeline(input_text, max_length=64)[0]["generated_text"]
|
27 |
|
28 |
+
# Keep only the first question
|
29 |
+
question = generated.split("<sep>")[0].strip()
|
30 |
+
|
31 |
+
# Generate audio
|
32 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as fp:
|
33 |
tts.tts_to_file(text=question, file_path=fp.name)
|
34 |
audio_path = fp.name
|
35 |
|
36 |
return question, audio_path
|
37 |
|
38 |
+
def transcribe_and_check(audio_path):
|
39 |
+
global last_answer
|
40 |
recognizer = sr.Recognizer()
|
41 |
with sr.AudioFile(audio_path) as source:
|
42 |
audio_data = recognizer.record(source)
|
43 |
try:
|
44 |
+
response = recognizer.recognize_google(audio_data)
|
45 |
except sr.UnknownValueError:
|
46 |
+
return "Sorry, I could not understand your answer.", ""
|
47 |
except sr.RequestError:
|
48 |
+
return "Speech recognition service error.", ""
|
49 |
+
|
50 |
+
# Check match (very basic β can be made smarter)
|
51 |
+
if last_answer.lower() in response.lower():
|
52 |
+
feedback = "βοΈ Correct!"
|
53 |
+
else:
|
54 |
+
feedback = "β Try again."
|
55 |
+
|
56 |
+
return response, feedback
|
57 |
|
58 |
with gr.Blocks() as app:
|
59 |
+
gr.Markdown("### π Interactive Coursebook Q&A")
|
60 |
|
61 |
+
course_text = gr.Textbox(lines=6, label="π Coursebook Text")
|
62 |
|
63 |
+
generate_btn = gr.Button("π Generate Question and Speak")
|
64 |
+
question_output = gr.Textbox(label="π§ Generated Question")
|
65 |
+
audio_output = gr.Audio(label="π Question Audio", type="filepath")
|
66 |
|
67 |
+
user_audio = gr.Audio(label="π€ Your Answer", type="filepath", sources=["microphone"])
|
68 |
transcribe_btn = gr.Button("π Transcribe Answer")
|
69 |
+
transcription_output = gr.Textbox(label="π£ Transcribed Answer")
|
70 |
+
feedback_output = gr.Textbox(label="π§ͺ Feedback")
|
71 |
|
72 |
generate_btn.click(fn=generate_question, inputs=course_text, outputs=[question_output, audio_output])
|
73 |
+
transcribe_btn.click(fn=transcribe_and_check, inputs=user_audio, outputs=[transcription_output, feedback_output])
|
74 |
|
75 |
app.launch()
|