Update app.py
Browse files
app.py
CHANGED
@@ -19,23 +19,16 @@ session.headers = {
|
|
19 |
"Referer": "https://bard.google.com/",
|
20 |
}
|
21 |
session.cookies.set("__Secure-1PSID", os.getenv("_BARD_API_KEY"))
|
22 |
-
|
23 |
-
#
|
24 |
-
selected_language_name = st.sidebar.selectbox(
|
25 |
-
|
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,64 +40,46 @@ st.write(DESCRIPTION)
|
|
47 |
|
48 |
# Prediction function
|
49 |
def predict(message):
|
50 |
-
with st.
|
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 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
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 |
-
|
|
|
79 |
|
80 |
for message in st.session_state.messages:
|
81 |
-
chat_message
|
82 |
-
|
83 |
|
84 |
# React to user input
|
85 |
-
if prompt := st.
|
86 |
-
chat_message
|
87 |
-
|
88 |
|
89 |
response = predict(prompt)
|
90 |
-
chat_message
|
91 |
-
|
92 |
-
|
93 |
-
if response
|
94 |
-
with st.
|
95 |
url = bard.export_replit(
|
96 |
-
code=response[
|
97 |
-
|
98 |
-
|
99 |
-
st.
|
|
|
100 |
if code_interpreter:
|
101 |
try:
|
102 |
-
exec(response[
|
103 |
except Exception as e:
|
104 |
-
st.
|
105 |
-
|
106 |
-
#
|
107 |
-
st.session_state.messages.append(
|
108 |
-
{"role": "human", "content": prompt},
|
109 |
-
{"role": "assistant", "content": response["content"]},
|
110 |
-
)
|
|
|
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 |
|
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']})
|
|
|
|
|
|
|
|