Spaces:
Sleeping
Sleeping
Update app.py
Browse files123454567789101 unmodified line12131415161718192021222324232526272829303130323334353 unmodified lines3940414042434445444646474849505051525354import osimport gradio as grfrom openai import OpenAIfrom prompts.main_prompt import MAIN_PROMPT # β
Import Main Prompt Correctlyfrom prompts.initial_prompt import INITIAL_PROMPT # β
Import Initial Promptfrom prompts.main_prompt import MAIN_PROMPTfrom prompts.initial_prompt import INITIAL_PROMPT
# β
Ensure OpenAI API Key is Set (Use Secrets in Hugging Face)# β
Load API KeyOPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
if not OPENAI_API_KEY:1 unmodified line
client = OpenAI(api_key=OPENAI_API_KEY)
# β
Ensure Conversation Happens Step by Stepdef respond(user_message, history): if not user_message: return "", history
# β
AI only sends ONE response at a time (step-by-step) try: assistant_reply = client.chat.completions.create( model="gpt-4o", messages=[ {"role": "system", "content": MAIN_PROMPT}, # β
Ensures MAIN_PROMPT is used first {"role": "system", "content": MAIN_PROMPT}, *[ {"role": "user", "content": u} if i % 2 == 0 else {"role": "assistant", "content": a} for i, (u, a) in enumerate(history) ], {"role": "user", "content": user_message} ], max_tokens=512, max_tokens=256, # β
Limits response size (so AI doesnβt dump everything) temperature=0.7, ).choices[0].message.content except Exception as e:3 unmodified lines
return "", history
# β
Gradio UI# β
Fix Gradio UI to Start Properlywith gr.Blocks() as demo: gr.Markdown("# **AI-Guided Math PD Chatbot**")
# β
Start Chatbot with MAIN_PROMPT Instead of Initial Reflection chatbot = gr.Chatbot( value=[("", MAIN_PROMPT)], # β
Ensures chatbot begins with the math problems value=[("", INITIAL_PROMPT)], # β
Starts with an introduction message height=500 )
state_history = gr.State([("", MAIN_PROMPT)]) # β
Makes sure history starts correctly state_history = gr.State([("", INITIAL_PROMPT)]) # β
Ensures step-by-step history
user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")
app.py+10-9