File size: 4,410 Bytes
6038be0 2cba708 56681c6 e235e30 56681c6 2cba708 56681c6 6743734 872c6a2 952a9fa b60058d 12b6845 6743734 2cba708 e235e30 56681c6 1bf8daa 56681c6 e5b0d64 2cba708 56681c6 e235e30 6743734 56681c6 e235e30 56681c6 6743734 12b6845 8348fe5 6743734 56681c6 2cba708 6743734 4fbaa37 6743734 2cba708 56681c6 6743734 8348fe5 56681c6 6743734 2f2c040 6743734 12b6845 2f2c040 12b6845 2f2c040 12b6845 2f2c040 56b843a 2f2c040 adf84bb 2f2c040 346b91a 2f2c040 6743734 2f2c040 346b91a 2f2c040 8348fe5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
import streamlit as st
import json
import os
import requests
from bardapi import Bard
# Load the GOOGLE_LANGUAGES_TO_CODES dictionary from lang.json
with open("lang.json", "r") as file:
GOOGLE_LANGUAGES_TO_CODES = json.load(file)
with st.sidebar:
# Add a selector in the sidebar using the dictionary's keys
selected_language_name = st.sidebar.selectbox("Select Language", list(GOOGLE_LANGUAGES_TO_CODES.keys()))
code_interpreter = st.sidebar.toggle("Code Interpreter", value=True)
system_prompt = st.sidebar.text_input("System prompt for code interpreter", value = "Rule 1: If a user requests a code snippet, provide only one that can run in a Streamlit app without requiring additional libraries. Don't use any type of inputs in streamlit app.")
useSystemPrompt = st.sidebar.toggle("Use System prompt", value=True)
exportToReplIt = st.sidebar.toggle("Export to repl.it", value=False)
showImages = st.sidebar.toggle("Show images", value=True)
# Retrieve the corresponding language code from the dictionary
selected_language_code = GOOGLE_LANGUAGES_TO_CODES[selected_language_name]
# Initialize Bard with the selected language code
bard = Bard(token=os.getenv("_BARD_API_KEY"), language=selected_language_code)
TITLE = "Palm 2π΄ Chatbot"
DESCRIPTION = """
Welcome to Palm 2π΄ Chatbot! Choose your language, interpret code, prompt the chatbot, export code to repl.it, and display images.
"""
# Streamlit UI
st.title(TITLE)
st.write(DESCRIPTION)
# Prediction function
def predict(message):
with st.status("Requesting Palm-2π΄..."):
st.write("Requesting API...")
response = bard.get_answer(message if not (code_interpreter and useSystemPrompt) else message + " . "+system_prompt)
st.write("Done...")
st.write("Checking images...")
if 'images' in response.keys() and showImages:
for i in response['images']:
st.image(i)
return response
# Display chat messages from history on app rerun
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
with st.chat_message(message["role"], avatar=("π§βπ»" if message["role"] == 'human' else 'π΄')):
st.markdown(message["content"])
# React to user input
if prompt := st.chat_input("Ask Palm 2 anything..."):
st.chat_message("human", avatar="π§βπ»").markdown(prompt)
st.session_state.messages.append({"role": "human", "content": prompt})
response = predict(prompt)
with st.chat_message("assistant", avatar='π΄'):
st.markdown(response['content'])
MAX_ATTEMPTS = 3 # Define a maximum number of attempts to execute the code
if response['code']:
if exportToReplIt:
with st.status("Exporting replit..."):
fail = False
try:
url = bard.export_replit(code=response['code'], program_lang=response['program_lang'])['url']
except Exception as error:
fail = True
st.write('ERROR exporting to repl.it')
if not fail:
st.title('Export to repl.it')
st.markdown(f'[link]({url})')
if code_interpreter:
current_attempt = 0
current_code = response['code']
while current_attempt < MAX_ATTEMPTS:
try:
exec(current_code)
st.success('Code run succesfully!', icon="β
")
break
except Exception as e:
st.error("Error running code...")
new_prompt = f"{prompt} . There was an error executing the code: {e}. The last code you provided was: ```{current_code}```. Can you provide a corrected version?"
new_response = predict(new_prompt)
with st.chat_message("assistant", avatar='π΄'):
st.markdown(new_response['content'])
current_code = new_response['code']
current_attempt += 1
if current_attempt == MAX_ATTEMPTS:
st.warning("Reached maximum attempts. Unable to execute the code successfully.")
st.session_state.messages.append({"role": "assistant", "content": response['content']})
|