Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -33,119 +33,62 @@ import tempfile
|
|
33 |
|
34 |
load_dotenv()
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
css = file.read()
|
39 |
|
40 |
# For maintaining user session (to keep track of userID)
|
41 |
user_state = gr.State(value="")
|
42 |
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
client = OpenAI()
|
46 |
image_path = "picturePerformance.jpg"
|
47 |
img_html = get_image_html(image_path)
|
48 |
|
49 |
-
# Create a thread pool executor
|
50 |
executor = ThreadPoolExecutor()
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
async def generate_feedback(user_id, question_choice, strategy_choice, message, feedback_level):
|
58 |
-
current_question_index = thinkingframes.questions.index(question_choice)
|
59 |
-
strategy, explanation = thinkingframes.strategy_options[strategy_choice]
|
60 |
|
61 |
conversation = [{
|
62 |
"role": "system",
|
63 |
-
"content":
|
64 |
-
f"directly guiding a Primary 6 student in Singapore in their oral responses. "
|
65 |
-
f"Format the feedback in Markdown so that it can be easily read. "
|
66 |
-
f"Address the student directly in the second person in your feedback. "
|
67 |
-
f"The student is answering the question: '{thinkingframes.questions[current_question_index]}'. "
|
68 |
-
f"For Question 1, consider the picture description: '{thinkingframes.description}'. "
|
69 |
-
f"For Questions 2 and 3, the picture is not relevant, so the student should not refer to it in their response. "
|
70 |
-
f"Analyze the student's response using the following step-by-step approach: "
|
71 |
-
f"1. Evaluate the response against the {strategy} thinking frame. "
|
72 |
-
f"2. Assess how well the student's response addresses each component of the {strategy} thinking frame: "
|
73 |
-
f" - Assign emoticon scores based on how well the student comprehensively covered each component: "
|
74 |
-
f" - 😊😊😊 (three smiling faces) for a good coverage "
|
75 |
-
f" - 😊😊 (two smiling faces) for an average coverage "
|
76 |
-
f" - 😊 (one smiling face) for a poor coverage "
|
77 |
-
f" - Provide a clear, direct, and concise explanation of how well the answer addresses each component. "
|
78 |
-
f" - Identify specific areas for improvement in students responses, and provide targeted suggestions for improvement. "
|
79 |
-
f"3. Identify overall strengths and areas for improvement in the student's response using the {strategy} to format and provide targeted areas for improvement. "
|
80 |
-
f"4. Provide specific feedback on grammar, vocabulary, and sentence structure. "
|
81 |
-
f" Suggest age-appropriate enhancements that are one level higher than the student's current response. "
|
82 |
-
f"5. Conclude with follow-up questions for reflection. "
|
83 |
-
f"If the student's response deviates from the question, provide clear and concise feedback to help them refocus and try again. "
|
84 |
-
f"Ensure that the vocabulary and sentence structure recommendations are achievable for Primary 6 students in Singapore. "
|
85 |
-
f"Example Feedback Structure for Each Component: "
|
86 |
-
f"Component: [Component Name] "
|
87 |
-
f"Score: [Smiling emoticons] "
|
88 |
-
f"Explanation: [Clear, direct, and concise explanation of how well the answer addresses the component. Identify specific areas for improvement, and provide targeted suggestions for improvement.] "
|
89 |
-
f"{thinkingframes.generate_prompt(feedback_level)}"
|
90 |
}, {
|
91 |
"role": "user",
|
92 |
"content": message
|
93 |
}]
|
94 |
|
95 |
-
|
96 |
-
model='gpt-4o-2024-05-13',
|
97 |
-
messages=conversation,
|
98 |
-
temperature=0.6,
|
99 |
-
max_tokens=1000,
|
100 |
-
stream=True
|
101 |
-
)
|
102 |
-
|
103 |
-
chat_history = [] # Initialize chat history outside the loop
|
104 |
-
full_feedback = "" # Accumulate the entire feedback message
|
105 |
-
try:
|
106 |
-
for chunk in response:
|
107 |
-
if chunk.choices[0].delta and chunk.choices[0].delta.content:
|
108 |
-
feedback_chunk = chunk.choices[0].delta.content
|
109 |
-
full_feedback += feedback_chunk # Accumulate the feedback
|
110 |
-
await asyncio.sleep(0)
|
111 |
-
|
112 |
-
# Append the complete feedback to the chat history
|
113 |
-
chat_history.append(("Oral Coach ⚡ ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", full_feedback))
|
114 |
-
yield chat_history # Yield the chat history only once
|
115 |
-
|
116 |
-
except Exception as e:
|
117 |
-
logging.error(f"An error occurred during feedback generation: {str(e)}")
|
118 |
|
119 |
questionNo = current_question_index + 1
|
120 |
-
|
121 |
-
add_submission(user_id, message, full_feedback, int(0), "", questionNo)
|
122 |
-
|
123 |
-
async def generate_audio_feedback(feedback_buffer):
|
124 |
-
try:
|
125 |
-
response = client.audio.speech.create(
|
126 |
-
model="tts-1",
|
127 |
-
voice="alloy",
|
128 |
-
input=feedback_buffer,
|
129 |
-
response_format="wav"
|
130 |
-
)
|
131 |
|
132 |
-
|
133 |
-
sample_rate = 24000 # Default sample rate for OpenAI's WAV output
|
134 |
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
|
|
|
|
140 |
|
141 |
async def predict(question_choice, strategy_choice, feedback_level, audio):
|
142 |
-
current_audio_output = None
|
143 |
-
final_feedback = "" # Store only the assistant's feedback
|
144 |
|
145 |
if audio is None:
|
146 |
yield [("Oral Coach ⚡ ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", "No audio data received. Please try again.")], current_audio_output
|
147 |
return
|
148 |
-
|
149 |
sample_rate, audio_data = audio
|
150 |
|
151 |
if audio_data is None or len(audio_data) == 0:
|
@@ -161,58 +104,60 @@ async def predict(question_choice, strategy_choice, feedback_level, audio):
|
|
161 |
yield chat_history, current_audio_output
|
162 |
|
163 |
try:
|
164 |
-
transcription_future = executor.submit(
|
165 |
student_response = await asyncio.wrap_future(transcription_future)
|
166 |
|
167 |
if not student_response.strip():
|
168 |
yield [("Oral Coach ⚡ ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", "Transcription failed. Please try again or seek assistance.")], current_audio_output
|
169 |
return
|
170 |
|
171 |
-
chat_history.append(("Student", student_response))
|
172 |
yield chat_history, current_audio_output
|
173 |
|
174 |
chat_history.append(("Oral Coach ⚡ ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", "Transcription complete. Generating feedback. Please continue listening to your oral response while waiting ..."))
|
175 |
yield chat_history, current_audio_output
|
176 |
|
177 |
-
|
178 |
-
|
179 |
-
if flagged:
|
180 |
-
moderated_message = "The message has been flagged. Please see your teacher to clarify."
|
181 |
-
questionNo = thinkingframes.questions.index(question_choice) + 1
|
182 |
-
add_submission(int(user_state.value), moderated_message, "", int(0), "", questionNo)
|
183 |
-
yield chat_history, current_audio_output
|
184 |
-
return
|
185 |
|
186 |
-
|
187 |
-
|
188 |
-
chat_history.extend(chat_update)
|
189 |
-
final_feedback = chat_history[-1][1] # Update final_feedback with the latest chunk
|
190 |
-
yield chat_history, current_audio_output # Yield audio output
|
191 |
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
|
|
196 |
|
197 |
except Exception as e:
|
198 |
logging.error(f"An error occurred: {str(e)}", exc_info=True)
|
199 |
yield [("Oral Coach ⚡ ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", "An error occurred. Please try again or seek assistance.")], current_audio_output
|
200 |
|
201 |
-
|
202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
gr.Markdown("## Student Information")
|
204 |
class_name = gr.Dropdown(label="Class", choices=CLASS_OPTIONS)
|
205 |
index_no = gr.Dropdown(label="Index No", choices=[f"{i:02}" for i in range(1, 46)])
|
206 |
|
207 |
policy_text = gr.Markdown(user_acceptance_policy)
|
208 |
-
policy_checkbox = gr.Checkbox(label="I have read and agree to the Things to Note When using the Oral Coach
|
209 |
|
210 |
submit_info_btn = gr.Button("Submit Info")
|
211 |
info_output = gr.Text()
|
212 |
-
|
213 |
with gr.Column(visible=False) as oral_coach_content:
|
214 |
-
gr.Markdown("##
|
215 |
-
gr.Markdown(img_html)
|
216 |
with gr.Row():
|
217 |
with gr.Column(scale=1):
|
218 |
gr.Markdown("### Step 1: Choose a Question")
|
@@ -229,31 +174,14 @@ with gr.Blocks(title="Oral Coach ⚡ ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", theme=t
|
|
229 |
submit_answer_btn = gr.Button("Submit Oral Response")
|
230 |
|
231 |
gr.Markdown("### Step 5: Review your personalised feedback")
|
232 |
-
feedback_output = gr.Chatbot(
|
233 |
-
label="Feedback",
|
234 |
-
scale=4,
|
235 |
-
height=700,
|
236 |
-
show_label=True
|
237 |
-
)
|
238 |
-
#audio
|
239 |
-
#submit_answer_here
|
240 |
audio_output = gr.Audio(type="numpy", label="Audio Playback", format="wav", autoplay="True")
|
241 |
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
)
|
248 |
-
|
249 |
-
def toggle_oral_coach_visibility(class_name, index_no, policy_checked):
|
250 |
-
if not policy_checked:
|
251 |
-
return "Please agree to the Things to Note When using the Oral Coach ⚡ ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡ before submitting.", gr.update(visible=False)
|
252 |
-
validation_passed, message, userid = collect_student_info(class_name, index_no)
|
253 |
-
if not validation_passed:
|
254 |
-
return message, gr.update(visible=False)
|
255 |
-
user_state.value = userid
|
256 |
-
return message, gr.update(visible=True)
|
257 |
|
258 |
submit_info_btn.click(
|
259 |
toggle_oral_coach_visibility,
|
@@ -261,7 +189,6 @@ with gr.Blocks(title="Oral Coach ⚡ ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", theme=t
|
|
261 |
outputs=[info_output, oral_coach_content]
|
262 |
)
|
263 |
|
264 |
-
# Define other tabs like Teacher's Dashboard
|
265 |
create_teachers_dashboard_tab()
|
266 |
|
267 |
app.queue(max_size=20).launch(
|
|
|
33 |
|
34 |
load_dotenv()
|
35 |
|
36 |
+
default_lang = "en"
|
37 |
+
engines = {default_lang: Model(default_lang)}
|
|
|
38 |
|
39 |
# For maintaining user session (to keep track of userID)
|
40 |
user_state = gr.State(value="")
|
41 |
|
42 |
+
@spaces.GPU(duration=120)
|
43 |
+
def transcribe(audio):
|
44 |
+
lang = "en"
|
45 |
+
model = engines[lang]
|
46 |
+
text = model.stt_file(audio)[0]
|
47 |
+
return text
|
48 |
+
|
49 |
+
# Load the Meta-Llama-3-8B model from Hugging Face
|
50 |
+
llm = pipeline("text-generation", model="models/meta-llama/Meta-Llama-3-8B")
|
51 |
|
|
|
52 |
image_path = "picturePerformance.jpg"
|
53 |
img_html = get_image_html(image_path)
|
54 |
|
|
|
55 |
executor = ThreadPoolExecutor()
|
56 |
|
57 |
+
@spaces.GPU(duration=120)
|
58 |
+
def generate_feedback(user_id, question_choice, strategy_choice, message, feedback_level):
|
59 |
+
current_question_index = questions.index(question_choice)
|
60 |
+
strategy, explanation = strategy_options[strategy_choice]
|
|
|
|
|
|
|
|
|
61 |
|
62 |
conversation = [{
|
63 |
"role": "system",
|
64 |
+
"content": thinkingframes.generate_system_message(current_question_index, feedback_level)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
}, {
|
66 |
"role": "user",
|
67 |
"content": message
|
68 |
}]
|
69 |
|
70 |
+
feedback = llm(conversation, max_length=1000, num_return_sequences=1)[0]["generated_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
questionNo = current_question_index + 1
|
73 |
+
add_submission(user_id, message, feedback, int(0), "", questionNo)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
return feedback
|
|
|
76 |
|
77 |
+
@spaces.GPU(duration=60)
|
78 |
+
async def generate_audio_feedback(feedback_buffer):
|
79 |
+
communicate = edge_tts.Communicate(feedback_buffer)
|
80 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
81 |
+
tmp_path = tmp_file.name
|
82 |
+
await communicate.save(tmp_path)
|
83 |
+
return tmp_path
|
84 |
|
85 |
async def predict(question_choice, strategy_choice, feedback_level, audio):
|
86 |
+
current_audio_output = None
|
|
|
87 |
|
88 |
if audio is None:
|
89 |
yield [("Oral Coach ⚡ ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", "No audio data received. Please try again.")], current_audio_output
|
90 |
return
|
91 |
+
|
92 |
sample_rate, audio_data = audio
|
93 |
|
94 |
if audio_data is None or len(audio_data) == 0:
|
|
|
104 |
yield chat_history, current_audio_output
|
105 |
|
106 |
try:
|
107 |
+
transcription_future = executor.submit(transcribe, audio_path)
|
108 |
student_response = await asyncio.wrap_future(transcription_future)
|
109 |
|
110 |
if not student_response.strip():
|
111 |
yield [("Oral Coach ⚡ ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", "Transcription failed. Please try again or seek assistance.")], current_audio_output
|
112 |
return
|
113 |
|
114 |
+
chat_history.append(("Student", student_response))
|
115 |
yield chat_history, current_audio_output
|
116 |
|
117 |
chat_history.append(("Oral Coach ⚡ ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", "Transcription complete. Generating feedback. Please continue listening to your oral response while waiting ..."))
|
118 |
yield chat_history, current_audio_output
|
119 |
|
120 |
+
feedback_future = executor.submit(generate_feedback, int(user_state.value), question_choice, strategy_choice, student_response, feedback_level)
|
121 |
+
feedback = await asyncio.wrap_future(feedback_future)
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
+
chat_history.append(("Oral Coach ⚡ ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", feedback))
|
124 |
+
yield chat_history, current_audio_output
|
|
|
|
|
|
|
125 |
|
126 |
+
audio_future = executor.submit(generate_audio_feedback, feedback)
|
127 |
+
audio_output_path = await asyncio.wrap_future(audio_future)
|
128 |
+
|
129 |
+
current_audio_output = (24000, audio_output_path)
|
130 |
+
yield chat_history, current_audio_output
|
131 |
|
132 |
except Exception as e:
|
133 |
logging.error(f"An error occurred: {str(e)}", exc_info=True)
|
134 |
yield [("Oral Coach ⚡ ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", "An error occurred. Please try again or seek assistance.")], current_audio_output
|
135 |
|
136 |
+
# Modify the toggle_oral_coach_visibility function to call add_user_privacy and store the returned user_id in user_state.value
|
137 |
+
def toggle_oral_coach_visibility(class_name, index_no, policy_checked):
|
138 |
+
if not policy_checked:
|
139 |
+
return "Please agree to the Things to Note When using the Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡ before submitting.", gr.update(visible=False)
|
140 |
+
user_id, message = add_user_privacy(class_name, index_no)
|
141 |
+
if "Error" in message:
|
142 |
+
return message, gr.update(visible=False)
|
143 |
+
user_state.value = user_id
|
144 |
+
return message, gr.update(visible=True)
|
145 |
+
|
146 |
+
with gr.Blocks(title="Oral Coach powered by Hugging Face", theme=theme) as app:
|
147 |
+
with gr.Tab("Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡"):
|
148 |
gr.Markdown("## Student Information")
|
149 |
class_name = gr.Dropdown(label="Class", choices=CLASS_OPTIONS)
|
150 |
index_no = gr.Dropdown(label="Index No", choices=[f"{i:02}" for i in range(1, 46)])
|
151 |
|
152 |
policy_text = gr.Markdown(user_acceptance_policy)
|
153 |
+
policy_checkbox = gr.Checkbox(label="I have read and agree to the Things to Note When using the Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", value=False)
|
154 |
|
155 |
submit_info_btn = gr.Button("Submit Info")
|
156 |
info_output = gr.Text()
|
157 |
+
|
158 |
with gr.Column(visible=False) as oral_coach_content:
|
159 |
+
gr.Markdown("## Powered by Hugging Face")
|
160 |
+
gr.Markdown(img_html)
|
161 |
with gr.Row():
|
162 |
with gr.Column(scale=1):
|
163 |
gr.Markdown("### Step 1: Choose a Question")
|
|
|
174 |
submit_answer_btn = gr.Button("Submit Oral Response")
|
175 |
|
176 |
gr.Markdown("### Step 5: Review your personalised feedback")
|
177 |
+
feedback_output = gr.Chatbot(label="Feedback", scale=4, height=700, show_label=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
audio_output = gr.Audio(type="numpy", label="Audio Playback", format="wav", autoplay="True")
|
179 |
|
180 |
+
submit_answer_btn.click(
|
181 |
+
predict,
|
182 |
+
inputs=[question_choice, strategy_choice, feedback_level, audio_input],
|
183 |
+
outputs=[feedback_output, audio_output]
|
184 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
|
186 |
submit_info_btn.click(
|
187 |
toggle_oral_coach_visibility,
|
|
|
189 |
outputs=[info_output, oral_coach_content]
|
190 |
)
|
191 |
|
|
|
192 |
create_teachers_dashboard_tab()
|
193 |
|
194 |
app.queue(max_size=20).launch(
|