alibicer commited on
Commit
12b9be3
Β·
verified Β·
1 Parent(s): bbd8f0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -52
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import os
3
  import gradio as gr
4
  from dotenv import load_dotenv
@@ -12,75 +11,50 @@ if os.path.exists(".env"):
12
 
13
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
14
 
15
- # βœ… Ensure API Key is available
16
  if not OPENAI_API_KEY:
17
- raise ValueError("🚨 OpenAI API key is missing! Set it in the .env file.")
18
 
19
  client = OpenAI(api_key=OPENAI_API_KEY)
20
 
21
- # βœ… Chatbot Response Function with Full Debugging
22
- def respond(user_message, history, selected_method):
23
  if not user_message:
24
- return "❌ No input received.", history, selected_method
25
-
26
- user_message = user_message.strip().lower() # Normalize input
27
-
28
- valid_methods = ["bar model", "double number line", "equation"]
29
-
30
- # βœ… Ensure history is a list of tuples
31
- if not isinstance(history, list):
32
- history = []
33
- history = [(str(h[0]), str(h[1])) for h in history if isinstance(h, tuple) and len(h) == 2]
34
-
35
- # βœ… Debug Logs
36
- print("\nDEBUG: Incoming User Message:", user_message)
37
- print("DEBUG: Current History:", history)
38
- print("DEBUG: Selected Method Before Processing:", selected_method)
39
-
40
- # βœ… If user selects a method, store it and provide the method-specific prompt
41
- if user_message in valid_methods:
42
- selected_method = user_message # Store the method
43
- method_prompt = get_prompt_for_method(user_message)
44
- history.append((user_message, method_prompt)) # Store correctly formatted tuple
45
-
46
- print("DEBUG: Method Selected:", selected_method)
47
- print("DEBUG: Sending Prompt for Method:", method_prompt)
48
-
49
- return method_prompt, history, selected_method
50
-
51
- # βœ… If a method has already been selected, provide feedback
52
- if selected_method:
53
- feedback = get_feedback_for_method(selected_method, user_message)
54
- history.append((user_message, feedback)) # Store correctly formatted tuple
55
-
56
- print("DEBUG: Feedback Given:", feedback)
57
- print("DEBUG: Updated History:", history)
58
-
59
- return feedback, history, selected_method
60
 
61
- # βœ… Ensure chatbot always responds with a valid tuple
62
- error_msg = "❌ Please select a method first (Bar Model, Double Number Line, or Equation)."
63
- history.append((user_message, error_msg)) # Store correctly formatted tuple
 
 
64
 
65
- print("DEBUG: Error Triggered, No Method Selected")
 
 
 
 
 
66
 
67
- return error_msg, history, selected_method
 
68
 
69
  # βœ… Gradio UI Setup
70
  with gr.Blocks() as demo:
71
  gr.Markdown("## πŸ€– AI-Guided Math PD Chatbot")
72
 
73
- chatbot = gr.Chatbot(value=[(INITIAL_PROMPT, "Hello! Please select a method to begin.")], height=500)
74
- state_history = gr.State([(INITIAL_PROMPT, "Hello! Please select a method to begin.")])
75
- state_selected_method = gr.State(None) # βœ… New state to track selected method
76
 
77
  user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")
78
 
79
- # βœ… Handling user input and response logic
80
  user_input.submit(
81
  respond,
82
- inputs=[user_input, state_history, state_selected_method],
83
- outputs=[chatbot, state_history, state_selected_method]
 
 
 
 
84
  )
85
 
86
  if __name__ == "__main__":
 
 
1
  import os
2
  import gradio as gr
3
  from dotenv import load_dotenv
 
11
 
12
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
13
 
14
+ # βœ… Ensure API Key is available, otherwise show an error
15
  if not OPENAI_API_KEY:
16
+ raise ValueError("🚨 OpenAI API key is missing! Set it in the .env file or hardcode it in app.py.")
17
 
18
  client = OpenAI(api_key=OPENAI_API_KEY)
19
 
20
+ # βœ… Chatbot Response Function
21
+ def respond(user_message, history):
22
  if not user_message:
23
+ return "", history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
+ # βœ… Handle method selection
26
+ if user_message.lower() in ["bar model", "double number line", "equation"]:
27
+ response = get_prompt_for_method(user_message)
28
+ history.append((user_message, response))
29
+ return "", history
30
 
31
+ # βœ… Handle teacher response to method
32
+ last_method = history[-1][0] if history else None
33
+ if last_method and last_method.lower() in ["bar model", "double number line", "equation"]:
34
+ response = get_feedback_for_method(last_method, user_message)
35
+ history.append((user_message, response))
36
+ return "", history
37
 
38
+ # βœ… Default response if input is unclear
39
+ return "I didn’t understand that. Please select a method first (Bar Model, Double Number Line, or Equation).", history
40
 
41
  # βœ… Gradio UI Setup
42
  with gr.Blocks() as demo:
43
  gr.Markdown("## πŸ€– AI-Guided Math PD Chatbot")
44
 
45
+ chatbot = gr.Chatbot(value=[(INITIAL_PROMPT, "")], height=500) # βœ… Ensures AI starts with INITIAL_PROMPT
46
+ state_history = gr.State([(INITIAL_PROMPT, "")]) # βœ… Sets initial prompt as the first message
 
47
 
48
  user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")
49
 
 
50
  user_input.submit(
51
  respond,
52
+ inputs=[user_input, state_history],
53
+ outputs=[user_input, chatbot]
54
+ ).then(
55
+ fn=lambda _, h: h,
56
+ inputs=[user_input, chatbot],
57
+ outputs=[state_history]
58
  )
59
 
60
  if __name__ == "__main__":