Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,52 @@
|
|
| 1 |
-
# Q&A Chatbot with Gemini
|
| 2 |
-
import streamlit as st
|
| 3 |
-
import os, random, time
|
| 4 |
-
import google.generativeai as genai
|
| 5 |
-
from IPython.display import display
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
st.
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
st.
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
with st.chat_message("
|
| 31 |
-
|
| 32 |
-
message_placeholder.
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
except
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
| 1 |
+
# Q&A Chatbot with Gemini
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import os, random, time
|
| 4 |
+
import google.generativeai as genai
|
| 5 |
+
from IPython.display import display
|
| 6 |
+
|
| 7 |
+
genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
|
| 8 |
+
|
| 9 |
+
st.set_page_config(page_title="Q&A Demo")
|
| 10 |
+
st.header("🎊 SURAT Q&A ChatBot 🫰")
|
| 11 |
+
|
| 12 |
+
if "history" not in st.session_state:
|
| 13 |
+
st.session_state.history = []
|
| 14 |
+
|
| 15 |
+
model = genai.GenerativeModel('gemini-1.5-pro')
|
| 16 |
+
chat = model.start_chat(history = st.session_state.history)
|
| 17 |
+
|
| 18 |
+
with st.sidebar:
|
| 19 |
+
if st.button("Clear Chat Window", use_container_width=True, type="primary"):
|
| 20 |
+
st.session_state.history = []
|
| 21 |
+
st.rerun()
|
| 22 |
+
|
| 23 |
+
for message in chat.history:
|
| 24 |
+
role ="assistant" if message.role == 'model' else message.role
|
| 25 |
+
with st.chat_message(role):
|
| 26 |
+
st.markdown(message.parts[0].text)
|
| 27 |
+
|
| 28 |
+
if prompt := st.chat_input(""):
|
| 29 |
+
prompt = prompt.replace('\n', ' \n')
|
| 30 |
+
with st.chat_message("user"):st.markdown(prompt)
|
| 31 |
+
with st.chat_message("assistant"):
|
| 32 |
+
message_placeholder = st.empty()
|
| 33 |
+
message_placeholder.markdown("Thinking...")
|
| 34 |
+
try:
|
| 35 |
+
full_response = ""
|
| 36 |
+
for chunk in chat.send_message(prompt, stream=True):
|
| 37 |
+
word_count = 0
|
| 38 |
+
random_int = random.randint(5,10)
|
| 39 |
+
for word in chunk.text:
|
| 40 |
+
full_response+=word
|
| 41 |
+
word_count+=1
|
| 42 |
+
if word_count == random_int:
|
| 43 |
+
time.sleep(0.05)
|
| 44 |
+
message_placeholder.markdown(full_response + "_")
|
| 45 |
+
word_count = 0
|
| 46 |
+
random_int = random.randint(5,10)
|
| 47 |
+
message_placeholder.markdown(full_response)
|
| 48 |
+
except genai.types.generation_types.BlockedPromptException as e:st.exception(e)
|
| 49 |
+
except Exception as e:st.exception(e)
|
| 50 |
+
st.session_state.history = chat.history
|
| 51 |
+
|
| 52 |
+
|