Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -88,51 +88,36 @@ def page1():
|
|
| 88 |
st.sidebar.markdown("**Session-ID:**")
|
| 89 |
st.sidebar.markdown(st.session_state.session_id)
|
| 90 |
st.sidebar.divider()
|
| 91 |
-
st.title("Assitent-Name")
|
| 92 |
-
st.text("Beschreibung")
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
# Load the previously created assistant
|
| 97 |
-
st.session_state.assistant = openai.beta.assistants.retrieve(assistant_id)
|
| 98 |
-
|
| 99 |
-
# Create a new thread for this session
|
| 100 |
-
st.session_state.thread = client.beta.threads.create(
|
| 101 |
-
metadata={'session_id': st.session_state.session_id}
|
| 102 |
-
)
|
| 103 |
-
|
| 104 |
-
# If the run is completed, display the messages
|
| 105 |
-
elif hasattr(st.session_state.run, 'status') and st.session_state.run.status == "completed":
|
| 106 |
-
# Retrieve the list of messages
|
| 107 |
-
st.session_state.messages = client.beta.threads.messages.list(
|
| 108 |
-
thread_id=st.session_state.thread.id
|
| 109 |
-
)
|
| 110 |
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
for content_part in message.content:
|
| 117 |
-
if content_part.type == 'text': # For text responses
|
| 118 |
-
message_text = content_part.text.value
|
| 119 |
-
st.markdown(message_text)
|
| 120 |
-
elif hasattr(content_part, 'image_file') and content_part.image_file:
|
| 121 |
-
image = handle_and_display_image(content_part.image_file.file_id)
|
| 122 |
-
if image:
|
| 123 |
-
st.image(image, caption="Generated Image")
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
if st.button("Submit"):
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
|
|
|
|
|
|
| 131 |
# Add message to the thread
|
| 132 |
st.session_state.messages = client.beta.threads.messages.create(
|
| 133 |
thread_id=st.session_state.thread.id,
|
| 134 |
role="user",
|
| 135 |
-
content=
|
| 136 |
)
|
| 137 |
|
| 138 |
# Do a run to process the messages in the thread
|
|
@@ -140,10 +125,71 @@ def page1():
|
|
| 140 |
thread_id=st.session_state.thread.id,
|
| 141 |
assistant_id=st.session_state.assistant.id,
|
| 142 |
)
|
|
|
|
|
|
|
| 143 |
if st.session_state.retry_error < 3:
|
| 144 |
-
time.sleep(1)
|
| 145 |
st.rerun()
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
# Check if 'run' object has 'status' attribute
|
| 148 |
if hasattr(st.session_state.run, 'status'):
|
| 149 |
# Handle the 'running' status
|
|
@@ -176,5 +222,6 @@ def page1():
|
|
| 176 |
time.sleep(3)
|
| 177 |
st.rerun()
|
| 178 |
|
|
|
|
| 179 |
if __name__ == "__main__":
|
| 180 |
-
main()
|
|
|
|
| 88 |
st.sidebar.markdown("**Session-ID:**")
|
| 89 |
st.sidebar.markdown(st.session_state.session_id)
|
| 90 |
st.sidebar.divider()
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
# Create columns for layout
|
| 93 |
+
col1, col2 = st.columns([3, 1]) # Adjust the ratio to your liking
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
with col1:
|
| 96 |
+
st.title('Welcome to MediMetrics!')
|
| 97 |
+
st.subheader('powered by :orange[BinDoc GmbH] ')
|
| 98 |
|
| 99 |
+
with col2:
|
| 100 |
+
# Load and display the image in the right column
|
| 101 |
+
image = Image.open('BinDoc Logo (Quadratisch).png')
|
| 102 |
+
st.image(image, use_column_width='always')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
# Initialize chat history in session state if not exists
|
| 105 |
+
if 'chat_history' not in st.session_state:
|
| 106 |
+
st.session_state.chat_history = []
|
|
|
|
| 107 |
|
| 108 |
+
# Function to handle question submission
|
| 109 |
+
def submit_question(question):
|
| 110 |
+
# Add question to chat history
|
| 111 |
+
st.session_state.chat_history.append(("User", question))
|
| 112 |
+
process_question(question)
|
| 113 |
|
| 114 |
+
# Function to process the question
|
| 115 |
+
def process_question(question):
|
| 116 |
# Add message to the thread
|
| 117 |
st.session_state.messages = client.beta.threads.messages.create(
|
| 118 |
thread_id=st.session_state.thread.id,
|
| 119 |
role="user",
|
| 120 |
+
content=question
|
| 121 |
)
|
| 122 |
|
| 123 |
# Do a run to process the messages in the thread
|
|
|
|
| 125 |
thread_id=st.session_state.thread.id,
|
| 126 |
assistant_id=st.session_state.assistant.id,
|
| 127 |
)
|
| 128 |
+
|
| 129 |
+
# Wait and rerun logic
|
| 130 |
if st.session_state.retry_error < 3:
|
| 131 |
+
time.sleep(1) # Wait 1 second before checking run status
|
| 132 |
st.rerun()
|
| 133 |
|
| 134 |
+
# Example query buttons
|
| 135 |
+
col3, col4 = st.columns(2) # Create two columns for buttons
|
| 136 |
+
|
| 137 |
+
with col3:
|
| 138 |
+
if st.button("Plotte mir die Anzahl der Kliniken von 2010 bis 2020."):
|
| 139 |
+
submit_question("Plotte mir die Anzahl der Kliniken von 2010 bis 2020.")
|
| 140 |
+
if st.button("Erstelle mir ein Bar Chart über die Bettenanzahl zwischen 1994 und 2002."):
|
| 141 |
+
submit_question("Erstelle mir ein Bar Chart über die Bettenanzahl zwischen 1994 und 2002.")
|
| 142 |
+
|
| 143 |
+
with col4:
|
| 144 |
+
if st.button("Hat die Bettenanzahl zwischen 2018 und 2021 zu- oder abgenommen?"):
|
| 145 |
+
submit_question("Hat die Bettenanzahl zwischen 2018 und 2021 zu- oder abgenommen?")
|
| 146 |
+
if st.button("Wie hat sich die Fremdkapitalquote des Malteser Krankenhaus St. Franziskus-Hospital verändert?"):
|
| 147 |
+
submit_question("Wie hat sich die Fremdkapitalquote des Malteser Krankenhaus St. Franziskus-Hospital verändert?")
|
| 148 |
+
|
| 149 |
+
prompt = st.text_input("Wie kann ich dir helfen?")
|
| 150 |
+
|
| 151 |
+
if st.button("Submit"):
|
| 152 |
+
submit_question(prompt)
|
| 153 |
+
|
| 154 |
+
if "assistant" not in st.session_state:
|
| 155 |
+
openai.api_key = api_key
|
| 156 |
+
# Load the previously created assistant
|
| 157 |
+
st.session_state.assistant = openai.beta.assistants.retrieve(assistant_id)
|
| 158 |
+
|
| 159 |
+
# Create a new thread for this session
|
| 160 |
+
st.session_state.thread = client.beta.threads.create(
|
| 161 |
+
metadata={'session_id': st.session_state.session_id}
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
# Check if the run is completed and display the last assistant message
|
| 165 |
+
if hasattr(st.session_state.run, 'status') and st.session_state.run.status == "completed":
|
| 166 |
+
# Retrieve the list of messages
|
| 167 |
+
st.session_state.messages = client.beta.threads.messages.list(
|
| 168 |
+
thread_id=st.session_state.thread.id
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
# Find the last assistant message
|
| 172 |
+
last_assistant_message = next((message for message in reversed(st.session_state.messages.data) if message.role == "assistant"), None)
|
| 173 |
+
if last_assistant_message:
|
| 174 |
+
with st.chat_message('assistant'):
|
| 175 |
+
for content_part in last_assistant_message.content:
|
| 176 |
+
if content_part.type == 'text':
|
| 177 |
+
st.markdown(content_part.text.value)
|
| 178 |
+
elif hasattr(content_part, 'image_file') and content_part.image_file:
|
| 179 |
+
image = handle_and_display_image(content_part.image_file.file_id)
|
| 180 |
+
if image:
|
| 181 |
+
st.image(image, caption="Generated Image")
|
| 182 |
+
|
| 183 |
+
# Add assistant's response to chat history
|
| 184 |
+
st.session_state.chat_history.append(("Assistant", last_assistant_message.content[0].text.value))
|
| 185 |
+
|
| 186 |
+
# Display chat history
|
| 187 |
+
for role, message in st.session_state.chat_history:
|
| 188 |
+
if role == "User":
|
| 189 |
+
st.text(f"User: {message}")
|
| 190 |
+
elif role == "Assistant":
|
| 191 |
+
st.text(f"Assistant: {message}")
|
| 192 |
+
|
| 193 |
# Check if 'run' object has 'status' attribute
|
| 194 |
if hasattr(st.session_state.run, 'status'):
|
| 195 |
# Handle the 'running' status
|
|
|
|
| 222 |
time.sleep(3)
|
| 223 |
st.rerun()
|
| 224 |
|
| 225 |
+
|
| 226 |
if __name__ == "__main__":
|
| 227 |
+
main()
|