import streamlit as st import json import os import requests from bardapi import BardAsync # 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.") 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 = BardAsync(token=os.getenv("_BARD_API_KEY"), language=selected_language_code) TITLE = "Palm 2🌴 Chatbot" DESCRIPTION = """ Welcome to the Palm 2🌴 Chatbot interface! This app allows you to interact with the Palm 2🌴 Chatbot powered by the Bard API. You can: - Select a language for the chatbot. - Decide if you want the chatbot to interpret code. - Use a system prompt to guide the chatbot's responses. - Export code snippets to repl.it. - Choose to display images in the chatbot's responses. Simply type in your question or prompt in the chat input below and get a response from Palm 2🌴! """ # Streamlit UI st.title(TITLE) st.write(DESCRIPTION) # Prediction function async def predict(message): with st.status("Requesting Palm-2🌴..."): st.write("Requesting API...") response = await 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']) if response['code']: if exportToReplIt: with st.status("Exporting replit..."): fale = False try: url = bard.export_replit(code=response['code'],program_lang=response['program_lang'])['url'] except error: fale=True st.write('ERROR') if not fale: st.title('Export to repl.it') st.markdown(f'[link]({url})') if code_interpreter: try: exec(response['code']) except Exception as e: st.write(f"ERROR {e}...") st.session_state.messages.append({"role": "assistant", "content": response['content']})