Create app_chat.py
Browse files- app_chat.py +65 -0
app_chat.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
def app_chat():
|
4 |
+
|
5 |
+
# Chat code (user input, agent responses, etc.)
|
6 |
+
if "messages" not in st.session_state:
|
7 |
+
st.session_state.messages = []
|
8 |
+
|
9 |
+
for message in st.session_state.messages:
|
10 |
+
with st.chat_message(message["role"]):
|
11 |
+
st.markdown(message["content"])
|
12 |
+
|
13 |
+
with st.chat_message("assistant"):
|
14 |
+
st.markdown("Hello there! How can I assist you today?")
|
15 |
+
|
16 |
+
if user_message := st.chat_input("Enter message"):
|
17 |
+
st.chat_message("user").markdown(user_message)
|
18 |
+
st.session_state.messages.append({"role": "user", "content": user_message})
|
19 |
+
|
20 |
+
selected_tools = [tool_loader.tools[idx] for idx, checkbox in enumerate(tool_checkboxes) if checkbox]
|
21 |
+
# Handle submission with the selected inference URL
|
22 |
+
response = handle_submission(user_message, selected_tools, url_endpoint)
|
23 |
+
|
24 |
+
with st.chat_message("assistant"):
|
25 |
+
if response is None:
|
26 |
+
st.warning("The agent's response is None. Please try again. Generate an image of a flying horse.")
|
27 |
+
elif isinstance(response, Image.Image):
|
28 |
+
st.image(response)
|
29 |
+
elif isinstance(response, AudioSegment):
|
30 |
+
st.audio(response)
|
31 |
+
elif isinstance(response, int):
|
32 |
+
st.markdown(response)
|
33 |
+
elif isinstance(response, str):
|
34 |
+
if "emojified_text" in response:
|
35 |
+
st.markdown(f"{response['emojified_text']}")
|
36 |
+
else:
|
37 |
+
st.markdown(response)
|
38 |
+
elif isinstance(response, list):
|
39 |
+
for item in response:
|
40 |
+
st.markdown(item) # Assuming the list contains strings
|
41 |
+
elif isinstance(response, pd.DataFrame):
|
42 |
+
st.dataframe(response)
|
43 |
+
elif isinstance(response, pd.Series):
|
44 |
+
st.table(response.iloc[0:10])
|
45 |
+
elif isinstance(response, dict):
|
46 |
+
st.json(response)
|
47 |
+
elif isinstance(response, st.graphics_altair.AltairChart):
|
48 |
+
st.altair_chart(response)
|
49 |
+
elif isinstance(response, st.graphics_bokeh.BokehChart):
|
50 |
+
st.bokeh_chart(response)
|
51 |
+
elif isinstance(response, st.graphics_graphviz.GraphvizChart):
|
52 |
+
st.graphviz_chart(response)
|
53 |
+
elif isinstance(response, st.graphics_plotly.PlotlyChart):
|
54 |
+
st.plotly_chart(response)
|
55 |
+
elif isinstance(response, st.graphics_pydeck.PydeckChart):
|
56 |
+
st.pydeck_chart(response)
|
57 |
+
elif isinstance(response, matplotlib.figure.Figure):
|
58 |
+
st.pyplot(response)
|
59 |
+
elif isinstance(response, streamlit.graphics_vega_lite.VegaLiteChart):
|
60 |
+
st.vega_lite_chart(response)
|
61 |
+
else:
|
62 |
+
st.warning("Unrecognized response type. Please try again. e.g. Generate an image of a flying horse.")
|
63 |
+
|
64 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
65 |
+
|