Spaces:
Sleeping
Sleeping
Update app.py
Browse files123456789101 unmodified line121314151617181920212223242526272829303132333 unmodified lines3738394041424344454647484950515253import 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 Prompt# β Ensure OpenAI API Key is Set (Use Secrets in Hugging Face)OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")if not OPENAI_API_KEY:1 unmodified lineclient = OpenAI(api_key=OPENAI_API_KEY)def respond(user_message, history):if not user_message:return "", historytry:assistant_reply = client.chat.completions.create(model="gpt-4o",messages=[{"role": "system", "content": MAIN_PROMPT}, # β Ensures MAIN_PROMPT is used first*[{"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,temperature=0.7,).choices[0].message.contentexcept Exception as e:3 unmodified linesreturn "", history# β Gradio UIwith gr.Blocks() as demo:gr.Markdown("# **AI-Guided Math PD Chatbot**")# β Start Chatbot with MAIN_PROMPT Instead of Initial Reflectionchatbot = gr.Chatbot(value=[("", MAIN_PROMPT)], # β Ensures chatbot begins with the math problemsheight=500)state_history = gr.State([("", MAIN_PROMPT)]) # β Makes sure history starts correctlyuser_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")123456789101 unmodified line1213141516171819202122232425262728293031323334353 unmodified lines39404142434445464748495051525354import osimport gradio as grfrom openai import OpenAIfrom prompts.main_prompt import MAIN_PROMPTfrom prompts.initial_prompt import INITIAL_PROMPT# β Load API KeyOPENAI_API_KEY = os.getenv("OPENAI_API_KEY")if not OPENAI_API_KEY:1 unmodified lineclient = 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},*[{"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=256, # β Limits response size (so AI doesnβt dump everything)temperature=0.7,).choices[0].message.contentexcept Exception as e:3 unmodified linesreturn "", history# β Fix Gradio UI to Start Properlywith gr.Blocks() as demo:gr.Markdown("# **AI-Guided Math PD Chatbot**")chatbot = gr.Chatbot(value=[("", INITIAL_PROMPT)], # β Starts with an introduction messageheight=500)state_history = gr.State([("", INITIAL_PROMPT)]) # β Ensures step-by-step historyuser_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")
app.py+10-9