Spaces:
Runtime error
Runtime error
louis030195
commited on
Commit
•
e5d3ffe
1
Parent(s):
ca8d91f
Add application file
Browse files- .gitignore +2 -0
- .vscode/settings.json +3 -0
- Makefile +15 -0
- app.py +132 -0
- requirements-test.txt +13 -0
- requirements.txt +3 -0
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
env
|
2 |
+
.env
|
.vscode/settings.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"python.formatting.provider": "black"
|
3 |
+
}
|
Makefile
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
push:
|
2 |
+
git add .
|
3 |
+
git commit -m "🙈"
|
4 |
+
git push
|
5 |
+
|
6 |
+
|
7 |
+
install: ## [DEVELOPMENT] Install the API dependencies
|
8 |
+
virtualenv env; \
|
9 |
+
source env/bin/activate; \
|
10 |
+
pip install -r requirements.txt; \
|
11 |
+
pip install -r requirements-test.txt
|
12 |
+
@echo "Done, run '\033[0;31msource env/bin/activate\033[0m' to activate the virtual environment"
|
13 |
+
|
14 |
+
run: ## [DEVELOPMENT] Run the streamlit app
|
15 |
+
streamlit run app.py
|
app.py
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import typing
|
2 |
+
import streamlit as st
|
3 |
+
import dotenv
|
4 |
+
|
5 |
+
dotenv.load_dotenv()
|
6 |
+
import openai
|
7 |
+
import os
|
8 |
+
import streamlit.components.v1 as components
|
9 |
+
from urllib.parse import quote
|
10 |
+
import requests
|
11 |
+
from tenacity import retry
|
12 |
+
from tenacity.wait import wait_exponential
|
13 |
+
from tenacity.before import before_log
|
14 |
+
from tenacity.after import after_log
|
15 |
+
from tenacity.stop import stop_after_attempt
|
16 |
+
|
17 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
18 |
+
|
19 |
+
|
20 |
+
@retry(
|
21 |
+
wait=wait_exponential(multiplier=1, min=1, max=3),
|
22 |
+
stop=stop_after_attempt(3),
|
23 |
+
)
|
24 |
+
def query_lexica(
|
25 |
+
inp: str
|
26 |
+
) -> typing.List[dict]:
|
27 |
+
"""
|
28 |
+
"""
|
29 |
+
url_encoded_user_input = quote(inp)
|
30 |
+
response = requests.get(
|
31 |
+
f"https://lexica.art/api/v1/search?q={url_encoded_user_input}&"
|
32 |
+
)
|
33 |
+
json_response = response.json()
|
34 |
+
return json_response
|
35 |
+
|
36 |
+
history = []
|
37 |
+
|
38 |
+
|
39 |
+
def chat(user_input: str) -> str:
|
40 |
+
history.append({"role": "user", "content": user_input})
|
41 |
+
response = openai.ChatCompletion.create(
|
42 |
+
model="gpt-3.5-turbo",
|
43 |
+
messages=[
|
44 |
+
{
|
45 |
+
"role": "system",
|
46 |
+
"content": "You are a helpful assistant that consummed LSD"
|
47 |
+
+ " and thus experience multimodal hallucinations."
|
48 |
+
+ " Your messages are accompanied by images generated by Stable Diffusion"
|
49 |
+
+ " and helps the user understand the message better.",
|
50 |
+
},
|
51 |
+
*[
|
52 |
+
{
|
53 |
+
"role": "assistant",
|
54 |
+
"content": h["content"],
|
55 |
+
}
|
56 |
+
for h in history[-5:]
|
57 |
+
],
|
58 |
+
{"role": "user", "content": user_input},
|
59 |
+
],
|
60 |
+
)
|
61 |
+
message = response.choices[0]["message"]
|
62 |
+
history.append(message)
|
63 |
+
return message["content"]
|
64 |
+
|
65 |
+
# eg not local dev
|
66 |
+
if not os.getenv("OPENAI_API_KEY"):
|
67 |
+
openai_key = st.text_input("Your OpenAI key")
|
68 |
+
openai.api_key = openai_key
|
69 |
+
user_input = st.text_input("You", "How can I reach maximum happiness this year?")
|
70 |
+
# button
|
71 |
+
if st.button("Send"):
|
72 |
+
# display clone response
|
73 |
+
lsdpt_response = chat(user_input)
|
74 |
+
lexica_response = query_lexica(user_input)
|
75 |
+
st.markdown(
|
76 |
+
f"""
|
77 |
+
LSD-PT
|
78 |
+
"""
|
79 |
+
)
|
80 |
+
st.markdown(
|
81 |
+
f"""
|
82 |
+
![{lexica_response['images'][0]['prompt']}]({lexica_response['images'][0]['src']})
|
83 |
+
"""
|
84 |
+
)
|
85 |
+
st.write(lsdpt_response)
|
86 |
+
st.markdown(
|
87 |
+
f"""
|
88 |
+
![{lexica_response['images'][1]['prompt']}]({lexica_response['images'][1]['src']})
|
89 |
+
"""
|
90 |
+
)
|
91 |
+
|
92 |
+
components.html(
|
93 |
+
"""
|
94 |
+
<script>
|
95 |
+
const doc = window.parent.document;
|
96 |
+
buttons = Array.from(doc.querySelectorAll('button[kind=primary]'));
|
97 |
+
const send = buttons.find(el => el.innerText === 'Send');
|
98 |
+
doc.addEventListener('keydown', function(e) {
|
99 |
+
switch (e.keyCode) {
|
100 |
+
case 13:
|
101 |
+
send.click();
|
102 |
+
break;
|
103 |
+
}
|
104 |
+
});
|
105 |
+
</script>
|
106 |
+
""",
|
107 |
+
height=0,
|
108 |
+
width=0,
|
109 |
+
)
|
110 |
+
|
111 |
+
|
112 |
+
st.markdown(
|
113 |
+
"""
|
114 |
+
TODO
|
115 |
+
"""
|
116 |
+
)
|
117 |
+
|
118 |
+
|
119 |
+
# add a reference to the source code at https://github.com/louis030195/paul-graham-clone
|
120 |
+
st.markdown(
|
121 |
+
"""
|
122 |
+
[Source code](https://github.com/louis030195/krishnamurti-clone)
|
123 |
+
"""
|
124 |
+
)
|
125 |
+
|
126 |
+
# reference to gpt-index
|
127 |
+
st.markdown(
|
128 |
+
"""
|
129 |
+
Built with ❤️ by [louis030195](https://louis030195.com) using the amazing [GPT-Index](https://github.com/jerryjliu/gpt_i
|
130 |
+
ndex) library
|
131 |
+
"""
|
132 |
+
)
|
requirements-test.txt
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
black
|
2 |
+
mypy
|
3 |
+
pylint
|
4 |
+
pytest-cov
|
5 |
+
pytest-xdist
|
6 |
+
pytest
|
7 |
+
ipykernel
|
8 |
+
ipywidgets
|
9 |
+
httpx
|
10 |
+
trio
|
11 |
+
pytest-asyncio
|
12 |
+
watchdog
|
13 |
+
python-dotenv
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
openai
|
3 |
+
tenacity
|