alibicer commited on
Commit
2f8a229
·
verified ·
1 Parent(s): a15c076

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -1
app.py CHANGED
@@ -1,3 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  def respond(user_message, history):
2
  if not user_message:
3
  return "", history
@@ -23,7 +42,7 @@ def respond(user_message, history):
23
 
24
  messages.append({"role": "user", "content": user_message})
25
 
26
- # ✅ Get AI response
27
  completion = client.chat.completions.create(
28
  model="gpt-4o",
29
  messages=messages,
@@ -38,3 +57,25 @@ def respond(user_message, history):
38
 
39
  except Exception as e:
40
  return f"⚠️ An error occurred: {str(e)}", history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, get_prompt_for_method
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
+ # ✅ Correct OpenAI Client Initialization
18
+ client = OpenAI()
19
+
20
  def respond(user_message, history):
21
  if not user_message:
22
  return "", history
 
42
 
43
  messages.append({"role": "user", "content": user_message})
44
 
45
+ # ✅ Get AI response (Updated call to OpenAI API)
46
  completion = client.chat.completions.create(
47
  model="gpt-4o",
48
  messages=messages,
 
57
 
58
  except Exception as e:
59
  return f"⚠️ An error occurred: {str(e)}", history
60
+
61
+ # ✅ Gradio UI Setup
62
+ with gr.Blocks() as demo:
63
+ gr.Markdown("## 🤖 AI-Guided Math PD Chatbot")
64
+
65
+ chatbot = gr.Chatbot(value=[("", INITIAL_PROMPT)], height=500)
66
+ state_history = gr.State([("", INITIAL_PROMPT)])
67
+
68
+ user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")
69
+
70
+ user_input.submit(
71
+ respond,
72
+ inputs=[user_input, state_history],
73
+ outputs=[user_input, chatbot]
74
+ ).then(
75
+ fn=lambda _, h: h,
76
+ inputs=[user_input, chatbot],
77
+ outputs=[state_history]
78
+ )
79
+
80
+ if __name__ == "__main__":
81
+ demo.launch(server_name="0.0.0.0", server_port=7860)