-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
91 lines (82 loc) · 2.51 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import streamlit as st
from openai import OpenAI
def apply_custom_css():
"""
Applies custom CSS styles to the Streamlit app for a mathematical theme.
"""
st.markdown(
"""
<style>
body {
background-color: #f0f2f6;
font-family: 'Georgia', serif;
}
.stTextInput > div > input {
font-family: 'Courier New', monospace;
background-color: #eef;
}
.stTextArea > div > textarea {
font-family: 'Courier New', monospace;
background-color: #eef;
}
.stButton > button {
background-color: #004080;
color: white;
}
.stMarkdown {
font-size: 18px;
}
</style>
""",
unsafe_allow_html=True
)
def get_user_input():
"""
Renders input fields for the API key, model selection, and math query, and returns their values.
"""
st.title("∑ MathGPT")
st.write("Welcome to **MathGPT**! Enter your OpenAI API key, select a model, and input your math query below.")
# Input field for OpenAI API key
api_key = st.text_input("🔑 OpenAI API Key", type="password")
# Dropdown for model selection
model_options = ["o1-mini", "o1-preview"]
selected_model = st.selectbox("🧠 Select Model", model_options)
# Input field for the math query
query = st.text_area("✍️ Math Query", height=150)
return api_key, query, selected_model
def generate_response(api_key, query, selected_model):
"""
Sends the user's query to the OpenAI API and returns the response.
"""
# Set the OpenAI API key
client = OpenAI(api_key=api_key)
try:
response = client.chat.completions.create(
model=selected_model,
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": query
},
],
}
]
)
answer = response.choices[0].message.content
return answer
except Exception as e:
st.error(f"An error occurred: {e}")
return None
def display_answer(answer):
"""
Displays the answer returned from the OpenAI API.
"""
st.markdown("### 📚 Answer:")
# Display the answer with syntax highlighting if it's code
if "```" in answer:
st.markdown(answer)
else:
st.write(answer)