Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,18 +11,20 @@ from difflib import SequenceMatcher
|
|
11 |
qg_pipeline = pipeline("text2text-generation", model="valhalla/t5-small-e2e-qg")
|
12 |
tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False, gpu=False)
|
13 |
|
14 |
-
#
|
15 |
def extract_answer(question, context):
|
16 |
for line in context.split("\n"):
|
17 |
if any(word.lower() in line.lower() for word in question.split()[:3]):
|
18 |
return line
|
19 |
return ""
|
20 |
|
|
|
21 |
def generate_questions(text):
|
22 |
output = qg_pipeline(f"generate questions: {text}", num_return_sequences=3)
|
23 |
questions = [q["generated_text"] for q in output]
|
24 |
-
return (questions, text, 0) #
|
25 |
|
|
|
26 |
def ask_question(state):
|
27 |
questions, context, idx = state
|
28 |
if idx >= len(questions):
|
@@ -35,10 +37,11 @@ def ask_question(state):
|
|
35 |
|
36 |
return question, audio_path, (questions, context, idx + 1)
|
37 |
|
|
|
38 |
def transcribe_and_feedback(audio_path, state):
|
39 |
questions, context, idx = state
|
40 |
if idx == 0 or idx > len(questions):
|
41 |
-
return "Please ask a question first.", state
|
42 |
|
43 |
recognizer = sr.Recognizer()
|
44 |
with sr.AudioFile(audio_path) as source:
|
@@ -49,10 +52,9 @@ def transcribe_and_feedback(audio_path, state):
|
|
49 |
return "β Could not understand your answer.", state
|
50 |
|
51 |
# Compare with expected answer
|
52 |
-
question = questions[idx - 1]
|
53 |
expected = extract_answer(question, context)
|
54 |
ratio = SequenceMatcher(None, user_answer.lower(), expected.lower()).ratio()
|
55 |
-
|
56 |
if ratio > 0.6:
|
57 |
feedback = f"β
Good answer: {user_answer}"
|
58 |
else:
|
@@ -60,20 +62,21 @@ def transcribe_and_feedback(audio_path, state):
|
|
60 |
|
61 |
return feedback, (questions, context, idx)
|
62 |
|
|
|
63 |
with gr.Blocks() as app:
|
64 |
-
gr.Markdown("
|
65 |
|
66 |
with gr.Row():
|
67 |
course_text = gr.Textbox(lines=8, label="π Paste Coursebook Text")
|
68 |
gen_btn = gr.Button("π Generate Questions")
|
69 |
|
70 |
-
question_text = gr.Textbox(label="
|
71 |
question_audio = gr.Audio(label="π Listen to Question", type="filepath")
|
72 |
ask_btn = gr.Button("βΆοΈ Ask Next Question")
|
73 |
|
74 |
-
user_audio = gr.Audio(label="
|
75 |
transcribe_btn = gr.Button("π Submit Answer")
|
76 |
-
feedback_output = gr.Textbox(label="
|
77 |
|
78 |
conversation_state = gr.State()
|
79 |
|
|
|
11 |
qg_pipeline = pipeline("text2text-generation", model="valhalla/t5-small-e2e-qg")
|
12 |
tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False, gpu=False)
|
13 |
|
14 |
+
# Extract answer for comparison
|
15 |
def extract_answer(question, context):
|
16 |
for line in context.split("\n"):
|
17 |
if any(word.lower() in line.lower() for word in question.split()[:3]):
|
18 |
return line
|
19 |
return ""
|
20 |
|
21 |
+
# Generate questions from text
|
22 |
def generate_questions(text):
|
23 |
output = qg_pipeline(f"generate questions: {text}", num_return_sequences=3)
|
24 |
questions = [q["generated_text"] for q in output]
|
25 |
+
return (questions, text, 0) # This is stored in conversation_state
|
26 |
|
27 |
+
# Play the next question
|
28 |
def ask_question(state):
|
29 |
questions, context, idx = state
|
30 |
if idx >= len(questions):
|
|
|
37 |
|
38 |
return question, audio_path, (questions, context, idx + 1)
|
39 |
|
40 |
+
# Transcribe and provide feedback
|
41 |
def transcribe_and_feedback(audio_path, state):
|
42 |
questions, context, idx = state
|
43 |
if idx == 0 or idx > len(questions):
|
44 |
+
return "β Please ask a question first.", state
|
45 |
|
46 |
recognizer = sr.Recognizer()
|
47 |
with sr.AudioFile(audio_path) as source:
|
|
|
52 |
return "β Could not understand your answer.", state
|
53 |
|
54 |
# Compare with expected answer
|
55 |
+
question = questions[idx - 1]
|
56 |
expected = extract_answer(question, context)
|
57 |
ratio = SequenceMatcher(None, user_answer.lower(), expected.lower()).ratio()
|
|
|
58 |
if ratio > 0.6:
|
59 |
feedback = f"β
Good answer: {user_answer}"
|
60 |
else:
|
|
|
62 |
|
63 |
return feedback, (questions, context, idx)
|
64 |
|
65 |
+
# Gradio UI
|
66 |
with gr.Blocks() as app:
|
67 |
+
gr.Markdown("## π Interactive Speaking Practice")
|
68 |
|
69 |
with gr.Row():
|
70 |
course_text = gr.Textbox(lines=8, label="π Paste Coursebook Text")
|
71 |
gen_btn = gr.Button("π Generate Questions")
|
72 |
|
73 |
+
question_text = gr.Textbox(label="π€ Current Question")
|
74 |
question_audio = gr.Audio(label="π Listen to Question", type="filepath")
|
75 |
ask_btn = gr.Button("βΆοΈ Ask Next Question")
|
76 |
|
77 |
+
user_audio = gr.Audio(label="ποΈ Your Answer (Record)", sources="microphone", type="filepath")
|
78 |
transcribe_btn = gr.Button("π Submit Answer")
|
79 |
+
feedback_output = gr.Textbox(label="π¬ Feedback")
|
80 |
|
81 |
conversation_state = gr.State()
|
82 |
|