Illia56 commited on
Commit
befcec0
Β·
1 Parent(s): 3feaca0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -35
app.py CHANGED
@@ -19,16 +19,23 @@ session.headers = {
19
  "Referer": "https://bard.google.com/",
20
  }
21
  session.cookies.set("__Secure-1PSID", os.getenv("_BARD_API_KEY"))
22
- with st.sidebar:
23
- # Add a selector in the sidebar using the dictionary's keys
24
- selected_language_name = st.sidebar.selectbox("Select Language", list(GOOGLE_LANGUAGES_TO_CODES.keys()))
25
- code_interpreter = st.sidebar.checkbox("Code Interpreter", value=True)
26
-
 
 
27
  # Retrieve the corresponding language code from the dictionary
28
  selected_language_code = GOOGLE_LANGUAGES_TO_CODES[selected_language_name]
29
 
30
  # Initialize Bard with the selected language code
31
- bard = Bard(token=os.getenv("_BARD_API_KEY"), language=selected_language_code, session=session, timeout=30)
 
 
 
 
 
32
 
33
  TITLE = "Palm 2🌴 Chatbot"
34
  DESCRIPTION = """
@@ -40,46 +47,64 @@ st.write(DESCRIPTION)
40
 
41
  # Prediction function
42
  def predict(message):
43
- with st.status("Requesting Palm-2🌴..."):
44
  st.write("Requesting API...")
45
- response = bard.get_answer(message if not code_interpreter else message + "Rule 1: If User requires a code snippet, write each only one code snippet and only in that way that it would run in streamlit app, and but don't output anything if it requires some additional libraries.")
 
 
46
  st.write("Done...")
47
-
48
- st.write("Checking images...")
49
- for i in response['images']:
50
- st.image(i)
51
-
52
- return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  # Display chat messages from history on app rerun
55
- if "messages" not in st.session_state:
56
- st.session_state.messages = []
57
 
58
  for message in st.session_state.messages:
59
- with st.chat_message(message["role"], avatar=("πŸ§‘β€πŸ’»" if message["role"] == 'human' else '🌴')):
60
- st.markdown(message["content"])
61
 
62
  # React to user input
63
- if prompt := st.chat_input("Ask Palm 2 anything..."):
64
- st.chat_message("human", avatar="πŸ§‘β€πŸ’»").markdown(prompt)
65
- # st.session_state.messages.append({"role": "human", "content": prompt})
66
 
67
  response = predict(prompt)
68
- with st.chat_message("assistant", avatar='🌴'):
69
- st.markdown(response['content'])
70
-
71
- if response['code']:
72
- with st.status("Exporting replit..."):
73
  url = bard.export_replit(
74
- code=response['code'],
75
- program_lang=response['program_lang'],
76
- )['url']
77
- st.title('Export to repl.it')
78
- st.markdown(f'[link]({url})')
79
  if code_interpreter:
80
  try:
81
- exec(response['code'])
82
  except Exception as e:
83
- st.write(f"ERROR {e}...")
84
-
85
- # st.session_state.messages.append({"role": "assistant", "content": response['content']})
 
 
 
 
 
19
  "Referer": "https://bard.google.com/",
20
  }
21
  session.cookies.set("__Secure-1PSID", os.getenv("_BARD_API_KEY"))
22
+
23
+ # Set up the sidebar with language selection and code interpreter checkbox
24
+ selected_language_name = st.sidebar.selectbox(
25
+ "Select Language", list(GOOGLE_LANGUAGES_TO_CODES.keys()), index=0
26
+ )
27
+ code_interpreter = st.sidebar.checkbox("Code Interpreter", value=True)
28
+
29
  # Retrieve the corresponding language code from the dictionary
30
  selected_language_code = GOOGLE_LANGUAGES_TO_CODES[selected_language_name]
31
 
32
  # Initialize Bard with the selected language code
33
+ bard = Bard(
34
+ token=os.getenv("_BARD_API_KEY"),
35
+ language=selected_language_code,
36
+ session=session,
37
+ timeout=30,
38
+ )
39
 
40
  TITLE = "Palm 2🌴 Chatbot"
41
  DESCRIPTION = """
 
47
 
48
  # Prediction function
49
  def predict(message):
50
+ with st.spinner("Requesting Palm-2🌴..."):
51
  st.write("Requesting API...")
52
+ response = bard.get_answer(
53
+ message if not code_interpreter else message + " Rule 1: If User requires a code snippet, write only one code snippet that would run in the Streamlit app without requiring additional libraries."
54
+ )
55
  st.write("Done...")
56
+
57
+ if "images" in response:
58
+ st.write("Checking images...")
59
+ for i in response["images"]:
60
+ st.image(i)
61
+
62
+ return response
63
+
64
+ # Create a class to handle the chat messages
65
+ class ChatMessage:
66
+ def __init__(self, role, content):
67
+ self.role = role
68
+ self.content = content
69
+
70
+ def display(self):
71
+ if self.role == "human":
72
+ st.text(self.content)
73
+ else:
74
+ with st.echo():
75
+ exec(self.content)
76
 
77
  # Display chat messages from history on app rerun
78
+ st.session_state.messages = st.session_state.get("messages", [])
 
79
 
80
  for message in st.session_state.messages:
81
+ chat_message = ChatMessage(message["role"], message["content"])
82
+ chat_message.display()
83
 
84
  # React to user input
85
+ if prompt := st.text_input("Ask Palm 2 anything..."):
86
+ chat_message = ChatMessage("human", prompt)
87
+ chat_message.display()
88
 
89
  response = predict(prompt)
90
+ chat_message = ChatMessage("assistant", response["content"])
91
+ chat_message.display()
92
+
93
+ if response.get("code"):
94
+ with st.spinner("Exporting to repl.it..."):
95
  url = bard.export_replit(
96
+ code=response["code"], program_lang=response["program_lang"]
97
+ )["url"]
98
+ st.title("Export to repl.it")
99
+ st.markdown(f"[link]({url})")
 
100
  if code_interpreter:
101
  try:
102
+ exec(response["code"])
103
  except Exception as e:
104
+ st.error(f"Error: {e}")
105
+
106
+ # Append chat messages to the session state
107
+ st.session_state.messages.append(
108
+ {"role": "human", "content": prompt},
109
+ {"role": "assistant", "content": response["content"]},
110
+ )