Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import os
|
| 4 |
+
import google.generativeai as genai
|
| 5 |
+
|
| 6 |
+
load_dotenv()
|
| 7 |
+
# loading all the environment variables:
|
| 8 |
+
genai.configure(api_key=os.environ["google_api_key"])
|
| 9 |
+
## creating a pipeline to load the model and take input and give the response in return:
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
model=genai.GenerativeModel('gemini-pro')
|
| 13 |
+
|
| 14 |
+
chat=model.start_chat(history=[])
|
| 15 |
+
|
| 16 |
+
def gemini_response(question):
|
| 17 |
+
response=chat.send_message(question,stream=True)
|
| 18 |
+
# stream is true means to keep the track of the previous messages:
|
| 19 |
+
return response
|
| 20 |
+
st.set_page_config(page_title="Gemini QnA ChatBot")
|
| 21 |
+
st.header("Gemini LLM Application")
|
| 22 |
+
#Initialize the session state for chat history if it doesn't exist
|
| 23 |
+
|
| 24 |
+
if 'chat_hisotry' not in st.session_state:
|
| 25 |
+
st.session_state['chat_history']=[]
|
| 26 |
+
input=st.text_input("Input",key='input')
|
| 27 |
+
submit=st.button("Ask the question")
|
| 28 |
+
if submit and input:
|
| 29 |
+
response=gemini_response(input)
|
| 30 |
+
# add user query and response to the session chat history
|
| 31 |
+
st.session_state['chat_history'].append(("You",input))
|
| 32 |
+
st.subheader("The Response is:")
|
| 33 |
+
for chunk in response:
|
| 34 |
+
st.write(chunk.text)
|
| 35 |
+
st.session_state['chat_history'].append(("Bot",chunk.text))
|
| 36 |
+
st.subheader("The Chat History is")
|
| 37 |
+
for role,text in st.session_state['chat_history']:
|
| 38 |
+
st.write(f"{role}:{text}")
|