Spaces:
Runtime error
Runtime error
| 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") | |
| 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). | |
| """ | |
| ) | |