Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 2 |
+
import os, spaces, streamlit as st
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
# api key config
|
| 7 |
+
load_dotenv()
|
| 8 |
+
os.environ["GOOGLE_API_KEY"] = os.getenv('GOOGLE_API_KEY')
|
| 9 |
+
# using gemini
|
| 10 |
+
llm_gemini = ChatGoogleGenerativeAI(model="gemini-pro")
|
| 11 |
+
# page config
|
| 12 |
+
st.set_page_config(
|
| 13 |
+
page_title="Chatbot",
|
| 14 |
+
page_icon="🤖"
|
| 15 |
+
)
|
| 16 |
+
# using session state for storing chat history
|
| 17 |
+
if 'chat_history' not in st.session_state:
|
| 18 |
+
st.session_state['chat_history'] = []
|
| 19 |
+
# other page configs
|
| 20 |
+
st.title("Basic Chatbot with History")
|
| 21 |
+
usr_text = st.text_input("Enter Query")
|
| 22 |
+
submit_button = st.button("Submit")
|
| 23 |
+
|
| 24 |
+
# action on user entering text and pressing Submit button
|
| 25 |
+
if submit_button and usr_text:
|
| 26 |
+
response = llm_gemini.invoke(usr_text)
|
| 27 |
+
st.subheader("Answer:")
|
| 28 |
+
st.write(response.content)
|
| 29 |
+
st.session_state.chat_history.append(f'User: {usr_text} \nAI: {response.content}')
|
| 30 |
+
|
| 31 |
+
# for displaying chat history
|
| 32 |
+
chat_history_text = "\n\n".join(st.session_state.chat_history)
|
| 33 |
+
st.text_area("**Chat History**", value=chat_history_text, height=300)
|
| 34 |
+
# button for clearing chat history
|
| 35 |
+
clr_btn = st.button("Clear Chat History")
|
| 36 |
+
if clr_btn:
|
| 37 |
+
st.session_state['chat_history']=[]
|