the-confused-coder's picture
Create app.py
b36da2d verified
raw
history blame
1.21 kB
from langchain_google_genai import ChatGoogleGenerativeAI
import os, spaces, streamlit as st
from dotenv import load_dotenv
# api key config
load_dotenv()
os.environ["GOOGLE_API_KEY"] = os.getenv('GOOGLE_API_KEY')
# using gemini
llm_gemini = ChatGoogleGenerativeAI(model="gemini-pro")
# page config
st.set_page_config(
page_title="Chatbot",
page_icon="πŸ€–"
)
# using session state for storing chat history
if 'chat_history' not in st.session_state:
st.session_state['chat_history'] = []
# other page configs
st.title("Basic Chatbot with History")
usr_text = st.text_input("Enter Query")
submit_button = st.button("Submit")
# action on user entering text and pressing Submit button
if submit_button and usr_text:
response = llm_gemini.invoke(usr_text)
st.subheader("Answer:")
st.write(response.content)
st.session_state.chat_history.append(f'User: {usr_text} \nAI: {response.content}')
# for displaying chat history
chat_history_text = "\n\n".join(st.session_state.chat_history)
st.text_area("**Chat History**", value=chat_history_text, height=300)
# button for clearing chat history
clr_btn = st.button("Clear Chat History")
if clr_btn:
st.session_state['chat_history']=[]