Spaces:
Build error
Build error
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960# Genericsimport osimport keyfileimport warningsimport streamlit as stwarnings.filterwarnings("ignore")# Langchain packagesfrom langchain_google_genai import ChatGoogleGenerativeAIfrom langchain.schema import HumanMessage, SystemMessage, AIMessage# First message that will pop on the screenst.set_page_config(page_title = "Magical Healer")st.header("Welcome, What help do you need?")# initializing the sessionMessagesif "sessionMessages" not in st.session_state:st.session_state["sessionMessages"] = []# General Instructionif "sessionMessages" not in st.session_state:st.session_state.sessionMessage = [SystemMessage(content = "You are a medieval magical healer known for your peculiar sarcasm")]# Configuring the keyos.environ["GOOGLE_API_KEY"] = keyfile.GOOGLEKEY# Create a modelllm = ChatGoogleGenerativeAI(model="gemini-1.5-pro",temperature=0.7,convert_system_message_to_human= True)# Response functiondef load_answer(question):# This is code, where we are adding new message to the modelst.session_state.sessionMessages.append(HumanMessage(content = question))# We will get output from the modelassistant_answer = llm.invoke(st.session_state.sessionMessages)# Appending the assistance answer in conversationst.session_state.sessionMessages.append(AIMessage(content = assistant_answer))return assistant_answer.content# User messagedef get_text():input_text = st.text_input("You: ", key = input)return str(input_text)# Implementationuser_input = get_text()submit = st.button("Generate")if submit:resp = load_answer(user_input)st.subheader("Answer: ")st.write(resp, key = 1)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859import osimport keyfileimport warningsimport streamlit as stfrom langchain_google_genai import ChatGoogleGenerativeAIfrom langchain.schema import HumanMessage, SystemMessage, AIMessage# Ignore warningswarnings.filterwarnings("ignore")# Streamlit settingsst.set_page_config(page_title="Magical Healer")st.header("Welcome, What help do you need?")# Initialize session state for messagesif "sessionMessages" not in st.session_state:st.session_state["sessionMessages"] = [SystemMessage(content="You are a medieval magical healer known for your peculiar sarcasm")]# Set Google API keyos.environ["GOOGLE_API_KEY"] = keyfile.GOOGLEKEY# Initialize the modelllm = ChatGoogleGenerativeAI(model="gemini-1.5-pro",temperature=0.7,convert_system_message_to_human=True)# Response functiondef load_answer(question):# Add user question to the message historyst.session_state.sessionMessages.append(HumanMessage(content=question))# Get AI's responseassistant_answer = llm.invoke(st.session_state.sessionMessages)# Append AI's answer to the session messagesif isinstance(assistant_answer, AIMessage):st.session_state.sessionMessages.append(assistant_answer)return assistant_answer.contentelse:st.session_state.sessionMessages.append(AIMessage(content=assistant_answer))return assistant_answer# Capture user inputdef get_text():input_text = st.text_input("You: ", key="input")return str(input_text)# Main implementationuser_input = get_text()submit = st.button("Generate")if submit and user_input:response = load_answer(user_input)st.subheader("Answer:")st.write(response)
app.py+31-32