ciaochris commited on
Commit
0bafada
·
verified ·
1 Parent(s): 1a524f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -2
app.py CHANGED
@@ -119,6 +119,8 @@ def generate_tutor_output(subject: str, difficulty: str, student_input: str) ->
119
  """
120
 
121
  try:
 
 
122
  # Make sure we're using the correct model and parameters
123
  completion = client.chat.completions.create(
124
  messages=[
@@ -137,6 +139,7 @@ def generate_tutor_output(subject: str, difficulty: str, student_input: str) ->
137
 
138
  # Handle the response parsing more robustly
139
  response_content = completion.choices[0].message.content
 
140
 
141
  # Improved JSON parsing with better error handling
142
  try:
@@ -190,6 +193,9 @@ def generate_tutor_output(subject: str, difficulty: str, student_input: str) ->
190
 
191
  def process_output(output: Dict[str, Any]) -> Tuple[str, str, str, str]:
192
  try:
 
 
 
193
  # Use markdown2 to convert markdown to HTML, with fallbacks for missing content
194
  lesson = markdown2.markdown(str(output.get("lesson", "No lesson available")))
195
  example = markdown2.markdown(str(output.get("example", "No example available")))
@@ -265,23 +271,35 @@ def create_interface() -> gr.Blocks:
265
 
266
  def process_input(subject, difficulty, text_input, audio_input):
267
  try:
 
 
 
 
268
  # Prioritize text input if both are provided
269
  if text_input and text_input.strip():
270
  student_input = text_input
271
  transcribed_text = "Using text input instead of audio."
 
272
  elif audio_input:
273
  transcribed_text = transcribe_audio(audio_input)
274
  student_input = transcribed_text
 
275
  else:
 
276
  return "No input provided. Please type a question or record audio.", "Please provide a question to begin.", "", "", ""
277
 
278
- logging.info(f"Processing input: subject={subject}, difficulty={difficulty}, student_input={student_input}")
279
-
280
  if not student_input or student_input.strip() == "":
 
281
  return "Input was empty or could not be processed.", "Please provide a valid question.", "", "", ""
282
 
 
283
  tutor_output = generate_tutor_output(subject, difficulty, student_input)
 
 
 
284
  lesson, example, real_world, quiz = process_output(tutor_output)
 
 
285
  return transcribed_text, lesson, example, real_world, quiz
286
  except Exception as e:
287
  logging.error(f"Error in process_input: {str(e)}")
@@ -291,12 +309,14 @@ def create_interface() -> gr.Blocks:
291
  def clear_outputs():
292
  return "", "", "", "", ""
293
 
 
294
  submit_button.click(
295
  fn=process_input,
296
  inputs=[subject, difficulty, student_input, audio_input],
297
  outputs=[transcription_output, lesson_output, example_output, real_world_output, quiz_output]
298
  )
299
 
 
300
  clear_button.click(
301
  fn=clear_outputs,
302
  inputs=[],
 
119
  """
120
 
121
  try:
122
+ logging.info(f"Sending prompt for subject: {subject}, difficulty: {difficulty}, student input: {student_input}")
123
+
124
  # Make sure we're using the correct model and parameters
125
  completion = client.chat.completions.create(
126
  messages=[
 
139
 
140
  # Handle the response parsing more robustly
141
  response_content = completion.choices[0].message.content
142
+ logging.info(f"Received response: {response_content[:100]}...") # Log first 100 chars
143
 
144
  # Improved JSON parsing with better error handling
145
  try:
 
193
 
194
  def process_output(output: Dict[str, Any]) -> Tuple[str, str, str, str]:
195
  try:
196
+ # Log the output structure for debugging
197
+ logging.info(f"Processing output: {str(output)[:100]}...")
198
+
199
  # Use markdown2 to convert markdown to HTML, with fallbacks for missing content
200
  lesson = markdown2.markdown(str(output.get("lesson", "No lesson available")))
201
  example = markdown2.markdown(str(output.get("example", "No example available")))
 
271
 
272
  def process_input(subject, difficulty, text_input, audio_input):
273
  try:
274
+ # Add debug logging
275
+ logging.info(f"Received inputs - subject: {subject}, difficulty: {difficulty}")
276
+ logging.info(f"Text input: '{text_input}', Audio input: {audio_input}")
277
+
278
  # Prioritize text input if both are provided
279
  if text_input and text_input.strip():
280
  student_input = text_input
281
  transcribed_text = "Using text input instead of audio."
282
+ logging.info(f"Using text input: {student_input}")
283
  elif audio_input:
284
  transcribed_text = transcribe_audio(audio_input)
285
  student_input = transcribed_text
286
+ logging.info(f"Using transcribed audio: {student_input}")
287
  else:
288
+ logging.warning("No input provided")
289
  return "No input provided. Please type a question or record audio.", "Please provide a question to begin.", "", "", ""
290
 
 
 
291
  if not student_input or student_input.strip() == "":
292
+ logging.warning("Input was empty after processing")
293
  return "Input was empty or could not be processed.", "Please provide a valid question.", "", "", ""
294
 
295
+ # Generate tutor output
296
  tutor_output = generate_tutor_output(subject, difficulty, student_input)
297
+ logging.info("Successfully generated tutor output")
298
+
299
+ # Process the output
300
  lesson, example, real_world, quiz = process_output(tutor_output)
301
+ logging.info("Successfully processed output into formatted HTML")
302
+
303
  return transcribed_text, lesson, example, real_world, quiz
304
  except Exception as e:
305
  logging.error(f"Error in process_input: {str(e)}")
 
309
  def clear_outputs():
310
  return "", "", "", "", ""
311
 
312
+ # Connect the submit button to process_input function
313
  submit_button.click(
314
  fn=process_input,
315
  inputs=[subject, difficulty, student_input, audio_input],
316
  outputs=[transcription_output, lesson_output, example_output, real_world_output, quiz_output]
317
  )
318
 
319
+ # Connect the clear button to clear_outputs function
320
  clear_button.click(
321
  fn=clear_outputs,
322
  inputs=[],