File size: 2,407 Bytes
6038be0
56681c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57fce9f
56681c6
 
 
 
 
 
 
 
 
 
 
092c220
56681c6
23a5b76
56681c6
092c220
23a5b76
182e542
 
56681c6
 
 
 
 
 
4fbaa37
 
 
 
 
 
 
 
56681c6
 
 
 
 
 
 
 
 
 
182e542
 
 
b5e64b2
182e542
 
 
 
 
 
 
 
56681c6
 
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
import streamlit as st
from bardapi import Bard
import os
import requests


session = requests.Session()
session.headers = {
            "Host": "bard.google.com",
            "X-Same-Domain": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
            "Origin": "https://bard.google.com",
            "Referer": "https://bard.google.com/",
        }
session.cookies.set("__Secure-1PSID", os.getenv("_BARD_API_KEY")) 

bard = Bard(token=os.getenv("_BARD_API_KEY"), session=session)

TITLE = "Palm 2🌴 Chatbot"
DESCRIPTION = """
"""



# Prediction function
def predict(message):
    with st.status("Requesting Palm-2🌴..."):
        st.write("Requesting API...")
        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.')
        st.write("Done...")
        
        st.write("Checking images...")
    for i in response['images']:
        st.image(i)
    
    return response

# Streamlit UI
st.title(TITLE)
st.write(DESCRIPTION)


if "messages" not in st.session_state:
    st.session_state.messages = []

# Display chat messages from history on app rerun
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])
        
# React to user input
if prompt := st.chat_input("Ask Palm 2 anything..."):
    # Display user message in chat message container
    st.chat_message("human",avatar = "πŸ§‘β€πŸ’»").markdown(prompt)
    # Add user message to chat history
    st.session_state.messages.append({"role": "human", "content": prompt})

    response = predict(prompt)
    # Display assistant response in chat message container
    with st.chat_message("assistant", avatar='🌴'):
        st.markdown(response['content'])
    
    if response['code']:
        if st.button('Run Code'):
            with st.status("Running code..."):
                try:
                    exec(response['code'])
                except Exception as e:
                    st.write(f"ERROR {e}...")
                    
            
            
    # Add assistant response to chat history
    st.session_state.messages.append({"role": "assistant", "content": response})