Spaces:
Paused
Paused
Guy
commited on
Commit
•
d7c468c
1
Parent(s):
c34445a
added code
Browse files- app.py +39 -0
- requirements.txt +6 -0
- spirit_animal/__init__.py +0 -0
- spirit_animal/__pycache__/__init__.cpython-310.pyc +0 -0
- spirit_animal/pages/__init__.py +0 -0
- spirit_animal/pages/__pycache__/__init__.cpython-310.pyc +0 -0
- spirit_animal/pages/__pycache__/image_gen.cpython-310.pyc +0 -0
- spirit_animal/pages/__pycache__/intro.cpython-310.pyc +0 -0
- spirit_animal/pages/__pycache__/quiz.cpython-310.pyc +0 -0
- spirit_animal/pages/image_gen.py +48 -0
- spirit_animal/pages/intro.py +0 -0
- spirit_animal/pages/quiz.py +81 -0
- spirit_animal/utils/__init__.py +0 -0
- spirit_animal/utils/auth.py +18 -0
- spirit_animal/utils/logging.py +22 -0
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from spirit_animal.pages import intro, quiz, image_gen
|
3 |
+
|
4 |
+
|
5 |
+
# Define stateful objects
|
6 |
+
|
7 |
+
if "openai_model" not in st.session_state:
|
8 |
+
st.session_state["openai_model"] = "gpt-4"
|
9 |
+
|
10 |
+
if "spirit_animal" not in st.session_state:
|
11 |
+
st.session_state["spirit_animal"] = ""
|
12 |
+
|
13 |
+
# Define the pages
|
14 |
+
PAGES = {
|
15 |
+
"Intro": "intro",
|
16 |
+
"Spirit Animal Quiz": "quiz",
|
17 |
+
"Image Generator": "image_gen"
|
18 |
+
}
|
19 |
+
|
20 |
+
def intro():
|
21 |
+
st.title("Intro")
|
22 |
+
st.info("This is the intro page of the app.")
|
23 |
+
|
24 |
+
# Page functions dictionary
|
25 |
+
PAGE_FUNCTIONS = {
|
26 |
+
"intro": intro,
|
27 |
+
"quiz": quiz.quiz,
|
28 |
+
"image_gen": image_gen.image_gen
|
29 |
+
}
|
30 |
+
|
31 |
+
def main():
|
32 |
+
st.sidebar.title("Navigation")
|
33 |
+
choice = st.sidebar.selectbox("Go to", list(PAGES.keys()))
|
34 |
+
|
35 |
+
# Call the page function
|
36 |
+
PAGE_FUNCTIONS[PAGES[choice]]()
|
37 |
+
|
38 |
+
if __name__ == "__main__":
|
39 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
tiktoken
|
2 |
+
openai
|
3 |
+
python-dotenv==1.0.0
|
4 |
+
black
|
5 |
+
emoji
|
6 |
+
streamlit
|
spirit_animal/__init__.py
ADDED
File without changes
|
spirit_animal/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (155 Bytes). View file
|
|
spirit_animal/pages/__init__.py
ADDED
File without changes
|
spirit_animal/pages/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (161 Bytes). View file
|
|
spirit_animal/pages/__pycache__/image_gen.cpython-310.pyc
ADDED
Binary file (1.49 kB). View file
|
|
spirit_animal/pages/__pycache__/intro.cpython-310.pyc
ADDED
Binary file (158 Bytes). View file
|
|
spirit_animal/pages/__pycache__/quiz.cpython-310.pyc
ADDED
Binary file (2.38 kB). View file
|
|
spirit_animal/pages/image_gen.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Note: DALL-E 3 requires version 1.0.0 of the openai-python library or later
|
2 |
+
import os
|
3 |
+
from openai import AzureOpenAI
|
4 |
+
import json
|
5 |
+
import streamlit as st
|
6 |
+
|
7 |
+
client = AzureOpenAI(
|
8 |
+
api_version="2023-12-01-preview",
|
9 |
+
azure_endpoint=os.environ["DALLE_ENDPOINT"],
|
10 |
+
api_key=os.environ["AZURE_OPENAI_API_KEY"],
|
11 |
+
)
|
12 |
+
|
13 |
+
def get_image_url(prompt):
|
14 |
+
result = client.images.generate(
|
15 |
+
model="dall-e-3", # the name of your DALL-E 3 deployment
|
16 |
+
prompt=prompt,
|
17 |
+
n=1,
|
18 |
+
quality="standard",
|
19 |
+
size="1024x1024",
|
20 |
+
)
|
21 |
+
return json.loads(result.model_dump_json())['data'][0]['url']
|
22 |
+
|
23 |
+
def image_gen():
|
24 |
+
|
25 |
+
st.title("Image Generator")
|
26 |
+
if st.session_state["spirit_animal"] != "":
|
27 |
+
st.write(f"Looks like you're a {st.session_state['spirit_animal']}!")
|
28 |
+
|
29 |
+
col1, col2, col3 = st.columns(3)
|
30 |
+
|
31 |
+
# Create the 3 dropdowns for the user to select the environment, style, and bonus_style
|
32 |
+
environment = col1.selectbox("Environment", ["urban", "forest", "mountains", "coastal"])
|
33 |
+
style = col2.selectbox("Style", ["cartoon", "anime", "realistic", "cubism", "surrealism"])
|
34 |
+
bonus_style = col3.selectbox("Bonus style", ["cyberpunk", "steampunk", "geometric"])
|
35 |
+
|
36 |
+
# Create the generate button for the user to click
|
37 |
+
generate = st.button("Generate")
|
38 |
+
|
39 |
+
# If the user clicks the generate button
|
40 |
+
if generate:
|
41 |
+
# Create a prompt for the graphic_art tool using the user's selections and input
|
42 |
+
art_prompt = f"A picture of a {st.session_state['spirit_animal']} in a {environment} environment with the following styles {style}, {bonus_style}."
|
43 |
+
st.write(art_prompt)
|
44 |
+
# Call the graphic_art tool with the art_prompt
|
45 |
+
image_url = get_image_url(art_prompt)
|
46 |
+
# Display the image output from the graphic_art tool
|
47 |
+
st.image(image_url)
|
48 |
+
|
spirit_animal/pages/intro.py
ADDED
File without changes
|
spirit_animal/pages/quiz.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv, find_dotenv
|
3 |
+
import emoji
|
4 |
+
import streamlit as st
|
5 |
+
import openai
|
6 |
+
|
7 |
+
|
8 |
+
load_dotenv(find_dotenv())
|
9 |
+
|
10 |
+
use_azure_active_directory = False # Set this flag to True if you are using Azure Active Directory
|
11 |
+
|
12 |
+
if not use_azure_active_directory:
|
13 |
+
endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]
|
14 |
+
api_key = os.environ["AZURE_OPENAI_API_KEY"]
|
15 |
+
|
16 |
+
client = openai.AzureOpenAI(
|
17 |
+
azure_endpoint=endpoint,
|
18 |
+
api_key=api_key,
|
19 |
+
api_version="2023-09-01-preview"
|
20 |
+
)
|
21 |
+
|
22 |
+
client = openai.AzureOpenAI(api_key=os.getenv("AZURE_OPENAI_API_KEY"),
|
23 |
+
api_version=os.getenv("OPENAI_API_VERSION"))
|
24 |
+
|
25 |
+
# TODO: The 'openai.api_base' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(api_base=os.getenv("OPENAI_ENDPOINT"))'
|
26 |
+
# openai.api_base = os.getenv("OPENAI_ENDPOINT")
|
27 |
+
|
28 |
+
def quiz():
|
29 |
+
st.title("Find your spirit animal!")
|
30 |
+
st.info("Interact with our digital overlord to find out your spirit animal.")
|
31 |
+
|
32 |
+
if "messages" not in st.session_state:
|
33 |
+
st.session_state.messages = []
|
34 |
+
st.session_state.messages.append({
|
35 |
+
"role": "system",
|
36 |
+
"content": """Write a 2 question quiz to help the user find their spirit animal.
|
37 |
+
You should ask open ended questions, and only ask on question at a time, providing an affirmative response.
|
38 |
+
If a user's answer doesnt make sense, ask again.
|
39 |
+
Once you have the 5 answers respond with an appropriate spirit animal and an emoji for it where the emoji
|
40 |
+
is the last character. The animal should be the only emoji in the response."""
|
41 |
+
}
|
42 |
+
)
|
43 |
+
|
44 |
+
for message in st.session_state.messages:
|
45 |
+
if message["role"] != "system":
|
46 |
+
with st.chat_message(message["role"]):
|
47 |
+
st.markdown(message["content"])
|
48 |
+
|
49 |
+
if prompt := st.chat_input("Let's find out your spirit animal!"):
|
50 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
51 |
+
with st.chat_message("user"):
|
52 |
+
st.markdown(prompt)
|
53 |
+
|
54 |
+
with st.chat_message("assistant"):
|
55 |
+
|
56 |
+
message_placeholder = st.empty()
|
57 |
+
|
58 |
+
full_response = ""
|
59 |
+
for a in client.chat.completions.create(model=st.session_state["openai_model"],
|
60 |
+
messages=[
|
61 |
+
{"role": m["role"], "content": m["content"]}
|
62 |
+
for m in st.session_state.messages
|
63 |
+
],
|
64 |
+
stream=True):
|
65 |
+
if len(a.choices) == 0 :
|
66 |
+
continue
|
67 |
+
else:
|
68 |
+
full_response += (a.choices[0].delta.content or "")
|
69 |
+
message_placeholder.markdown(full_response + "▌")
|
70 |
+
|
71 |
+
|
72 |
+
message_placeholder.markdown(full_response)
|
73 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
74 |
+
try:
|
75 |
+
st.session_state["spirit_animal"] = list(emoji.analyze(full_response))[0].chars
|
76 |
+
print(st.session_state["spirit_animal"])
|
77 |
+
except:
|
78 |
+
pass
|
79 |
+
|
80 |
+
|
81 |
+
|
spirit_animal/utils/__init__.py
ADDED
File without changes
|
spirit_animal/utils/auth.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from azure.identity import DefaultAzureCredential
|
2 |
+
from spirit_animal.utils.logging import get_logger
|
3 |
+
|
4 |
+
logger = get_logger(__name__)
|
5 |
+
|
6 |
+
def get_default_az_cred():
|
7 |
+
"""
|
8 |
+
Returns a DefaultAzureCredential object that can be used to authenticate with Azure services.
|
9 |
+
If the credential cannot be obtained, an error is logged and an exception is raised.
|
10 |
+
"""
|
11 |
+
try:
|
12 |
+
credential = DefaultAzureCredential()
|
13 |
+
# Check if credential can get token successfully.
|
14 |
+
credential.get_token("https://management.azure.com/.default")
|
15 |
+
except Exception as ex:
|
16 |
+
logger.error("Unable to get a token from DefaultAzureCredential. Please run 'az login' in your terminal and try again.")
|
17 |
+
raise ex
|
18 |
+
return credential
|
spirit_animal/utils/logging.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import logging
|
3 |
+
|
4 |
+
# Global variable to cache the logging level
|
5 |
+
_cached_logging_level = None
|
6 |
+
|
7 |
+
|
8 |
+
def set_logging_params():
|
9 |
+
logging.basicConfig(format="%(asctime)s - %(levelname)s - %(name)s - %(message)s")
|
10 |
+
|
11 |
+
|
12 |
+
def get_logger(name: str) -> logging.Logger:
|
13 |
+
set_logging_params()
|
14 |
+
|
15 |
+
global _cached_logging_level
|
16 |
+
if _cached_logging_level is None:
|
17 |
+
_cached_logging_level = os.getenv("LOGGING_LEVEL", "INFO").upper()
|
18 |
+
|
19 |
+
logger = logging.getLogger(name)
|
20 |
+
logger.setLevel(_cached_logging_level)
|
21 |
+
|
22 |
+
return logger
|