Skip to content

Commit 9dc56cc

Browse files
committed
Make app asynchronous on the main request
1 parent e2a9a7a commit 9dc56cc

File tree

2 files changed

+21
-47
lines changed

2 files changed

+21
-47
lines changed

app/app.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

streamlit_app/root_page.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import asyncio
12
from io import BytesIO
2-
import requests
3-
import streamlit as st
3+
from loguru import logger
4+
import nest_asyncio
45
import plotly.graph_objects as go
56
import plotly.express as px
6-
import numpy as np
7-
import igraph as ig
7+
import requests
8+
import streamlit as st
89

910
from app.pipeline import process_pipeline
1011

@@ -14,6 +15,8 @@
1415
and side effects related to longevity research.
1516
"""
1617

18+
# Monkey patch Streamlit's internal event loop
19+
nest_asyncio.apply()
1720

1821
# Configure page
1922
st.set_page_config(
@@ -180,9 +183,13 @@ def plot_igraph_with_plotly(g, layout_algorithm="fr", width=800, height=800):
180183

181184
return fig
182185

186+
async def predict(query: str):
187+
logger.debug(f"Request: {query}")
188+
response = process_pipeline(query)
189+
logger.debug(f"Got response!")
190+
return response
183191

184192
def main():
185-
186193
st.markdown("""
187194
<style>
188195
.stApp {
@@ -254,7 +261,15 @@ def main():
254261
with col_right:
255262
st.image(get_image("http://nb3.me/public/labubu.png"), caption="You've been laboobed!")
256263

257-
response = process_pipeline(prompt)
264+
loop = asyncio.get_event_loop()
265+
with st.spinner("Predicting..."):
266+
try:
267+
response = loop.run_until_complete(
268+
predict(prompt)
269+
)
270+
logger.debug(response)
271+
except Exception as e:
272+
st.error(f"An error occurred: {e}")
258273
st.session_state.messages.append({"role": "assistant", "content": response['text']})
259274

260275
if response['graph'] is not None:

0 commit comments

Comments
 (0)