Spaces:
Runtime error
Runtime error
Commit
•
03acf19
1
Parent(s):
1ec6eee
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dotenv import load_dotenv
|
2 |
+
load_dotenv()
|
3 |
+
|
4 |
+
import google.generativeai as genai
|
5 |
+
import os
|
6 |
+
import streamlit as st
|
7 |
+
|
8 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
9 |
+
|
10 |
+
#call gemini model, receive response
|
11 |
+
model = genai.GenerativeModel("gemini-pro")
|
12 |
+
chat = model.start_chat(history=[])
|
13 |
+
|
14 |
+
#this function gives back response to question asked
|
15 |
+
|
16 |
+
def gemini_response(question):
|
17 |
+
response = chat.send_message(question, stream=True)
|
18 |
+
return response
|
19 |
+
|
20 |
+
#Streamlit Part (initialization app)
|
21 |
+
|
22 |
+
st.set_page_config(page_title = "Conversational Gemini ChatBot")
|
23 |
+
st.header("Powered by Google Gemini API")
|
24 |
+
|
25 |
+
# using session state of streamlit for chat history
|
26 |
+
#initially it is empty, later when we have conversations it will be stored in this session state
|
27 |
+
#name of session state is chat history
|
28 |
+
|
29 |
+
if 'chat_history' not in st.session_state:
|
30 |
+
st.session_state['chat_history'] = []
|
31 |
+
|
32 |
+
input = st.text_input("Input: ", key=input)
|
33 |
+
submit = st.button("Ask Question!")
|
34 |
+
|
35 |
+
if input and submit:
|
36 |
+
response = gemini_response(input)
|
37 |
+
|
38 |
+
st.session_state['chat_history'].append(("You", input))
|
39 |
+
st.subheader("The Response is...")
|
40 |
+
for chunk in response:
|
41 |
+
st.write(chunk.text)
|
42 |
+
st.session_state['chat_history'].append(("ChatBot", chunk))
|
43 |
+
|
44 |
+
st.subheader("The History is...")
|
45 |
+
|
46 |
+
for role, text in st.session_state['chat_history']:
|
47 |
+
st.write(f"{role}: {text}")
|