Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ from openai import OpenAI
|
|
3 |
import time
|
4 |
import re
|
5 |
|
6 |
-
# Streamlit
|
7 |
st.set_page_config(page_title="documentaitest")
|
8 |
st.title("documentaitest")
|
9 |
st.caption("Chat with an Ai Assistant on your Documents")
|
@@ -12,6 +12,7 @@ st.caption("Chat with an Ai Assistant on your Documents")
|
|
12 |
with st.sidebar:
|
13 |
OPENAI_API_KEY = st.text_input("Enter your C2 Group of Technologies Access Key", type="password")
|
14 |
|
|
|
15 |
if not OPENAI_API_KEY:
|
16 |
st.error("Please enter your C2 Group of Technologies Access Key to continue.")
|
17 |
st.stop()
|
@@ -19,36 +20,37 @@ if not OPENAI_API_KEY:
|
|
19 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
20 |
ASSISTANT_ID = "asst_PJjxQftfz2IJUUMvnldK58lB"
|
21 |
|
22 |
-
#
|
23 |
if "messages" not in st.session_state:
|
24 |
st.session_state.messages = []
|
25 |
if "thread_id" not in st.session_state:
|
26 |
st.session_state.thread_id = None
|
27 |
|
28 |
-
#
|
29 |
if st.button("Clear Chat", use_container_width=True):
|
30 |
st.session_state.messages = []
|
31 |
st.session_state.thread_id = None
|
32 |
st.rerun()
|
33 |
|
34 |
-
#
|
35 |
for message in st.session_state.messages:
|
36 |
role, content = message["role"], message["content"]
|
37 |
st.chat_message(role).write(content)
|
38 |
|
39 |
-
# Handle new input
|
40 |
if prompt := st.chat_input():
|
41 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
42 |
st.chat_message("user").write(prompt)
|
43 |
|
44 |
try:
|
|
|
45 |
if st.session_state.thread_id is None:
|
46 |
thread = client.beta.threads.create()
|
47 |
st.session_state.thread_id = thread.id
|
48 |
|
49 |
thread_id = st.session_state.thread_id
|
50 |
|
51 |
-
# Send
|
52 |
client.beta.threads.messages.create(
|
53 |
thread_id=thread_id,
|
54 |
role="user",
|
@@ -63,19 +65,23 @@ if prompt := st.chat_input():
|
|
63 |
|
64 |
# Poll until complete
|
65 |
while True:
|
66 |
-
run_status = client.beta.threads.runs.retrieve(
|
|
|
|
|
|
|
67 |
if run_status.status == "completed":
|
68 |
break
|
69 |
time.sleep(1)
|
70 |
|
71 |
# Get assistant response
|
72 |
messages = client.beta.threads.messages.list(thread_id=thread_id)
|
|
|
73 |
for message in reversed(messages.data):
|
74 |
if message.role == "assistant":
|
75 |
assistant_message = message.content[0].text.value
|
76 |
break
|
77 |
|
78 |
-
#
|
79 |
st.chat_message("assistant").write(assistant_message)
|
80 |
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
81 |
|
@@ -84,7 +90,7 @@ if prompt := st.chat_input():
|
|
84 |
if page_match:
|
85 |
page_number = page_match.group(1).zfill(3)
|
86 |
image_url = f"https://raw.githubusercontent.com/AndrewLORTech/surgical-pathology-manual/main/51940670-Manual-of-Surgical-Pathology-Third-Edition_1_page_{page_number}.png"
|
87 |
-
st.image(image_url, caption=f"Page {int(page_number)}",
|
88 |
|
89 |
except Exception as e:
|
90 |
st.error(f"Error: {str(e)}")
|
|
|
3 |
import time
|
4 |
import re
|
5 |
|
6 |
+
# Streamlit setup
|
7 |
st.set_page_config(page_title="documentaitest")
|
8 |
st.title("documentaitest")
|
9 |
st.caption("Chat with an Ai Assistant on your Documents")
|
|
|
12 |
with st.sidebar:
|
13 |
OPENAI_API_KEY = st.text_input("Enter your C2 Group of Technologies Access Key", type="password")
|
14 |
|
15 |
+
# Stop execution if no API key provided
|
16 |
if not OPENAI_API_KEY:
|
17 |
st.error("Please enter your C2 Group of Technologies Access Key to continue.")
|
18 |
st.stop()
|
|
|
20 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
21 |
ASSISTANT_ID = "asst_PJjxQftfz2IJUUMvnldK58lB"
|
22 |
|
23 |
+
# Initialize session state
|
24 |
if "messages" not in st.session_state:
|
25 |
st.session_state.messages = []
|
26 |
if "thread_id" not in st.session_state:
|
27 |
st.session_state.thread_id = None
|
28 |
|
29 |
+
# Button to clear chat
|
30 |
if st.button("Clear Chat", use_container_width=True):
|
31 |
st.session_state.messages = []
|
32 |
st.session_state.thread_id = None
|
33 |
st.rerun()
|
34 |
|
35 |
+
# Display message history
|
36 |
for message in st.session_state.messages:
|
37 |
role, content = message["role"], message["content"]
|
38 |
st.chat_message(role).write(content)
|
39 |
|
40 |
+
# Handle new user input
|
41 |
if prompt := st.chat_input():
|
42 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
43 |
st.chat_message("user").write(prompt)
|
44 |
|
45 |
try:
|
46 |
+
# Create thread if not yet created
|
47 |
if st.session_state.thread_id is None:
|
48 |
thread = client.beta.threads.create()
|
49 |
st.session_state.thread_id = thread.id
|
50 |
|
51 |
thread_id = st.session_state.thread_id
|
52 |
|
53 |
+
# Send message
|
54 |
client.beta.threads.messages.create(
|
55 |
thread_id=thread_id,
|
56 |
role="user",
|
|
|
65 |
|
66 |
# Poll until complete
|
67 |
while True:
|
68 |
+
run_status = client.beta.threads.runs.retrieve(
|
69 |
+
thread_id=thread_id,
|
70 |
+
run_id=run.id
|
71 |
+
)
|
72 |
if run_status.status == "completed":
|
73 |
break
|
74 |
time.sleep(1)
|
75 |
|
76 |
# Get assistant response
|
77 |
messages = client.beta.threads.messages.list(thread_id=thread_id)
|
78 |
+
assistant_message = None
|
79 |
for message in reversed(messages.data):
|
80 |
if message.role == "assistant":
|
81 |
assistant_message = message.content[0].text.value
|
82 |
break
|
83 |
|
84 |
+
# Display assistant message
|
85 |
st.chat_message("assistant").write(assistant_message)
|
86 |
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
87 |
|
|
|
90 |
if page_match:
|
91 |
page_number = page_match.group(1).zfill(3)
|
92 |
image_url = f"https://raw.githubusercontent.com/AndrewLORTech/surgical-pathology-manual/main/51940670-Manual-of-Surgical-Pathology-Third-Edition_1_page_{page_number}.png"
|
93 |
+
st.image(image_url, caption=f"Page {int(page_number)}", use_container_width=True)
|
94 |
|
95 |
except Exception as e:
|
96 |
st.error(f"Error: {str(e)}")
|