archana2324 commited on
Commit
1b3009e
·
verified ·
1 Parent(s): b6e39c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -32
app.py CHANGED
@@ -1,60 +1,59 @@
1
- # Generics
2
  import os
3
  import keyfile
4
  import warnings
5
  import streamlit as st
6
- warnings.filterwarnings("ignore")
7
-
8
- # Langchain packages
9
  from langchain_google_genai import ChatGoogleGenerativeAI
10
  from langchain.schema import HumanMessage, SystemMessage, AIMessage
11
 
12
- # First message that will pop on the screen
13
- st.set_page_config(page_title = "Magical Healer")
 
 
 
14
  st.header("Welcome, What help do you need?")
15
 
16
- # initializing the sessionMessages
17
- if "sessionMessages" not in st.session_state:
18
- st.session_state["sessionMessages"] = []
19
- # General Instruction
20
  if "sessionMessages" not in st.session_state:
21
- st.session_state.sessionMessage = [
22
- SystemMessage(content = "You are a medieval magical healer known for your peculiar sarcasm")
23
  ]
24
 
25
- # Configuring the key
26
  os.environ["GOOGLE_API_KEY"] = keyfile.GOOGLEKEY
27
 
28
- # Create a model
29
  llm = ChatGoogleGenerativeAI(
30
  model="gemini-1.5-pro",
31
  temperature=0.7,
32
- convert_system_message_to_human= True
33
  )
34
 
35
-
36
  # Response function
37
  def load_answer(question):
38
- # This is code, where we are adding new message to the model
39
- st.session_state.sessionMessages.append(HumanMessage(content = question))
40
- # We will get output from the model
 
41
  assistant_answer = llm.invoke(st.session_state.sessionMessages)
42
- # Appending the assistance answer in conversation
43
- st.session_state.sessionMessages.append(AIMessage(content = assistant_answer))
44
-
45
- return assistant_answer.content
46
-
47
- # User message
 
 
 
 
48
  def get_text():
49
- input_text = st.text_input("You: ", key = input)
50
  return str(input_text)
51
 
52
-
53
- # Implementation
54
  user_input = get_text()
55
  submit = st.button("Generate")
56
 
57
- if submit:
58
- resp = load_answer(user_input)
59
- st.subheader("Answer: ")
60
- st.write(resp, key = 1)
 
 
1
  import os
2
  import keyfile
3
  import warnings
4
  import streamlit as st
 
 
 
5
  from langchain_google_genai import ChatGoogleGenerativeAI
6
  from langchain.schema import HumanMessage, SystemMessage, AIMessage
7
 
8
+ # Ignore warnings
9
+ warnings.filterwarnings("ignore")
10
+
11
+ # Streamlit settings
12
+ st.set_page_config(page_title="Magical Healer")
13
  st.header("Welcome, What help do you need?")
14
 
15
+ # Initialize session state for messages
 
 
 
16
  if "sessionMessages" not in st.session_state:
17
+ st.session_state["sessionMessages"] = [
18
+ SystemMessage(content="You are a medieval magical healer known for your peculiar sarcasm")
19
  ]
20
 
21
+ # Set Google API key
22
  os.environ["GOOGLE_API_KEY"] = keyfile.GOOGLEKEY
23
 
24
+ # Initialize the model
25
  llm = ChatGoogleGenerativeAI(
26
  model="gemini-1.5-pro",
27
  temperature=0.7,
28
+ convert_system_message_to_human=True
29
  )
30
 
 
31
  # Response function
32
  def load_answer(question):
33
+ # Add user question to the message history
34
+ st.session_state.sessionMessages.append(HumanMessage(content=question))
35
+
36
+ # Get AI's response
37
  assistant_answer = llm.invoke(st.session_state.sessionMessages)
38
+
39
+ # Append AI's answer to the session messages
40
+ if isinstance(assistant_answer, AIMessage):
41
+ st.session_state.sessionMessages.append(assistant_answer)
42
+ return assistant_answer.content
43
+ else:
44
+ st.session_state.sessionMessages.append(AIMessage(content=assistant_answer))
45
+ return assistant_answer
46
+
47
+ # Capture user input
48
  def get_text():
49
+ input_text = st.text_input("You: ", key="input")
50
  return str(input_text)
51
 
52
+ # Main implementation
 
53
  user_input = get_text()
54
  submit = st.button("Generate")
55
 
56
+ if submit and user_input:
57
+ response = load_answer(user_input)
58
+ st.subheader("Answer:")
59
+ st.write(response)