Surat96 commited on
Commit
c566cea
·
verified ·
1 Parent(s): de619ec

Update app.py

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