Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ 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"):
|
|
@@ -18,31 +18,28 @@ if not OPENAI_API_KEY:
|
|
| 18 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 19 |
|
| 20 |
# β
Chatbot Response Function
|
| 21 |
-
def respond(user_message, history):
|
| 22 |
if not user_message:
|
| 23 |
-
return "", history
|
| 24 |
|
| 25 |
-
user_message = user_message.strip().lower() # Normalize input
|
| 26 |
|
| 27 |
-
# β
Extract previous user input
|
| 28 |
-
last_user_input = history[-1][0].strip().lower() if history else ""
|
| 29 |
-
|
| 30 |
-
# β
Check if user selected a method
|
| 31 |
valid_methods = ["bar model", "double number line", "equation"]
|
| 32 |
|
|
|
|
| 33 |
if user_message in valid_methods:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
-
# β
|
| 39 |
-
if
|
| 40 |
-
selected_method = last_user_input # Keep track of last selected method
|
| 41 |
feedback = get_feedback_for_method(selected_method, user_message)
|
| 42 |
-
history.append((user_message, feedback)) #
|
| 43 |
-
return feedback, history
|
| 44 |
|
| 45 |
-
return "β
|
| 46 |
|
| 47 |
# β
Gradio UI Setup
|
| 48 |
with gr.Blocks() as demo:
|
|
@@ -50,18 +47,15 @@ with gr.Blocks() as demo:
|
|
| 50 |
|
| 51 |
chatbot = gr.Chatbot(value=[(INITIAL_PROMPT, "")], height=500)
|
| 52 |
state_history = gr.State([(INITIAL_PROMPT, "")])
|
|
|
|
| 53 |
|
| 54 |
user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")
|
| 55 |
|
| 56 |
# β
Handling user input and response logic
|
| 57 |
user_input.submit(
|
| 58 |
respond,
|
| 59 |
-
inputs=[user_input, state_history],
|
| 60 |
-
outputs=[
|
| 61 |
-
).then(
|
| 62 |
-
fn=lambda _, h: h,
|
| 63 |
-
inputs=[user_input, chatbot],
|
| 64 |
-
outputs=[state_history]
|
| 65 |
)
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
|
|
|
| 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 get_prompt_for_method, get_feedback_for_method
|
| 7 |
|
| 8 |
# β
Load API key from .env file
|
| 9 |
if os.path.exists(".env"):
|
|
|
|
| 18 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 19 |
|
| 20 |
# β
Chatbot Response Function
|
| 21 |
+
def respond(user_message, history, selected_method):
|
| 22 |
if not user_message:
|
| 23 |
+
return "", history, selected_method
|
| 24 |
|
| 25 |
+
user_message = user_message.strip().lower() # Normalize input
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
valid_methods = ["bar model", "double number line", "equation"]
|
| 28 |
|
| 29 |
+
# β
If user selects a method, store it and provide the method-specific prompt
|
| 30 |
if user_message in valid_methods:
|
| 31 |
+
selected_method = user_message # Store the method
|
| 32 |
+
method_prompt = get_prompt_for_method(user_message)
|
| 33 |
+
history.append((user_message, method_prompt)) # Save to history
|
| 34 |
+
return method_prompt, history, selected_method
|
| 35 |
|
| 36 |
+
# β
If a method has already been selected, provide feedback
|
| 37 |
+
if selected_method:
|
|
|
|
| 38 |
feedback = get_feedback_for_method(selected_method, user_message)
|
| 39 |
+
history.append((user_message, feedback)) # Save user response and feedback
|
| 40 |
+
return feedback, history, selected_method
|
| 41 |
|
| 42 |
+
return "β Please select a method first (Bar Model, Double Number Line, or Equation).", history, selected_method
|
| 43 |
|
| 44 |
# β
Gradio UI Setup
|
| 45 |
with gr.Blocks() as demo:
|
|
|
|
| 47 |
|
| 48 |
chatbot = gr.Chatbot(value=[(INITIAL_PROMPT, "")], height=500)
|
| 49 |
state_history = gr.State([(INITIAL_PROMPT, "")])
|
| 50 |
+
state_selected_method = gr.State(None) # β
New state to track selected method
|
| 51 |
|
| 52 |
user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")
|
| 53 |
|
| 54 |
# β
Handling user input and response logic
|
| 55 |
user_input.submit(
|
| 56 |
respond,
|
| 57 |
+
inputs=[user_input, state_history, state_selected_method],
|
| 58 |
+
outputs=[chatbot, state_history, state_selected_method]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
)
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|