eddiebee commited on
Commit
ccf31d0
·
verified ·
1 Parent(s): 3ff4d20

fix: implement proper loading state for response generation

Browse files

- Replace custom JS with Gradio's native event handling
- Add status message component for loading feedback
- Split click event into two-step process using .then()
- Update tutor_interface to handle loading states
- Improve user feedback during response generation

Files changed (1) hide show
  1. app.py +18 -22
app.py CHANGED
@@ -1,16 +1,15 @@
1
  import gradio as gr
2
  import cohere
3
- import os
4
  from dotenv import load_dotenv
5
 
6
  # Load environment variables
7
  load_dotenv()
8
 
9
  # Initialize Cohere API client
10
- co = cohere.Client(os.getenv('COHERE_API_KEY'))
11
  MODEL_NAME = 'command-r-plus' # Define model name as a constant
12
 
13
- # Adaptive learning functions
14
  def assess_knowledge(name, experience, goals):
15
  try:
16
  level_prompt = f"User Name: {name}, Experience: {experience}, Goals: {goals}. Classify as beginner, intermediate, or advanced."
@@ -38,10 +37,11 @@ def generate_challenge(topic, level):
38
  except Exception as e:
39
  return "Error in generating challenge: " + str(e)
40
 
 
41
  def tutor_interface(name, experience, goals, topic, request_challenge=False):
42
  # Validate inputs
43
  if not all([name, experience, goals, topic]):
44
- return "Please fill in all required fields."
45
 
46
  # Generate adaptive content
47
  level = assess_knowledge(name, experience, goals)
@@ -49,9 +49,11 @@ def tutor_interface(name, experience, goals, topic, request_challenge=False):
49
 
50
  if request_challenge:
51
  challenge = generate_challenge(topic, level)
52
- return f"**Level:** {level}\n\n**Explanation:**\n{explanation}\n\n**Challenge:**\n{challenge}"
53
  else:
54
- return f"**Level:** {level}\n\n**Explanation:**\n{explanation}"
 
 
55
 
56
  # Gradio UI setup with theme and structured layout
57
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
@@ -78,29 +80,23 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
78
 
79
  request_challenge = gr.Checkbox(label="Include a practice challenge", value=False)
80
 
 
 
 
81
  # Output display with markdown formatting
82
  output = gr.Markdown(label="Tutor's Response")
83
 
84
- # Loading indicator
85
- with gr.Row():
86
- loading = gr.Markdown("Loading...", visible=False)
87
-
88
- # Submit button with loading feedback
89
  submit_button = gr.Button("Get Started", variant="primary")
 
 
90
  submit_button.click(
 
 
 
91
  fn=tutor_interface,
92
  inputs=[name, experience, goals, topic, request_challenge],
93
- outputs=[output],
94
- _js="""
95
- () => {
96
- document.querySelector('[id^=loading]').style.display = 'block';
97
- return null;
98
- }
99
- """
100
- ).then(
101
- fn=lambda: gr.update(visible=False),
102
- inputs=None,
103
- outputs=loading
104
  )
105
 
106
  # Launch the app
 
1
  import gradio as gr
2
  import cohere
 
3
  from dotenv import load_dotenv
4
 
5
  # Load environment variables
6
  load_dotenv()
7
 
8
  # Initialize Cohere API client
9
+ co = cohere.Client()
10
  MODEL_NAME = 'command-r-plus' # Define model name as a constant
11
 
12
+ # Adaptive learning functions remain the same
13
  def assess_knowledge(name, experience, goals):
14
  try:
15
  level_prompt = f"User Name: {name}, Experience: {experience}, Goals: {goals}. Classify as beginner, intermediate, or advanced."
 
37
  except Exception as e:
38
  return "Error in generating challenge: " + str(e)
39
 
40
+ # Modified tutor_interface to work with loading state
41
  def tutor_interface(name, experience, goals, topic, request_challenge=False):
42
  # Validate inputs
43
  if not all([name, experience, goals, topic]):
44
+ return "", "Please fill in all required fields."
45
 
46
  # Generate adaptive content
47
  level = assess_knowledge(name, experience, goals)
 
49
 
50
  if request_challenge:
51
  challenge = generate_challenge(topic, level)
52
+ response = f"**Level:** {level}\n\n**Explanation:**\n{explanation}\n\n**Challenge:**\n{challenge}"
53
  else:
54
+ response = f"**Level:** {level}\n\n**Explanation:**\n{explanation}"
55
+
56
+ return "Generation complete!", response
57
 
58
  # Gradio UI setup with theme and structured layout
59
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
 
80
 
81
  request_challenge = gr.Checkbox(label="Include a practice challenge", value=False)
82
 
83
+ # Status message for loading state
84
+ status = gr.Markdown("", elem_id="status")
85
+
86
  # Output display with markdown formatting
87
  output = gr.Markdown(label="Tutor's Response")
88
 
89
+ # Submit button
 
 
 
 
90
  submit_button = gr.Button("Get Started", variant="primary")
91
+
92
+ # Handle submission with loading state
93
  submit_button.click(
94
+ fn=lambda: ("Generating response...", ""), # Clear output and show loading message
95
+ outputs=[status, output],
96
+ ).then(
97
  fn=tutor_interface,
98
  inputs=[name, experience, goals, topic, request_challenge],
99
+ outputs=[status, output],
 
 
 
 
 
 
 
 
 
 
100
  )
101
 
102
  # Launch the app