Update app.py
Browse files
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 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
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(
|
|
|
|
|
|
|
|
|
|
|
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.
|
44 |
st.write("Requesting API...")
|
45 |
-
response = bard.get_answer(
|
|
|
|
|
46 |
st.write("Done...")
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
# Display chat messages from history on app rerun
|
55 |
-
|
56 |
-
st.session_state.messages = []
|
57 |
|
58 |
for message in st.session_state.messages:
|
59 |
-
|
60 |
-
|
61 |
|
62 |
# React to user input
|
63 |
-
if prompt := st.
|
64 |
-
|
65 |
-
|
66 |
|
67 |
response = predict(prompt)
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
if response
|
72 |
-
with st.
|
73 |
url = bard.export_replit(
|
74 |
-
code=response[
|
75 |
-
|
76 |
-
|
77 |
-
st.
|
78 |
-
st.markdown(f'[link]({url})')
|
79 |
if code_interpreter:
|
80 |
try:
|
81 |
-
exec(response[
|
82 |
except Exception as e:
|
83 |
-
st.
|
84 |
-
|
85 |
-
#
|
|
|
|
|
|
|
|
|
|
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 |
+
)
|