Update app.py
Browse files
app.py
CHANGED
|
@@ -3,31 +3,28 @@ import gradio as gr
|
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
from openai import OpenAI
|
| 5 |
from prompts.initial_prompt import INITIAL_PROMPT
|
| 6 |
-
from prompts.main_prompt import
|
| 7 |
-
MAIN_PROMPT,
|
| 8 |
-
get_prompt_for_problem,
|
| 9 |
-
get_ccss_practice_standards,
|
| 10 |
-
get_problem_posing_task,
|
| 11 |
-
get_creativity_discussion,
|
| 12 |
-
get_summary,
|
| 13 |
-
)
|
| 14 |
|
| 15 |
-
# Load API
|
| 16 |
if os.path.exists(".env"):
|
| 17 |
load_dotenv(".env")
|
| 18 |
|
| 19 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 20 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 21 |
|
| 22 |
-
def gpt_call(history, user_message,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
"""
|
| 24 |
Calls OpenAI Chat API to generate responses.
|
| 25 |
- history: [(user_text, assistant_text), ...]
|
| 26 |
- user_message: latest message from user
|
| 27 |
"""
|
| 28 |
messages = [{"role": "system", "content": MAIN_PROMPT}]
|
| 29 |
-
|
| 30 |
-
# Add history
|
| 31 |
for user_text, assistant_text in history:
|
| 32 |
if user_text:
|
| 33 |
messages.append({"role": "user", "content": user_text})
|
|
@@ -35,7 +32,8 @@ def gpt_call(history, user_message, model="gpt-4o-mini", max_tokens=1024, temper
|
|
| 35 |
messages.append({"role": "assistant", "content": assistant_text})
|
| 36 |
|
| 37 |
messages.append({"role": "user", "content": user_message})
|
| 38 |
-
|
|
|
|
| 39 |
completion = client.chat.completions.create(
|
| 40 |
model=model,
|
| 41 |
messages=messages,
|
|
@@ -43,74 +41,60 @@ def gpt_call(history, user_message, model="gpt-4o-mini", max_tokens=1024, temper
|
|
| 43 |
temperature=temperature,
|
| 44 |
top_p=top_p
|
| 45 |
)
|
| 46 |
-
|
| 47 |
response = completion.choices[0].message.content
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
if
|
| 51 |
-
response =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
return response
|
| 54 |
|
| 55 |
def respond(user_message, history):
|
| 56 |
"""
|
| 57 |
Handles user input and chatbot responses.
|
| 58 |
-
- user_message: latest user input
|
| 59 |
-
- history: previous chat history
|
| 60 |
"""
|
| 61 |
if not user_message:
|
| 62 |
return "", history
|
| 63 |
|
| 64 |
-
|
| 65 |
-
if user_message.strip() in ["1", "2", "3"]:
|
| 66 |
-
assistant_reply = get_prompt_for_problem(user_message.strip())
|
| 67 |
-
|
| 68 |
-
# If user is at reflection stage, ask about CCSS Practice Standards
|
| 69 |
-
elif user_message.lower().strip() == "common core":
|
| 70 |
-
assistant_reply = get_ccss_practice_standards()
|
| 71 |
-
|
| 72 |
-
# If user is at problem-posing stage, ask them to create a new problem
|
| 73 |
-
elif user_message.lower().strip() == "problem posing":
|
| 74 |
-
assistant_reply = get_problem_posing_task()
|
| 75 |
-
|
| 76 |
-
# If user is at creativity discussion stage, ask for their thoughts
|
| 77 |
-
elif user_message.lower().strip() == "creativity":
|
| 78 |
-
assistant_reply = get_creativity_discussion()
|
| 79 |
-
|
| 80 |
-
# If user requests a summary, provide the final learning summary
|
| 81 |
-
elif user_message.lower().strip() == "summary":
|
| 82 |
-
assistant_reply = get_summary()
|
| 83 |
-
|
| 84 |
-
else:
|
| 85 |
-
# Continue conversation normally with AI guidance
|
| 86 |
-
assistant_reply = gpt_call(history, user_message)
|
| 87 |
-
|
| 88 |
-
# Update history
|
| 89 |
history.append((user_message, assistant_reply))
|
| 90 |
return "", history
|
| 91 |
|
| 92 |
##############################
|
| 93 |
-
# Gradio UI
|
| 94 |
##############################
|
| 95 |
with gr.Blocks() as demo:
|
| 96 |
gr.Markdown("## AI-Guided Math PD Chatbot")
|
| 97 |
|
| 98 |
-
# Initialize chatbot with first message
|
| 99 |
chatbot = gr.Chatbot(
|
| 100 |
-
value=[("", INITIAL_PROMPT)],
|
| 101 |
-
height=600
|
| 102 |
)
|
| 103 |
|
| 104 |
-
# Maintain chat history state
|
| 105 |
state_history = gr.State([("", INITIAL_PROMPT)])
|
| 106 |
|
| 107 |
-
# User input box
|
| 108 |
user_input = gr.Textbox(
|
| 109 |
placeholder="Type your message here...",
|
| 110 |
label="Your Input"
|
| 111 |
)
|
| 112 |
|
| 113 |
-
# Submit button
|
| 114 |
user_input.submit(
|
| 115 |
respond,
|
| 116 |
inputs=[user_input, state_history],
|
|
@@ -121,6 +105,5 @@ with gr.Blocks() as demo:
|
|
| 121 |
outputs=[state_history]
|
| 122 |
)
|
| 123 |
|
| 124 |
-
# Launch app
|
| 125 |
if __name__ == "__main__":
|
| 126 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
from openai import OpenAI
|
| 5 |
from prompts.initial_prompt import INITIAL_PROMPT
|
| 6 |
+
from prompts.main_prompt import MAIN_PROMPT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Load OpenAI 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 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 14 |
|
| 15 |
+
def gpt_call(history, user_message,
|
| 16 |
+
model="gpt-4o-mini",
|
| 17 |
+
max_tokens=1024,
|
| 18 |
+
temperature=0.7,
|
| 19 |
+
top_p=0.95):
|
| 20 |
"""
|
| 21 |
Calls OpenAI Chat API to generate responses.
|
| 22 |
- history: [(user_text, assistant_text), ...]
|
| 23 |
- user_message: latest message from user
|
| 24 |
"""
|
| 25 |
messages = [{"role": "system", "content": MAIN_PROMPT}]
|
| 26 |
+
|
| 27 |
+
# Add conversation history
|
| 28 |
for user_text, assistant_text in history:
|
| 29 |
if user_text:
|
| 30 |
messages.append({"role": "user", "content": user_text})
|
|
|
|
| 32 |
messages.append({"role": "assistant", "content": assistant_text})
|
| 33 |
|
| 34 |
messages.append({"role": "user", "content": user_message})
|
| 35 |
+
|
| 36 |
+
# OpenAI API Call
|
| 37 |
completion = client.chat.completions.create(
|
| 38 |
model=model,
|
| 39 |
messages=messages,
|
|
|
|
| 41 |
temperature=temperature,
|
| 42 |
top_p=top_p
|
| 43 |
)
|
| 44 |
+
|
| 45 |
response = completion.choices[0].message.content
|
| 46 |
|
| 47 |
+
# Encourage teachers to explain their reasoning before providing guidance
|
| 48 |
+
if "solve" in user_message.lower() or "explain" in user_message.lower():
|
| 49 |
+
response = "Great! Before we move forward, can you explain your reasoning? Why do you think this is the right approach? Once you share your thoughts, I'll guide you further.\n\n" + response
|
| 50 |
+
|
| 51 |
+
# Encourage problem posing
|
| 52 |
+
if "pose a problem" in user_message.lower():
|
| 53 |
+
response += "\n\nNow that you've explored this concept, try creating your own problem related to it. How would you challenge your students?"
|
| 54 |
+
|
| 55 |
+
# Cover Common Core practice standards
|
| 56 |
+
if "common core" in user_message.lower():
|
| 57 |
+
response += "\n\nHow do you see this aligning with Common Core practice standards? Can you identify any specific standards this connects to?"
|
| 58 |
+
|
| 59 |
+
# Encourage creativity-directed practices
|
| 60 |
+
if "creativity" in user_message.lower():
|
| 61 |
+
response += "\n\nHow did creativity play a role in this problem-solving process? Did you find any opportunities to think differently?"
|
| 62 |
+
|
| 63 |
+
# Provide structured summary
|
| 64 |
+
if "summary" in user_message.lower():
|
| 65 |
+
response += "\n\nSummary: Today, we explored problem-solving strategies, reflected on reasoning, and connected ideas to teaching practices. We examined key characteristics of proportional and non-proportional relationships, explored their graphical representations, and considered pedagogical approaches. Keep thinking about how these concepts can be applied in your own classroom!"
|
| 66 |
|
| 67 |
return response
|
| 68 |
|
| 69 |
def respond(user_message, history):
|
| 70 |
"""
|
| 71 |
Handles user input and chatbot responses.
|
|
|
|
|
|
|
| 72 |
"""
|
| 73 |
if not user_message:
|
| 74 |
return "", history
|
| 75 |
|
| 76 |
+
assistant_reply = gpt_call(history, user_message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
history.append((user_message, assistant_reply))
|
| 78 |
return "", history
|
| 79 |
|
| 80 |
##############################
|
| 81 |
+
# Gradio Blocks UI
|
| 82 |
##############################
|
| 83 |
with gr.Blocks() as demo:
|
| 84 |
gr.Markdown("## AI-Guided Math PD Chatbot")
|
| 85 |
|
|
|
|
| 86 |
chatbot = gr.Chatbot(
|
| 87 |
+
value=[("", INITIAL_PROMPT)],
|
| 88 |
+
height=600
|
| 89 |
)
|
| 90 |
|
|
|
|
| 91 |
state_history = gr.State([("", INITIAL_PROMPT)])
|
| 92 |
|
|
|
|
| 93 |
user_input = gr.Textbox(
|
| 94 |
placeholder="Type your message here...",
|
| 95 |
label="Your Input"
|
| 96 |
)
|
| 97 |
|
|
|
|
| 98 |
user_input.submit(
|
| 99 |
respond,
|
| 100 |
inputs=[user_input, state_history],
|
|
|
|
| 105 |
outputs=[state_history]
|
| 106 |
)
|
| 107 |
|
|
|
|
| 108 |
if __name__ == "__main__":
|
| 109 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|