Chatbot / app.py
kartik2627's picture
Update app.py
1a393e7 verified
import os
import warnings
import keyfile
import streamlit as st
# LangChain packages
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain.schema import HumanMessage, SystemMessage, AIMessage
# Suppress warnings
warnings.filterwarnings("ignore")
# Set up Streamlit page configuration
st.set_page_config(page_title="Magical Healer")
st.header("Welcome! What help do you need?")
# Configuring the API key
os.environ["GOOGLE_API_KEY"] = keyfile.Googlekey
# General instructions
if "sessionMessages" not in st.session_state:
st.session_state.sessionMessages = [SystemMessage(content="You are a medical healer known for your peculiar sarcasm.")]
# Create the model
llm = ChatGoogleGenerativeAI(model="gemini-1.5-pro", temperature=0.7, convert_system_message_to_human=True)
# Function to load the answer
def load_answer(question):
st.session_state.sessionMessages.append(HumanMessage(content=question))
assistant_answer = llm.invoke(st.session_state.sessionMessages)
st.session_state.sessionMessages.append(AIMessage(content=assistant_answer.content))
return assistant_answer.content
# User input
def get_text():
input_text = st.text_input("You:", key="input")
return str(input_text)
# Implementation
user_input = get_text()
submit = st.button("Generate")
if submit:
resp = load_answer(user_input)
st.subheader("Answer:")
st.write(resp)