alibicer commited on
Commit
10a976f
Β·
verified Β·
1 Parent(s): a1d25c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -9
app.py CHANGED
@@ -1,10 +1,10 @@
1
  import os
2
  import gradio as gr
3
  from openai import OpenAI
4
- from prompts.main_prompt import MAIN_PROMPT # βœ… Import Main Prompt Correctly
5
- from prompts.initial_prompt import INITIAL_PROMPT # βœ… Import Initial Prompt
6
 
7
- # βœ… Ensure OpenAI API Key is Set (Use Secrets in Hugging Face)
8
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
9
 
10
  if not OPENAI_API_KEY:
@@ -12,22 +12,24 @@ if not OPENAI_API_KEY:
12
 
13
  client = OpenAI(api_key=OPENAI_API_KEY)
14
 
 
15
  def respond(user_message, history):
16
  if not user_message:
17
  return "", history
18
 
 
19
  try:
20
  assistant_reply = client.chat.completions.create(
21
  model="gpt-4o",
22
  messages=[
23
- {"role": "system", "content": MAIN_PROMPT}, # βœ… Ensures MAIN_PROMPT is used first
24
  *[
25
  {"role": "user", "content": u} if i % 2 == 0 else {"role": "assistant", "content": a}
26
  for i, (u, a) in enumerate(history)
27
  ],
28
  {"role": "user", "content": user_message}
29
  ],
30
- max_tokens=512,
31
  temperature=0.7,
32
  ).choices[0].message.content
33
  except Exception as e:
@@ -37,17 +39,16 @@ def respond(user_message, history):
37
 
38
  return "", history
39
 
40
- # βœ… Gradio UI
41
  with gr.Blocks() as demo:
42
  gr.Markdown("# **AI-Guided Math PD Chatbot**")
43
 
44
- # βœ… Start Chatbot with MAIN_PROMPT Instead of Initial Reflection
45
  chatbot = gr.Chatbot(
46
- value=[("", MAIN_PROMPT)], # βœ… Ensures chatbot begins with the math problems
47
  height=500
48
  )
49
 
50
- state_history = gr.State([("", MAIN_PROMPT)]) # βœ… Makes sure history starts correctly
51
 
52
  user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")
53
 
 
1
  import os
2
  import gradio as gr
3
  from openai import OpenAI
4
+ from prompts.main_prompt import MAIN_PROMPT
5
+ from prompts.initial_prompt import INITIAL_PROMPT
6
 
7
+ # βœ… Load API Key
8
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
9
 
10
  if not OPENAI_API_KEY:
 
12
 
13
  client = OpenAI(api_key=OPENAI_API_KEY)
14
 
15
+ # βœ… Ensure Conversation Happens Step by Step
16
  def respond(user_message, history):
17
  if not user_message:
18
  return "", history
19
 
20
+ # βœ… AI only sends ONE response at a time (step-by-step)
21
  try:
22
  assistant_reply = client.chat.completions.create(
23
  model="gpt-4o",
24
  messages=[
25
+ {"role": "system", "content": MAIN_PROMPT},
26
  *[
27
  {"role": "user", "content": u} if i % 2 == 0 else {"role": "assistant", "content": a}
28
  for i, (u, a) in enumerate(history)
29
  ],
30
  {"role": "user", "content": user_message}
31
  ],
32
+ max_tokens=256, # βœ… Limits response size (so AI doesn’t dump everything)
33
  temperature=0.7,
34
  ).choices[0].message.content
35
  except Exception as e:
 
39
 
40
  return "", history
41
 
42
+ # βœ… Fix Gradio UI to Start Properly
43
  with gr.Blocks() as demo:
44
  gr.Markdown("# **AI-Guided Math PD Chatbot**")
45
 
 
46
  chatbot = gr.Chatbot(
47
+ value=[("", INITIAL_PROMPT)], # βœ… Starts with an introduction message
48
  height=500
49
  )
50
 
51
+ state_history = gr.State([("", INITIAL_PROMPT)]) # βœ… Ensures step-by-step history
52
 
53
  user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")
54