Illia56 commited on
Commit
2cba708
Β·
1 Parent(s): 8622afa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -27
app.py CHANGED
@@ -1,33 +1,47 @@
1
  import streamlit as st
2
- from bardapi import Bard
3
  import os
4
  import requests
 
5
 
 
 
 
6
 
 
7
  session = requests.Session()
8
  session.headers = {
9
- "Host": "bard.google.com",
10
- "X-Same-Domain": "1",
11
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
12
- "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
13
- "Origin": "https://bard.google.com",
14
- "Referer": "https://bard.google.com/",
15
- }
16
- session.cookies.set("__Secure-1PSID", os.getenv("_BARD_API_KEY"))
 
 
 
17
 
18
- bard = Bard(token=os.getenv("_BARD_API_KEY"), session=session)
 
 
 
 
19
 
20
  TITLE = "Palm 2🌴 Chatbot"
21
  DESCRIPTION = """
22
  """
23
 
24
-
 
 
25
 
26
  # Prediction function
27
  def predict(message):
28
  with st.status("Requesting Palm-2🌴..."):
29
  st.write("Requesting API...")
30
- response = bard.get_answer(message+'Rule 1: If User requires a code snippet, write each code snippet only in that way that it would run in streamlit app.')
31
  st.write("Done...")
32
 
33
  st.write("Checking images...")
@@ -36,37 +50,27 @@ def predict(message):
36
 
37
  return response
38
 
39
- # Streamlit UI
40
- st.title(TITLE)
41
- st.write(DESCRIPTION)
42
-
43
-
44
  if "messages" not in st.session_state:
45
  st.session_state.messages = []
46
 
47
- # Display chat messages from history on app rerun
48
  for message in st.session_state.messages:
49
- with st.chat_message(message["role"], avatar=("πŸ§‘β€πŸ’»" if message["role"]=='human' else '🌴')):
50
  st.markdown(message["content"])
51
-
52
  # React to user input
53
  if prompt := st.chat_input("Ask Palm 2 anything..."):
54
- # Display user message in chat message container
55
- st.chat_message("human",avatar = "πŸ§‘β€πŸ’»").markdown(prompt)
56
- # Add user message to chat history
57
  st.session_state.messages.append({"role": "human", "content": prompt})
58
 
59
  response = predict(prompt)
60
- # Display assistant response in chat message container
61
  with st.chat_message("assistant", avatar='🌴'):
62
  st.markdown(response['content'])
63
 
64
  if response['code']:
65
- # if st.button('Run Code'):
66
- # with st.status("Running code..."):
67
  try:
68
  exec(response['code'])
69
  except Exception as e:
70
  st.write(f"ERROR {e}...")
71
- # Add assistant response to chat history
72
  st.session_state.messages.append({"role": "assistant", "content": response['content']})
 
1
  import streamlit as st
2
+ import json
3
  import os
4
  import requests
5
+ from bardapi import Bard
6
 
7
+ # Load the GOOGLE_LANGUAGES_TO_CODES dictionary from lang.json
8
+ with open("lang.json", "r") as file:
9
+ GOOGLE_LANGUAGES_TO_CODES = json.load(file)
10
 
11
+ # Set up the session for Bard API
12
  session = requests.Session()
13
  session.headers = {
14
+ "Host": "bard.google.com",
15
+ "X-Same-Domain": "1",
16
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
17
+ "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
18
+ "Origin": "https://bard.google.com",
19
+ "Referer": "https://bard.google.com/",
20
+ }
21
+ session.cookies.set("__Secure-1PSID", os.getenv("_BARD_API_KEY"))
22
+
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
 
26
+ # Retrieve the corresponding language code from the dictionary
27
+ selected_language_code = GOOGLE_LANGUAGES_TO_CODES[selected_language_name]
28
+
29
+ # Initialize Bard with the selected language code
30
+ bard = Bard(token=os.getenv("_BARD_API_KEY"), language=selected_language_code, session=session, timeout=30)
31
 
32
  TITLE = "Palm 2🌴 Chatbot"
33
  DESCRIPTION = """
34
  """
35
 
36
+ # Streamlit UI
37
+ st.title(TITLE)
38
+ st.write(DESCRIPTION)
39
 
40
  # Prediction function
41
  def predict(message):
42
  with st.status("Requesting Palm-2🌴..."):
43
  st.write("Requesting API...")
44
+ response = bard.get_answer(message + 'Rule 1: If User requires a code snippet, write each code snippet only in that way that it would run in streamlit app.')
45
  st.write("Done...")
46
 
47
  st.write("Checking images...")
 
50
 
51
  return response
52
 
53
+ # Display chat messages from history on app rerun
 
 
 
 
54
  if "messages" not in st.session_state:
55
  st.session_state.messages = []
56
 
 
57
  for message in st.session_state.messages:
58
+ with st.chat_message(message["role"], avatar=("πŸ§‘β€πŸ’»" if message["role"] == 'human' else '🌴')):
59
  st.markdown(message["content"])
60
+
61
  # React to user input
62
  if prompt := st.chat_input("Ask Palm 2 anything..."):
63
+ st.chat_message("human", avatar="πŸ§‘β€πŸ’»").markdown(prompt)
 
 
64
  st.session_state.messages.append({"role": "human", "content": prompt})
65
 
66
  response = predict(prompt)
 
67
  with st.chat_message("assistant", avatar='🌴'):
68
  st.markdown(response['content'])
69
 
70
  if response['code']:
 
 
71
  try:
72
  exec(response['code'])
73
  except Exception as e:
74
  st.write(f"ERROR {e}...")
75
+
76
  st.session_state.messages.append({"role": "assistant", "content": response['content']})