parasucolucy
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import openai
|
3 |
+
import uuid
|
4 |
+
import time
|
5 |
+
import pandas as pd
|
6 |
+
import io
|
7 |
+
from openai import OpenAI
|
8 |
+
|
9 |
+
css_code = """
|
10 |
+
<style>
|
11 |
+
section[data-testid="stSidebar"] > div > div:nth-child(2) {
|
12 |
+
padding-top: 0.75rem !important;
|
13 |
+
}
|
14 |
+
|
15 |
+
section.main > div {
|
16 |
+
padding-top: 64px;
|
17 |
+
}
|
18 |
+
</style>
|
19 |
+
"""
|
20 |
+
|
21 |
+
# Initialize OpenAI client
|
22 |
+
client = OpenAI()
|
23 |
+
|
24 |
+
# Your chosen model
|
25 |
+
MODEL = st.secrets["MODEL"]
|
26 |
+
|
27 |
+
def main():
|
28 |
+
# Initialize session state variables
|
29 |
+
if "session_id" not in st.session_state:
|
30 |
+
st.session_state.session_id = str(uuid.uuid4())
|
31 |
+
|
32 |
+
if "run" not in st.session_state:
|
33 |
+
st.session_state.run = {"status": None}
|
34 |
+
|
35 |
+
if "messages" not in st.session_state:
|
36 |
+
st.session_state.messages = []
|
37 |
+
|
38 |
+
if "retry_error" not in st.session_state:
|
39 |
+
st.session_state.retry_error = 0
|
40 |
+
|
41 |
+
# Set up the page
|
42 |
+
st.set_page_config(page_title="Chemistry Instructor", page_icon="⚖️")
|
43 |
+
st.header("ChatGPT Based Chemistry Assistant")
|
44 |
+
st.markdown(css_code, unsafe_allow_html=True)
|
45 |
+
with st.sidebar:
|
46 |
+
st.write("---")
|
47 |
+
st.image("hydrogen.jpg")
|
48 |
+
st.write("---")
|
49 |
+
st.title("Chemistry AI")
|
50 |
+
st.write("---")
|
51 |
+
|
52 |
+
# Initialize OpenAI assistant
|
53 |
+
if "assistant" not in st.session_state:
|
54 |
+
openai.api_key = st.secrets["OPENAI_API_KEY"]
|
55 |
+
st.session_state.assistant = openai.beta.assistants.retrieve(st.secrets["OPENAI_ASSISTANT"])
|
56 |
+
st.session_state.thread = client.beta.threads.create(
|
57 |
+
metadata={'session_id': st.session_state.session_id}
|
58 |
+
)
|
59 |
+
|
60 |
+
# Display chat messages
|
61 |
+
elif hasattr(st.session_state.run, 'status') and st.session_state.run.status == "completed":
|
62 |
+
st.session_state.messages = client.beta.threads.messages.list(
|
63 |
+
thread_id=st.session_state.thread.id
|
64 |
+
)
|
65 |
+
for message in reversed(st.session_state.messages.data):
|
66 |
+
if message.role in ["user", "assistant"]:
|
67 |
+
with st.chat_message(message.role):
|
68 |
+
for content_part in message.content:
|
69 |
+
message_text = content_part.text.value
|
70 |
+
st.markdown(message_text)
|
71 |
+
|
72 |
+
# Chat input and message creation with file ID
|
73 |
+
if prompt := st.chat_input("How can I help you?"):
|
74 |
+
with st.chat_message('user'):
|
75 |
+
st.write(prompt)
|
76 |
+
|
77 |
+
message_data = {
|
78 |
+
"thread_id": st.session_state.thread.id,
|
79 |
+
"role": "user",
|
80 |
+
"content": prompt
|
81 |
+
}
|
82 |
+
# Include file ID in the request if available
|
83 |
+
if "file_id" in st.session_state:
|
84 |
+
message_data["file_ids"] = [st.session_state.file_id]
|
85 |
+
|
86 |
+
st.session_state.messages = client.beta.threads.messages.create(**message_data)
|
87 |
+
|
88 |
+
st.session_state.run = client.beta.threads.runs.create(
|
89 |
+
thread_id=st.session_state.thread.id,
|
90 |
+
assistant_id=st.session_state.assistant.id,
|
91 |
+
)
|
92 |
+
if st.session_state.retry_error < 3:
|
93 |
+
time.sleep(1)
|
94 |
+
st.rerun()
|
95 |
+
|
96 |
+
# Handle run status
|
97 |
+
if hasattr(st.session_state.run, 'status'):
|
98 |
+
if st.session_state.run.status == "running":
|
99 |
+
with st.chat_message('assistant'):
|
100 |
+
st.write("Thinking ......")
|
101 |
+
if st.session_state.retry_error < 3:
|
102 |
+
time.sleep(1)
|
103 |
+
st.rerun()
|
104 |
+
elif st.session_state.run.status == "failed":
|
105 |
+
st.session_state.retry_error += 1
|
106 |
+
with st.chat_message('assistant'):
|
107 |
+
if st.session_state.retry_error < 3:
|
108 |
+
st.write("Run failed, retrying ......")
|
109 |
+
time.sleep(3)
|
110 |
+
st.rerun()
|
111 |
+
else:
|
112 |
+
st.error(
|
113 |
+
"FAILED: The OpenAI API is currently processing too many requests. Please try again later ......")
|
114 |
+
elif st.session_state.run.status != "completed":
|
115 |
+
st.session_state.run = client.beta.threads.runs.retrieve(
|
116 |
+
thread_id=st.session_state.thread.id,
|
117 |
+
run_id=st.session_state.run.id,
|
118 |
+
)
|
119 |
+
if st.session_state.retry_error < 3:
|
120 |
+
time.sleep(3)
|
121 |
+
st.rerun()
|
122 |
+
|
123 |
+
if __name__ == "__main__":
|
124 |
+
main()
|