app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,22 @@
|
|
1 |
import streamlit as st
|
2 |
from openai import OpenAI
|
|
|
3 |
|
4 |
-
st.set_page_config(page_title="Streamlit
|
5 |
|
6 |
st.title("Basic Chatbot with Streamlit and OpenAI Assistant")
|
7 |
st.caption("Features text streaming using OpenAI Assistants API")
|
8 |
|
9 |
-
#
|
10 |
with st.sidebar:
|
11 |
OPENAI_API_KEY = st.text_input("Enter your OpenAI API Key", type="password")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# Initialize OpenAI client
|
14 |
if OPENAI_API_KEY:
|
@@ -19,13 +27,17 @@ else:
|
|
19 |
|
20 |
ASSISTANT_ID = "asst_rd9h8PfYuOmHbkvOF3RTmVfn"
|
21 |
|
22 |
-
#
|
23 |
if "messages" not in st.session_state:
|
24 |
st.session_state["messages"] = []
|
25 |
|
26 |
-
# Display all chat messages
|
27 |
for message in st.session_state.messages:
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
|
30 |
# User input
|
31 |
if prompt := st.chat_input():
|
@@ -51,12 +63,19 @@ if prompt := st.chat_input():
|
|
51 |
run_status = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id)
|
52 |
if run_status.status == "completed":
|
53 |
break
|
|
|
54 |
|
55 |
messages = client.beta.threads.messages.list(thread_id=thread_id)
|
56 |
assistant_message = messages.data[0].content[0].text.value
|
57 |
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
60 |
|
61 |
except Exception as e:
|
62 |
-
st.error(f"Error: {str(e)}")
|
|
|
1 |
import streamlit as st
|
2 |
from openai import OpenAI
|
3 |
+
import time
|
4 |
|
5 |
+
st.set_page_config(page_title="Basic Chatbot with Streamlit and OpenAI Assistant")
|
6 |
|
7 |
st.title("Basic Chatbot with Streamlit and OpenAI Assistant")
|
8 |
st.caption("Features text streaming using OpenAI Assistants API")
|
9 |
|
10 |
+
# Sidebar for API Key input and file uploads
|
11 |
with st.sidebar:
|
12 |
OPENAI_API_KEY = st.text_input("Enter your OpenAI API Key", type="password")
|
13 |
+
st.subheader("Minutes Uploads")
|
14 |
+
uploaded_files = st.file_uploader("Upload meeting minutes (PDF/DOCX)", type=["pdf", "docx"], accept_multiple_files=True)
|
15 |
+
|
16 |
+
if uploaded_files:
|
17 |
+
st.write("### Uploaded Files:")
|
18 |
+
for uploaded_file in uploaded_files:
|
19 |
+
st.write(f"- {uploaded_file.name}")
|
20 |
|
21 |
# Initialize OpenAI client
|
22 |
if OPENAI_API_KEY:
|
|
|
27 |
|
28 |
ASSISTANT_ID = "asst_rd9h8PfYuOmHbkvOF3RTmVfn"
|
29 |
|
30 |
+
# Session state for chat messages
|
31 |
if "messages" not in st.session_state:
|
32 |
st.session_state["messages"] = []
|
33 |
|
34 |
+
# Display all chat messages
|
35 |
for message in st.session_state.messages:
|
36 |
+
role, content = message["role"], message["content"]
|
37 |
+
if role == "assistant" and "|" in content: # Detect table formatting
|
38 |
+
st.chat_message(role).write(content, unsafe_allow_html=True)
|
39 |
+
else:
|
40 |
+
st.chat_message(role).write(content)
|
41 |
|
42 |
# User input
|
43 |
if prompt := st.chat_input():
|
|
|
63 |
run_status = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id)
|
64 |
if run_status.status == "completed":
|
65 |
break
|
66 |
+
time.sleep(1)
|
67 |
|
68 |
messages = client.beta.threads.messages.list(thread_id=thread_id)
|
69 |
assistant_message = messages.data[0].content[0].text.value
|
70 |
|
71 |
+
# Detect table format and use Markdown
|
72 |
+
if "|" in assistant_message and "---" in assistant_message:
|
73 |
+
formatted_message = f"```\n{assistant_message}\n```"
|
74 |
+
st.chat_message("assistant").write(formatted_message, unsafe_allow_html=True)
|
75 |
+
else:
|
76 |
+
st.chat_message("assistant").write(assistant_message)
|
77 |
+
|
78 |
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
79 |
|
80 |
except Exception as e:
|
81 |
+
st.error(f"Error: {str(e)}")
|