VishnuRamDebyez
commited on
Commit
•
184e73a
1
Parent(s):
14fdc24
Create chat_interface.py
Browse files- chat_interface.py +33 -0
chat_interface.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from api_utils import get_api_response
|
3 |
+
|
4 |
+
def display_chat_interface():
|
5 |
+
# Chat interface
|
6 |
+
for message in st.session_state.messages:
|
7 |
+
with st.chat_message(message["role"]):
|
8 |
+
st.markdown(message["content"])
|
9 |
+
|
10 |
+
if prompt := st.chat_input("Query:"):
|
11 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
12 |
+
with st.chat_message("user"):
|
13 |
+
st.markdown(prompt)
|
14 |
+
|
15 |
+
with st.spinner("Generating response..."):
|
16 |
+
response = get_api_response(prompt, st.session_state.session_id, st.session_state.model)
|
17 |
+
|
18 |
+
if response:
|
19 |
+
st.session_state.session_id = response.get('session_id')
|
20 |
+
st.session_state.messages.append({"role": "assistant", "content": response['answer']})
|
21 |
+
|
22 |
+
with st.chat_message("assistant"):
|
23 |
+
st.markdown(response['answer'])
|
24 |
+
|
25 |
+
with st.expander("Details"):
|
26 |
+
st.subheader("Generated Answer")
|
27 |
+
st.code(response['answer'])
|
28 |
+
st.subheader("Model Used")
|
29 |
+
st.code(response['model'])
|
30 |
+
st.subheader("Session ID")
|
31 |
+
st.code(response['session_id'])
|
32 |
+
else:
|
33 |
+
st.error("Failed to get a response from the API. Please try again.")
|