Spaces:
Sleeping
Sleeping
Aditya Patkar
commited on
Commit
·
19e3f0f
1
Parent(s):
a553b07
Init
Browse files- .vscode/settings.json +16 -0
- __pycache__/constants.cpython-39.pyc +0 -0
- __pycache__/text_to_image.cpython-39.pyc +0 -0
- app.py +87 -0
- constants.py +5 -0
- requirements.txt +4 -0
- text_to_image.py +33 -0
.vscode/settings.json
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"python.linting.enabled": true,
|
3 |
+
"python.linting.pylintPath": "pylint",
|
4 |
+
"editor.formatOnSave": true,
|
5 |
+
"python.formatting.provider": "yapf", // or "black" here
|
6 |
+
"python.linting.pylintEnabled": true,
|
7 |
+
"python.linting.lintOnSave": true,
|
8 |
+
"python.formatting.yapfArgs": [
|
9 |
+
"--style={based_on_style: google, column_limit: 80, indent_width: 4}"
|
10 |
+
],
|
11 |
+
"[python]": {
|
12 |
+
"editor.defaultFormatter": "ms-python.black-formatter"
|
13 |
+
},
|
14 |
+
|
15 |
+
|
16 |
+
}
|
__pycache__/constants.cpython-39.pyc
ADDED
Binary file (282 Bytes). View file
|
|
__pycache__/text_to_image.cpython-39.pyc
ADDED
Binary file (1.09 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
This file is the main file of the project.
|
3 |
+
"""
|
4 |
+
|
5 |
+
# imports
|
6 |
+
import streamlit as st
|
7 |
+
from text_to_image import generate_image
|
8 |
+
|
9 |
+
|
10 |
+
def setup():
|
11 |
+
"""
|
12 |
+
Streamlit related setup. This has to be run for each page.
|
13 |
+
"""
|
14 |
+
|
15 |
+
# hide hamburger menu
|
16 |
+
hide_streamlit_style = """
|
17 |
+
|
18 |
+
<style>
|
19 |
+
#MainMenu {visibility: hidden;}
|
20 |
+
footer {visibility: hidden;}
|
21 |
+
</style>
|
22 |
+
"""
|
23 |
+
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
24 |
+
|
25 |
+
|
26 |
+
def main():
|
27 |
+
"""
|
28 |
+
Main function of the app.
|
29 |
+
"""
|
30 |
+
|
31 |
+
setup()
|
32 |
+
|
33 |
+
# title, subheader, and description
|
34 |
+
st.title("Text2Canvas")
|
35 |
+
|
36 |
+
st.subheader("A tool that demonstrates the power of Diffusion")
|
37 |
+
|
38 |
+
# horizontal line and line break
|
39 |
+
st.markdown("<hr>", unsafe_allow_html=True)
|
40 |
+
st.markdown("<br>", unsafe_allow_html=True)
|
41 |
+
|
42 |
+
# sidebar
|
43 |
+
st.sidebar.title("Navigation")
|
44 |
+
mode = st.sidebar.radio("Select a mode", ["Home", "Text2Image", "Feature2Sprite"])
|
45 |
+
# st.sidebar.write("Select a mode to get started")
|
46 |
+
|
47 |
+
# main content
|
48 |
+
|
49 |
+
if mode == "Home":
|
50 |
+
st.write(
|
51 |
+
"""
|
52 |
+
This tool is a demonstration of the power of Diffusion. It helps you generate images from text. There are two modes:
|
53 |
+
1. **Text2Image**: This mode generates high quality image based on a given prompt. It uses a pretrained model.
|
54 |
+
2. **Feature2Sprite**: This mode generates 16*16 images of sprites based on a combination of features. It uses a custom model trained on a dataset of sprites.
|
55 |
+
|
56 |
+
To get started, select a mode from the sidebar.
|
57 |
+
"""
|
58 |
+
)
|
59 |
+
|
60 |
+
elif mode == "Text2Image":
|
61 |
+
st.write(
|
62 |
+
"""
|
63 |
+
This mode generates high quality image based on a given prompt. It uses a pretrained model from huggingface.
|
64 |
+
"""
|
65 |
+
)
|
66 |
+
|
67 |
+
form = st.form(key="my_form")
|
68 |
+
|
69 |
+
prompt = form.text_input("Enter a prompt", value="A painting of a cat")
|
70 |
+
|
71 |
+
submit_button = form.form_submit_button(label="Generate")
|
72 |
+
|
73 |
+
if submit_button:
|
74 |
+
st.write("Generating image...")
|
75 |
+
image = generate_image(prompt)
|
76 |
+
st.image(image, caption="Generated Image", use_column_width=True)
|
77 |
+
|
78 |
+
elif mode == "Feature2Sprite":
|
79 |
+
st.write(
|
80 |
+
"""
|
81 |
+
This mode generates 16*16 images of sprites based on a combination of features. It uses a custom model trained on a dataset of sprites.
|
82 |
+
"""
|
83 |
+
)
|
84 |
+
|
85 |
+
|
86 |
+
if __name__ == "__main__":
|
87 |
+
main()
|
constants.py
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
This file contains all the constants used in the project.
|
3 |
+
"""
|
4 |
+
|
5 |
+
MODEL_ID = "stabilityai/stable-diffusion-2-1"
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
diffusers==0.19.3
|
2 |
+
streamlit==1.25.0
|
3 |
+
torch==2.0.1
|
4 |
+
transformers==4.31.0
|
text_to_image.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
This file contains the code for the text to image generation using the Stable Diffusion model.
|
3 |
+
"""
|
4 |
+
|
5 |
+
import torch
|
6 |
+
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
|
7 |
+
|
8 |
+
from constants import MODEL_ID
|
9 |
+
|
10 |
+
|
11 |
+
def generate_image(prompt: str) -> torch.Tensor:
|
12 |
+
"""
|
13 |
+
Generates an image from a given prompt.
|
14 |
+
|
15 |
+
Args:
|
16 |
+
prompt (str): The prompt to generate the image from.
|
17 |
+
|
18 |
+
Returns:
|
19 |
+
torch.Tensor: The generated image.
|
20 |
+
"""
|
21 |
+
# load model
|
22 |
+
|
23 |
+
pipe = StableDiffusionPipeline.from_pretrained(MODEL_ID, torch_dtype=torch.float16)
|
24 |
+
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
25 |
+
if torch.cuda.is_available():
|
26 |
+
pipe = pipe.to("cuda") # move model to GPU if available
|
27 |
+
print("Model loaded successfully and moved to GPU.")
|
28 |
+
else:
|
29 |
+
print("Model loaded successfully on CPU.")
|
30 |
+
|
31 |
+
image = pipe(prompt).images[0]
|
32 |
+
|
33 |
+
return image
|