Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,10 +2,9 @@ import os
|
|
2 |
import gradio as gr
|
3 |
from dotenv import load_dotenv
|
4 |
from openai import OpenAI
|
5 |
-
from prompts.
|
6 |
-
from prompts.main_prompt import MAIN_PROMPT, PROBLEM_SOLUTIONS_PROMPT # Ensure both are imported
|
7 |
|
8 |
-
# Load
|
9 |
if os.path.exists(".env"):
|
10 |
load_dotenv(".env")
|
11 |
|
@@ -13,97 +12,36 @@ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
13 |
|
14 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
15 |
|
16 |
-
|
17 |
-
def gpt_call(history, user_message,
|
18 |
-
model="gpt-4o",
|
19 |
-
max_tokens=3000, # Increased to 3000 to prevent truncation
|
20 |
-
temperature=0.7,
|
21 |
-
top_p=0.95):
|
22 |
-
"""
|
23 |
-
Calls the OpenAI API to generate a response.
|
24 |
-
- history: [(user_text, assistant_text), ...]
|
25 |
-
- user_message: The latest user message
|
26 |
-
"""
|
27 |
-
# 1) Start with the system message (MAIN_PROMPT) for context
|
28 |
-
messages = [{"role": "system", "content": MAIN_PROMPT}]
|
29 |
-
|
30 |
-
# 2) Append conversation history
|
31 |
-
for user_text, assistant_text in history:
|
32 |
-
if user_text:
|
33 |
-
messages.append({"role": "user", "content": user_text})
|
34 |
-
if assistant_text:
|
35 |
-
messages.append({"role": "assistant", "content": assistant_text})
|
36 |
-
|
37 |
-
# 3) Add the user's new message
|
38 |
-
messages.append({"role": "user", "content": user_message})
|
39 |
-
|
40 |
-
# 4) Call OpenAI API (with continuation handling)
|
41 |
-
full_response = ""
|
42 |
-
while True:
|
43 |
-
completion = client.chat.completions.create(
|
44 |
-
model=model,
|
45 |
-
messages=messages,
|
46 |
-
max_tokens=max_tokens, # Increased to allow longer responses
|
47 |
-
temperature=temperature,
|
48 |
-
top_p=top_p
|
49 |
-
)
|
50 |
-
|
51 |
-
response_part = completion.choices[0].message.content.strip()
|
52 |
-
full_response += " " + response_part
|
53 |
-
|
54 |
-
# If the response looks incomplete, force the AI to continue
|
55 |
-
if len(response_part) < max_tokens - 50: # Ensures near full completion
|
56 |
-
break # Stop loop if response is complete
|
57 |
-
|
58 |
-
# Add last response back into conversation history to continue it
|
59 |
-
messages.append({"role": "assistant", "content": response_part})
|
60 |
-
|
61 |
-
return full_response.strip()
|
62 |
-
|
63 |
-
|
64 |
def respond(user_message, history):
|
65 |
-
"""
|
66 |
-
Handles user input and gets GPT-generated response.
|
67 |
-
- user_message: The message from the user
|
68 |
-
- history: List of (user, assistant) conversation history
|
69 |
-
"""
|
70 |
if not user_message:
|
71 |
return "", history
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
-
# Add conversation turn to history
|
80 |
history.append((user_message, assistant_reply))
|
81 |
|
82 |
return "", history
|
83 |
|
84 |
-
|
85 |
-
##############################
|
86 |
-
# Gradio Blocks UI
|
87 |
-
##############################
|
88 |
with gr.Blocks() as demo:
|
89 |
gr.Markdown("## AI-Guided Math PD Chatbot")
|
|
|
90 |
|
91 |
-
|
92 |
-
chatbot = gr.Chatbot(
|
93 |
-
value=[("", INITIAL_PROMPT)], # Initial system prompt
|
94 |
-
height=500
|
95 |
-
)
|
96 |
|
97 |
-
|
98 |
-
state_history = gr.State([("", INITIAL_PROMPT)])
|
99 |
|
100 |
-
# User input field
|
101 |
-
user_input = gr.Textbox(
|
102 |
-
placeholder="Type your message here...",
|
103 |
-
label="Your Input"
|
104 |
-
)
|
105 |
-
|
106 |
-
# Submit action
|
107 |
user_input.submit(
|
108 |
respond,
|
109 |
inputs=[user_input, state_history],
|
@@ -114,6 +52,16 @@ with gr.Blocks() as demo:
|
|
114 |
outputs=[state_history]
|
115 |
)
|
116 |
|
117 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
if __name__ == "__main__":
|
119 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
2 |
import gradio as gr
|
3 |
from dotenv import load_dotenv
|
4 |
from openai import OpenAI
|
5 |
+
from prompts.main_prompt import MAIN_PROMPT
|
|
|
6 |
|
7 |
+
# Load API key
|
8 |
if os.path.exists(".env"):
|
9 |
load_dotenv(".env")
|
10 |
|
|
|
12 |
|
13 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
def respond(user_message, history):
|
|
|
|
|
|
|
|
|
|
|
16 |
if not user_message:
|
17 |
return "", history
|
18 |
|
19 |
+
assistant_reply = client.chat.completions.create(
|
20 |
+
model="gpt-4o",
|
21 |
+
messages=[
|
22 |
+
{"role": "system", "content": MAIN_PROMPT},
|
23 |
+
*[
|
24 |
+
{"role": "user", "content": u} if i % 2 == 0 else {"role": "assistant", "content": a}
|
25 |
+
for i, (u, a) in enumerate(history)
|
26 |
+
],
|
27 |
+
{"role": "user", "content": user_message}
|
28 |
+
],
|
29 |
+
max_tokens=512,
|
30 |
+
temperature=0.7,
|
31 |
+
).choices[0].message.content
|
32 |
|
|
|
33 |
history.append((user_message, assistant_reply))
|
34 |
|
35 |
return "", history
|
36 |
|
|
|
|
|
|
|
|
|
37 |
with gr.Blocks() as demo:
|
38 |
gr.Markdown("## AI-Guided Math PD Chatbot")
|
39 |
+
chatbot = gr.Chatbot(height=500)
|
40 |
|
41 |
+
state_history = gr.State([("", MAIN_PROMPT)])
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
user_input = gr.Textbox(placeholder="Type your message here...", label="Your Input")
|
|
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
user_input.submit(
|
46 |
respond,
|
47 |
inputs=[user_input, state_history],
|
|
|
52 |
outputs=[state_history]
|
53 |
)
|
54 |
|
55 |
+
# ✅ **Explicitly tell Gradio to use MathJax for LaTeX rendering**
|
56 |
+
gr.Markdown(
|
57 |
+
r"""
|
58 |
+
<script type="text/javascript">
|
59 |
+
MathJax = {
|
60 |
+
tex: { inlineMath: [['$', '$'], ['\\(', '\\)']] }
|
61 |
+
};
|
62 |
+
</script>
|
63 |
+
"""
|
64 |
+
)
|
65 |
+
|
66 |
if __name__ == "__main__":
|
67 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|