alibicer commited on
Commit
a15c076
·
verified ·
1 Parent(s): 3c3240c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -42
app.py CHANGED
@@ -1,35 +1,26 @@
1
- import os
2
- import gradio as gr
3
- from dotenv import load_dotenv
4
- from openai import OpenAI
5
- from prompts.main_prompt import MAIN_PROMPT
6
- from prompts.initial_prompt import INITIAL_PROMPT
7
-
8
- # ✅ Load API key from .env file
9
- if os.path.exists(".env"):
10
- load_dotenv(".env")
11
-
12
- OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
13
-
14
- if not OPENAI_API_KEY:
15
- raise ValueError("🚨 OpenAI API key is missing! Set it in your .env file.")
16
-
17
- client = OpenAI(api_key=OPENAI_API_KEY)
18
-
19
  def respond(user_message, history):
20
  if not user_message:
21
  return "", history
22
 
23
  # ✅ Ensure proper message handling
24
  try:
25
- messages = [{"role": "system", "content": MAIN_PROMPT}]
26
-
27
  for user_text, assistant_text in history:
28
  if user_text:
29
  messages.append({"role": "user", "content": user_text})
30
  if assistant_text:
31
  messages.append({"role": "assistant", "content": assistant_text})
32
 
 
 
 
 
 
 
 
 
 
33
  messages.append({"role": "user", "content": user_message})
34
 
35
  # ✅ Get AI response
@@ -47,25 +38,3 @@ def respond(user_message, history):
47
 
48
  except Exception as e:
49
  return f"⚠️ An error occurred: {str(e)}", history
50
-
51
- # ✅ Gradio UI Setup
52
- with gr.Blocks() as demo:
53
- gr.Markdown("## 🤖 AI-Guided Math PD Chatbot")
54
-
55
- chatbot = gr.Chatbot(value=[("", INITIAL_PROMPT)], height=500)
56
- state_history = gr.State([("", INITIAL_PROMPT)])
57
-
58
- user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")
59
-
60
- user_input.submit(
61
- respond,
62
- inputs=[user_input, state_history],
63
- outputs=[user_input, chatbot]
64
- ).then(
65
- fn=lambda _, h: h,
66
- inputs=[user_input, chatbot],
67
- outputs=[state_history]
68
- )
69
-
70
- if __name__ == "__main__":
71
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  def respond(user_message, history):
2
  if not user_message:
3
  return "", history
4
 
5
  # ✅ Ensure proper message handling
6
  try:
7
+ messages = [{"role": "system", "content": INITIAL_PROMPT}]
8
+
9
  for user_text, assistant_text in history:
10
  if user_text:
11
  messages.append({"role": "user", "content": user_text})
12
  if assistant_text:
13
  messages.append({"role": "assistant", "content": assistant_text})
14
 
15
+ # ✅ Handling Method Selection
16
+ method_selection = user_message.lower().strip()
17
+ method_prompt = get_prompt_for_method(method_selection)
18
+
19
+ if method_prompt != "I didn’t understand your choice. Please type 'Bar Model,' 'Double Number Line,' or 'Equation' to proceed.":
20
+ messages.append({"role": "assistant", "content": method_prompt})
21
+ history.append((user_message, method_prompt))
22
+ return "", history
23
+
24
  messages.append({"role": "user", "content": user_message})
25
 
26
  # ✅ Get AI response
 
38
 
39
  except Exception as e:
40
  return f"⚠️ An error occurred: {str(e)}", history