File size: 3,053 Bytes
e5d3ffe
 
1b7d097
 
 
 
e5d3ffe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2142bad
e5d3ffe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b7d097
e5d3ffe
 
 
 
 
1b7d097
e5d3ffe
 
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import typing
import streamlit as st
try:
    import dotenv
except ImportError:
    pass

dotenv.load_dotenv()
import openai
import os
import streamlit.components.v1 as components
from urllib.parse import quote
import requests
from tenacity import retry
from tenacity.wait import wait_exponential
from tenacity.stop import stop_after_attempt

openai.api_key = os.getenv("OPENAI_API_KEY")


@retry(
    wait=wait_exponential(multiplier=1, min=1, max=3),
    stop=stop_after_attempt(3),
)
def query_lexica(
    inp: str
) -> typing.List[dict]:
    """
    """
    url_encoded_user_input = quote(inp)
    response = requests.get(
        f"https://lexica.art/api/v1/search?q={url_encoded_user_input}&"
    )
    json_response = response.json()
    return json_response

history = []


def chat(user_input: str) -> str:
    history.append({"role": "user", "content": user_input})
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {
                "role": "system",
                "content": "You are a helpful assistant that consummed LSD"
                + " and thus experience multimodal hallucinations."
                + " Your messages are accompanied by images generated by Stable Diffusion"
                + " and helps the user understand the message better.",
            },
            *[
                {
                    "role": "assistant",
                    "content": h["content"],
                }
                for h in history[-5:]
            ],
            {"role": "user", "content": user_input},
        ],
    )
    message = response.choices[0]["message"]
    history.append(message)
    return message["content"]

# eg not local dev
if not os.getenv("OPENAI_API_KEY"):
    openai_key = st.text_input("Your OpenAI key")
    openai.api_key = openai_key
user_input = st.text_input("You", "How can I reach maximum happiness?")
# button
if st.button("Send"):
    # display clone response
    lsdpt_response = chat(user_input)
    lexica_response = query_lexica(user_input)
    st.markdown(
        f"""
        LSD-PT
        """
    )
    st.markdown(
        f"""
        ![{lexica_response['images'][0]['prompt']}]({lexica_response['images'][0]['src']})
        """
    )
    st.write(lsdpt_response)
    st.markdown(
        f"""
        ![{lexica_response['images'][1]['prompt']}]({lexica_response['images'][1]['src']})
        """
    )

components.html(
    """
<script>
const doc = window.parent.document;
buttons = Array.from(doc.querySelectorAll('button[kind=primary]'));
const send = buttons.find(el => el.innerText === 'Send');
doc.addEventListener('keydown', function(e) {
    switch (e.keyCode) {
        case 13:
            send.click();
            break;
    }
});
</script>
""",
    height=0,
    width=0,
)


st.markdown(
    """
    TODO
    """
)


st.markdown(
    """
    [Source code](https://huggingface.co/spaces/louis030195/lsd-pt)
    """
)

st.markdown(
    """
    Built with ❤️ by [louis030195](https://louis030195.com).
    """
)