Update app.py
Browse files
app.py
CHANGED
|
@@ -3,24 +3,31 @@ 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 |
|
| 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 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 15 |
|
| 16 |
def gpt_call(history, user_message, model="gpt-4o-mini", max_tokens=512, temperature=0.7, top_p=0.95):
|
| 17 |
"""
|
| 18 |
-
Calls OpenAI API to generate
|
| 19 |
- history: [(user_text, assistant_text), ...]
|
| 20 |
-
- user_message:
|
| 21 |
"""
|
| 22 |
messages = [{"role": "system", "content": MAIN_PROMPT}]
|
| 23 |
|
|
|
|
| 24 |
for user_text, assistant_text in history:
|
| 25 |
if user_text:
|
| 26 |
messages.append({"role": "user", "content": user_text})
|
|
@@ -36,48 +43,68 @@ def gpt_call(history, user_message, model="gpt-4o-mini", max_tokens=512, tempera
|
|
| 36 |
temperature=temperature,
|
| 37 |
top_p=top_p
|
| 38 |
)
|
|
|
|
| 39 |
return completion.choices[0].message.content
|
| 40 |
|
| 41 |
def respond(user_message, history):
|
| 42 |
"""
|
| 43 |
-
Handles chatbot responses.
|
| 44 |
-
-
|
| 45 |
-
-
|
| 46 |
"""
|
| 47 |
if not user_message:
|
| 48 |
return "", history
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
if "problem" in last_message.lower() and "solve" in last_message.lower():
|
| 54 |
-
# If the bot is expecting an explanation, store the response and move forward
|
| 55 |
-
history.append((user_message, "Thanks for sharing your reasoning! Let's analyze your response."))
|
| 56 |
else:
|
| 57 |
-
#
|
| 58 |
assistant_reply = gpt_call(history, user_message)
|
| 59 |
-
history.append((user_message, assistant_reply))
|
| 60 |
|
|
|
|
|
|
|
| 61 |
return "", history
|
| 62 |
|
| 63 |
##############################
|
| 64 |
-
# Gradio
|
| 65 |
##############################
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
gr.Markdown("## AI-Guided Math PD Chatbot")
|
| 68 |
|
|
|
|
| 69 |
chatbot = gr.Chatbot(
|
| 70 |
-
value=[("", INITIAL_PROMPT)],
|
| 71 |
height=500
|
| 72 |
)
|
| 73 |
|
|
|
|
| 74 |
state_history = gr.State([("", INITIAL_PROMPT)])
|
| 75 |
|
|
|
|
| 76 |
user_input = gr.Textbox(
|
| 77 |
placeholder="Type your message here...",
|
| 78 |
label="Your Input"
|
| 79 |
)
|
| 80 |
|
|
|
|
| 81 |
user_input.submit(
|
| 82 |
respond,
|
| 83 |
inputs=[user_input, state_history],
|
|
@@ -88,5 +115,6 @@ with gr.Blocks() as demo:
|
|
| 88 |
outputs=[state_history]
|
| 89 |
)
|
| 90 |
|
|
|
|
| 91 |
if __name__ == "__main__":
|
| 92 |
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 (
|
| 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 key from .env file
|
| 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, model="gpt-4o-mini", max_tokens=512, temperature=0.7, top_p=0.95):
|
| 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 to conversation
|
| 31 |
for user_text, assistant_text in history:
|
| 32 |
if user_text:
|
| 33 |
messages.append({"role": "user", "content": user_text})
|
|
|
|
| 43 |
temperature=temperature,
|
| 44 |
top_p=top_p
|
| 45 |
)
|
| 46 |
+
|
| 47 |
return completion.choices[0].message.content
|
| 48 |
|
| 49 |
def respond(user_message, history):
|
| 50 |
"""
|
| 51 |
+
Handles user input and chatbot responses.
|
| 52 |
+
- user_message: latest user input
|
| 53 |
+
- history: previous chat history
|
| 54 |
"""
|
| 55 |
if not user_message:
|
| 56 |
return "", history
|
| 57 |
|
| 58 |
+
# If user selects a problem number, redirect to the appropriate prompt
|
| 59 |
+
if user_message.strip() in ["1", "2", "3"]:
|
| 60 |
+
assistant_reply = get_prompt_for_problem(user_message.strip())
|
| 61 |
+
|
| 62 |
+
# If user is at reflection stage, ask about CCSS Practice Standards
|
| 63 |
+
elif user_message.lower().strip() == "common core":
|
| 64 |
+
assistant_reply = get_ccss_practice_standards()
|
| 65 |
+
|
| 66 |
+
# If user is at problem-posing stage, ask them to create a new problem
|
| 67 |
+
elif user_message.lower().strip() == "problem posing":
|
| 68 |
+
assistant_reply = get_problem_posing_task()
|
| 69 |
+
|
| 70 |
+
# If user is at creativity discussion stage, ask for their thoughts
|
| 71 |
+
elif user_message.lower().strip() == "creativity":
|
| 72 |
+
assistant_reply = get_creativity_discussion()
|
| 73 |
+
|
| 74 |
+
# If user requests a summary, provide the final learning summary
|
| 75 |
+
elif user_message.lower().strip() == "summary":
|
| 76 |
+
assistant_reply = get_summary()
|
| 77 |
|
|
|
|
|
|
|
|
|
|
| 78 |
else:
|
| 79 |
+
# Continue conversation normally with AI guidance
|
| 80 |
assistant_reply = gpt_call(history, user_message)
|
|
|
|
| 81 |
|
| 82 |
+
# Update history
|
| 83 |
+
history.append((user_message, assistant_reply))
|
| 84 |
return "", history
|
| 85 |
|
| 86 |
##############################
|
| 87 |
+
# Gradio UI Setup
|
| 88 |
##############################
|
| 89 |
with gr.Blocks() as demo:
|
| 90 |
gr.Markdown("## AI-Guided Math PD Chatbot")
|
| 91 |
|
| 92 |
+
# Initialize chatbot with first message
|
| 93 |
chatbot = gr.Chatbot(
|
| 94 |
+
value=[("", INITIAL_PROMPT)], # Initial system message
|
| 95 |
height=500
|
| 96 |
)
|
| 97 |
|
| 98 |
+
# Maintain chat history state
|
| 99 |
state_history = gr.State([("", INITIAL_PROMPT)])
|
| 100 |
|
| 101 |
+
# User input box
|
| 102 |
user_input = gr.Textbox(
|
| 103 |
placeholder="Type your message here...",
|
| 104 |
label="Your Input"
|
| 105 |
)
|
| 106 |
|
| 107 |
+
# Submit button
|
| 108 |
user_input.submit(
|
| 109 |
respond,
|
| 110 |
inputs=[user_input, state_history],
|
|
|
|
| 115 |
outputs=[state_history]
|
| 116 |
)
|
| 117 |
|
| 118 |
+
# Launch app
|
| 119 |
if __name__ == "__main__":
|
| 120 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|