Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from prompts.initial_prompt import INITIAL_PROMPT
|
|
| 6 |
from prompts.main_prompt import TASK_PROMPT
|
| 7 |
|
| 8 |
|
| 9 |
-
# .env
|
| 10 |
if os.path.exists(".env"):
|
| 11 |
load_dotenv(".env")
|
| 12 |
|
|
@@ -15,96 +15,105 @@ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
| 15 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 16 |
|
| 17 |
|
| 18 |
-
|
| 19 |
def gpt_call(history, user_message,
|
| 20 |
model="gpt-4o-mini",
|
| 21 |
max_tokens=512,
|
| 22 |
temperature=0.7,
|
| 23 |
top_p=0.95):
|
| 24 |
"""
|
| 25 |
-
OpenAI ChatCompletion API
|
| 26 |
- history: [(user_text, assistant_text), ...]
|
| 27 |
-
- user_message:
|
| 28 |
"""
|
| 29 |
-
#
|
| 30 |
messages = [{"role": "system", "content": TASK_PROMPT}]
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
# user_text -> 'user' / assistant_text -> 'assistant'
|
| 34 |
for user_text, assistant_text in history:
|
| 35 |
if user_text:
|
| 36 |
messages.append({"role": "user", "content": user_text})
|
| 37 |
if assistant_text:
|
| 38 |
messages.append({"role": "assistant", "content": assistant_text})
|
| 39 |
|
| 40 |
-
#
|
| 41 |
messages.append({"role": "user", "content": user_message})
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
model
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
)
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
def respond(user_message, history):
|
| 54 |
"""
|
| 55 |
-
|
| 56 |
-
- user_message:
|
| 57 |
-
- history:
|
| 58 |
"""
|
| 59 |
-
# ์ฌ์ฉ์๊ฐ ๋น ๋ฌธ์์ด์ ๋ณด๋๋ค๋ฉด ์๋ฌด ์ผ๋ ํ์ง ์์
|
| 60 |
if not user_message:
|
| 61 |
return "", history
|
| 62 |
|
| 63 |
-
#
|
| 64 |
assistant_reply = gpt_call(history, user_message)
|
| 65 |
|
| 66 |
-
#
|
| 67 |
history.append((user_message, assistant_reply))
|
| 68 |
|
| 69 |
-
#
|
| 70 |
return "", history
|
| 71 |
|
|
|
|
| 72 |
##############################
|
| 73 |
-
# Gradio
|
| 74 |
##############################
|
| 75 |
with gr.Blocks() as demo:
|
| 76 |
-
gr.Markdown("##
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
# ์ฒซ ๋ฒ์งธ ๋ฉ์์ง๋ (user="", assistant=INITIAL_PROMPT) ํํ๋ก ๋ฃ์ด
|
| 80 |
-
# ํ๋ฉด์์์ 'assistant'๊ฐ INITIAL_PROMPT๋ฅผ ๋งํ ๊ฒ์ฒ๋ผ ๋ณด์ด๊ฒ ํจ
|
| 81 |
chatbot = gr.Chatbot(
|
| 82 |
-
value=[("", INITIAL_PROMPT)],
|
| 83 |
height=500
|
| 84 |
)
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
# ์ฌ๊ธฐ์๋ ๋์ผํ ์ด๊ธฐ ์ํ๋ฅผ ๋ฃ์ด์ค
|
| 88 |
state_history = gr.State([("", INITIAL_PROMPT)])
|
| 89 |
|
| 90 |
-
#
|
| 91 |
user_input = gr.Textbox(
|
| 92 |
-
placeholder="Type your
|
| 93 |
label="Your Input"
|
| 94 |
)
|
| 95 |
|
| 96 |
-
#
|
| 97 |
user_input.submit(
|
| 98 |
respond,
|
| 99 |
inputs=[user_input, state_history],
|
| 100 |
outputs=[user_input, chatbot]
|
| 101 |
).then(
|
| 102 |
-
# respond ๋๋ ๋ค, ์ต์ history๋ฅผ state_history์ ๋ฐ์
|
| 103 |
fn=lambda _, h: h,
|
| 104 |
inputs=[user_input, chatbot],
|
| 105 |
outputs=[state_history]
|
| 106 |
)
|
| 107 |
|
| 108 |
-
#
|
| 109 |
if __name__ == "__main__":
|
| 110 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|
| 6 |
from prompts.main_prompt import TASK_PROMPT
|
| 7 |
|
| 8 |
|
| 9 |
+
# Load the OpenAI API key from .env file
|
| 10 |
if os.path.exists(".env"):
|
| 11 |
load_dotenv(".env")
|
| 12 |
|
|
|
|
| 15 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 16 |
|
| 17 |
|
|
|
|
| 18 |
def gpt_call(history, user_message,
|
| 19 |
model="gpt-4o-mini",
|
| 20 |
max_tokens=512,
|
| 21 |
temperature=0.7,
|
| 22 |
top_p=0.95):
|
| 23 |
"""
|
| 24 |
+
Calls OpenAI's ChatCompletion API to generate responses.
|
| 25 |
- history: [(user_text, assistant_text), ...]
|
| 26 |
+
- user_message: User's latest input
|
| 27 |
"""
|
| 28 |
+
# System message (TASK_PROMPT) at the beginning
|
| 29 |
messages = [{"role": "system", "content": TASK_PROMPT}]
|
| 30 |
|
| 31 |
+
# Convert history into OpenAI format
|
|
|
|
| 32 |
for user_text, assistant_text in history:
|
| 33 |
if user_text:
|
| 34 |
messages.append({"role": "user", "content": user_text})
|
| 35 |
if assistant_text:
|
| 36 |
messages.append({"role": "assistant", "content": assistant_text})
|
| 37 |
|
| 38 |
+
# Add the latest user input
|
| 39 |
messages.append({"role": "user", "content": user_message})
|
| 40 |
|
| 41 |
+
# AI-controlled gradual guidance
|
| 42 |
+
if "bar model" in user_message.lower():
|
| 43 |
+
return "Great! You've started using a bar model. Can you explain how you divided it? What does each section represent?"
|
| 44 |
+
|
| 45 |
+
elif "double number line" in user_message.lower():
|
| 46 |
+
return "Nice! How does your number line show the relationship between time and distance? Did you mark the correct intervals?"
|
| 47 |
+
|
| 48 |
+
elif "ratio table" in user_message.lower():
|
| 49 |
+
return "Good choice! Before I check, how did you determine the ratio for 1 hour?"
|
| 50 |
+
|
| 51 |
+
elif "graph" in user_message.lower():
|
| 52 |
+
return "Graphs are powerful! What key points did you plot, and why?"
|
| 53 |
+
|
| 54 |
+
else:
|
| 55 |
+
# OpenAI API call (fallback response)
|
| 56 |
+
completion = client.chat.completions.create(
|
| 57 |
+
model=model,
|
| 58 |
+
messages=messages,
|
| 59 |
+
max_tokens=max_tokens,
|
| 60 |
+
temperature=temperature,
|
| 61 |
+
top_p=top_p
|
| 62 |
+
)
|
| 63 |
+
return completion.choices[0].message.content
|
| 64 |
+
|
| 65 |
|
| 66 |
def respond(user_message, history):
|
| 67 |
"""
|
| 68 |
+
Handles user input and chatbot response in Gradio.
|
| 69 |
+
- user_message: The latest input from the user.
|
| 70 |
+
- history: A list of (user, assistant) message pairs.
|
| 71 |
"""
|
|
|
|
| 72 |
if not user_message:
|
| 73 |
return "", history
|
| 74 |
|
| 75 |
+
# Generate AI response
|
| 76 |
assistant_reply = gpt_call(history, user_message)
|
| 77 |
|
| 78 |
+
# Append to history
|
| 79 |
history.append((user_message, assistant_reply))
|
| 80 |
|
| 81 |
+
# Return the updated history and clear the input box
|
| 82 |
return "", history
|
| 83 |
|
| 84 |
+
|
| 85 |
##############################
|
| 86 |
+
# Gradio Chatbot UI
|
| 87 |
##############################
|
| 88 |
with gr.Blocks() as demo:
|
| 89 |
+
gr.Markdown("## AI-Guided Teacher PD Chatbot")
|
| 90 |
|
| 91 |
+
# Initial chatbot message (starts with the task)
|
|
|
|
|
|
|
| 92 |
chatbot = gr.Chatbot(
|
| 93 |
+
value=[("", INITIAL_PROMPT)],
|
| 94 |
height=500
|
| 95 |
)
|
| 96 |
|
| 97 |
+
# Chat history state
|
|
|
|
| 98 |
state_history = gr.State([("", INITIAL_PROMPT)])
|
| 99 |
|
| 100 |
+
# User input box
|
| 101 |
user_input = gr.Textbox(
|
| 102 |
+
placeholder="Type your response here...",
|
| 103 |
label="Your Input"
|
| 104 |
)
|
| 105 |
|
| 106 |
+
# When user submits input โ respond() updates chatbot
|
| 107 |
user_input.submit(
|
| 108 |
respond,
|
| 109 |
inputs=[user_input, state_history],
|
| 110 |
outputs=[user_input, chatbot]
|
| 111 |
).then(
|
|
|
|
| 112 |
fn=lambda _, h: h,
|
| 113 |
inputs=[user_input, chatbot],
|
| 114 |
outputs=[state_history]
|
| 115 |
)
|
| 116 |
|
| 117 |
+
# Launch the chatbot
|
| 118 |
if __name__ == "__main__":
|
| 119 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|