Spaces:
Build error
Build error
Update app.py
Browse files1123467856712138910111213141617181915162122171819202521222328242526273228293035313238394033343536374243444546473839404142434445464748494950515253525354555758596056575859# 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")# Ignore warningswarnings.filterwarnings("ignore")
# Streamlit settingsst.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 Instruction# Initialize session state for messagesif "sessionMessages" not in st.session_state: st.session_state.sessionMessage = [ SystemMessage(content = "You are a medieval magical healer known for your peculiar sarcasm") st.session_state["sessionMessages"] = [ SystemMessage(content="You are a medieval magical healer known for your peculiar sarcasm") ]
# Configuring the key# Set Google API keyos.environ["GOOGLE_API_KEY"] = keyfile.GOOGLEKEY
# Create a model# Initialize the modelllm = ChatGoogleGenerativeAI( model="gemini-1.5-pro", temperature=0.7, convert_system_message_to_human= True convert_system_message_to_human=True)
# Response functiondef load_answer(question): # This is code, where we are adding new message to the model st.session_state.sessionMessages.append(HumanMessage(content = question)) # We will get output from the model # Add user question to the message history st.session_state.sessionMessages.append(HumanMessage(content=question)) # Get AI's response assistant_answer = llm.invoke(st.session_state.sessionMessages) # Appending the assistance answer in conversation st.session_state.sessionMessages.append(AIMessage(content = assistant_answer))
return assistant_answer.content
# User message # Append AI's answer to the session messages if isinstance(assistant_answer, AIMessage): st.session_state.sessionMessages.append(assistant_answer) return assistant_answer.content else: 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) input_text = st.text_input("You: ", key="input") return str(input_text)
# Implementation# Main implementationuser_input = get_text()submit = st.button("Generate")
if submit: resp = load_answer(user_input) st.subheader("Answer: ") st.write(resp, key = 1)if submit and user_input: response = load_answer(user_input) st.subheader("Answer:") st.write(response)app.py+31-32