david-oplatka commited on
Commit
0f27244
·
verified ·
1 Parent(s): a57f6eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -65
app.py CHANGED
@@ -3,7 +3,7 @@ from query import VectaraQuery
3
  import os
4
 
5
  import streamlit as st
6
- import streamlit_analytics2 as streamlit_analytics
7
  from PIL import Image
8
 
9
 
@@ -42,76 +42,76 @@ def launch_bot():
42
  st.set_page_config(page_title=cfg.title, layout="wide")
43
 
44
 
45
- with streamlit_analytics.track():
46
- # left side content
47
- with st.sidebar:
48
- image = Image.open('Vectara-logo.png')
49
- st.markdown(f"## Welcome to {cfg.title}\n\n"
50
- f"This demo uses Retrieval Augmented Generation to ask questions about {cfg.source_data_desc}\n\n")
51
-
52
- st.markdown("---")
53
- st.markdown(
54
- "## How this works?\n"
55
- "This app was built with [Vectara](https://vectara.com).\n"
56
- "Vectara's [Indexing API](https://docs.vectara.com/docs/api-reference/indexing-apis/indexing) was used to ingest the data into a Vectara corpus (or index).\n\n"
57
- "This app uses Vectara [Chat API](https://docs.vectara.com/docs/console-ui/vectara-chat-overview) to query the corpus and present the results to you, answering your question.\n\n"
58
- )
59
- st.markdown("---")
60
- st.image(image, width=250)
61
-
62
- st.markdown(f"<center> <h2> Vectara chat demo: {cfg.title} </h2> </center>", unsafe_allow_html=True)
63
- st.markdown(f"<center> <h4> {cfg.description} </h4> </center>", unsafe_allow_html=True)
64
-
65
-
66
- if "messages" not in st.session_state.keys():
67
- st.session_state.messages = [{"role": "assistant", "content": "How may I help you?"}]
68
-
69
-
70
- if cfg.examples:
71
- example_messages = [example.strip() for example in cfg.examples.split(",")]
72
- else:
73
- example_messages = []
74
 
75
- if len(example_messages) > 0:
76
- st.markdown("<h6>Queries To Try:</h6>", unsafe_allow_html=True)
77
- cols = st.columns(8)
78
-
79
- # Display chat messages
80
- for message in st.session_state.messages:
81
- with st.chat_message(message["role"]):
82
- st.write(message["content"])
 
 
 
 
83
 
84
- # User-provided prompt
85
- if prompt := st.chat_input():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  st.session_state.messages.append({"role": "user", "content": prompt})
87
  with st.chat_message("user"):
88
  st.write(prompt)
89
 
90
- # Example prompt
91
- for i, example in enumerate(example_messages):
92
- button_pressed = False
93
- with cols[i]:
94
- if st.button(example):
95
- prompt = example
96
- button_pressed = True
97
-
98
- if button_pressed:
99
- st.session_state.messages.append({"role": "user", "content": prompt})
100
- with st.chat_message("user"):
101
- st.write(prompt)
102
-
103
- # Generate a new response if last message is not from assistant
104
- if st.session_state.messages[-1]["role"] != "assistant":
105
- with st.chat_message("assistant"):
106
- if cfg.streaming:
107
- stream = generate_streaming_response(prompt)
108
- response = st.write_stream(stream)
109
- else:
110
- with st.spinner("Thinking..."):
111
- response = generate_response(prompt)
112
- st.write(response)
113
- message = {"role": "assistant", "content": response}
114
- st.session_state.messages.append(message)
115
 
116
  if __name__ == "__main__":
117
  launch_bot()
 
3
  import os
4
 
5
  import streamlit as st
6
+ import streamlit_analytics2
7
  from PIL import Image
8
 
9
 
 
42
  st.set_page_config(page_title=cfg.title, layout="wide")
43
 
44
 
45
+ # with streamlit_analytics.track():
46
+ # left side content
47
+ with st.sidebar:
48
+ image = Image.open('Vectara-logo.png')
49
+ st.markdown(f"## Welcome to {cfg.title}\n\n"
50
+ f"This demo uses Retrieval Augmented Generation to ask questions about {cfg.source_data_desc}\n\n")
51
+
52
+ st.markdown("---")
53
+ st.markdown(
54
+ "## How this works?\n"
55
+ "This app was built with [Vectara](https://vectara.com).\n"
56
+ "Vectara's [Indexing API](https://docs.vectara.com/docs/api-reference/indexing-apis/indexing) was used to ingest the data into a Vectara corpus (or index).\n\n"
57
+ "This app uses Vectara [Chat API](https://docs.vectara.com/docs/console-ui/vectara-chat-overview) to query the corpus and present the results to you, answering your question.\n\n"
58
+ )
59
+ st.markdown("---")
60
+ st.image(image, width=250)
61
+
62
+ st.markdown(f"<center> <h2> Vectara chat demo: {cfg.title} </h2> </center>", unsafe_allow_html=True)
63
+ st.markdown(f"<center> <h4> {cfg.description} </h4> </center>", unsafe_allow_html=True)
64
+
 
 
 
 
 
 
 
 
 
65
 
66
+ if "messages" not in st.session_state.keys():
67
+ st.session_state.messages = [{"role": "assistant", "content": "How may I help you?"}]
68
+
69
+
70
+ if cfg.examples:
71
+ example_messages = [example.strip() for example in cfg.examples.split(",")]
72
+ else:
73
+ example_messages = []
74
+
75
+ if len(example_messages) > 0:
76
+ st.markdown("<h6>Queries To Try:</h6>", unsafe_allow_html=True)
77
+ cols = st.columns(8)
78
 
79
+ # Display chat messages
80
+ for message in st.session_state.messages:
81
+ with st.chat_message(message["role"]):
82
+ st.write(message["content"])
83
+
84
+ # User-provided prompt
85
+ if prompt := st.chat_input():
86
+ st.session_state.messages.append({"role": "user", "content": prompt})
87
+ with st.chat_message("user"):
88
+ st.write(prompt)
89
+
90
+ # Example prompt
91
+ for i, example in enumerate(example_messages):
92
+ button_pressed = False
93
+ with cols[i]:
94
+ if st.button(example):
95
+ prompt = example
96
+ button_pressed = True
97
+
98
+ if button_pressed:
99
  st.session_state.messages.append({"role": "user", "content": prompt})
100
  with st.chat_message("user"):
101
  st.write(prompt)
102
 
103
+ # Generate a new response if last message is not from assistant
104
+ if st.session_state.messages[-1]["role"] != "assistant":
105
+ with st.chat_message("assistant"):
106
+ if cfg.streaming:
107
+ stream = generate_streaming_response(prompt)
108
+ response = st.write_stream(stream)
109
+ else:
110
+ with st.spinner("Thinking..."):
111
+ response = generate_response(prompt)
112
+ st.write(response)
113
+ message = {"role": "assistant", "content": response}
114
+ st.session_state.messages.append(message)
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
  if __name__ == "__main__":
117
  launch_bot()