Upload 10 files
Browse files- .gitignore +179 -0
- .streamlit/secrets.toml.example +2 -0
- Inicio.py +21 -0
- README.md +25 -13
- pages/1_Texto a Imagen.py +39 -0
- pages/2_Editar Imagen.py +67 -0
- pages/3_Imagen a Imagen.py +66 -0
- pages/__pycache__/pagina.cpython-312.pyc +0 -0
- pages/media/icon.png +0 -0
- requirements.txt +4 -0
.gitignore
ADDED
@@ -0,0 +1,179 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Created by https://www.toptal.com/developers/gitignore/api/python
|
2 |
+
# Edit at https://www.toptal.com/developers/gitignore?templates=python
|
3 |
+
|
4 |
+
### Python ###
|
5 |
+
# Byte-compiled / optimized / DLL files
|
6 |
+
__pycache__/
|
7 |
+
*.py[cod]
|
8 |
+
*$py.class
|
9 |
+
|
10 |
+
# C extensions
|
11 |
+
*.so
|
12 |
+
|
13 |
+
# Distribution / packaging
|
14 |
+
.Python
|
15 |
+
build/
|
16 |
+
develop-eggs/
|
17 |
+
dist/
|
18 |
+
downloads/
|
19 |
+
eggs/
|
20 |
+
.eggs/
|
21 |
+
lib/
|
22 |
+
lib64/
|
23 |
+
parts/
|
24 |
+
sdist/
|
25 |
+
var/
|
26 |
+
wheels/
|
27 |
+
share/python-wheels/
|
28 |
+
*.egg-info/
|
29 |
+
.installed.cfg
|
30 |
+
*.egg
|
31 |
+
MANIFEST
|
32 |
+
|
33 |
+
# PyInstaller
|
34 |
+
# Usually these files are written by a python script from a template
|
35 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
36 |
+
*.manifest
|
37 |
+
*.spec
|
38 |
+
|
39 |
+
# Installer logs
|
40 |
+
pip-log.txt
|
41 |
+
pip-delete-this-directory.txt
|
42 |
+
|
43 |
+
# Unit test / coverage reports
|
44 |
+
htmlcov/
|
45 |
+
.tox/
|
46 |
+
.nox/
|
47 |
+
.coverage
|
48 |
+
.coverage.*
|
49 |
+
.cache
|
50 |
+
nosetests.xml
|
51 |
+
coverage.xml
|
52 |
+
*.cover
|
53 |
+
*.py,cover
|
54 |
+
.hypothesis/
|
55 |
+
.pytest_cache/
|
56 |
+
cover/
|
57 |
+
|
58 |
+
# Translations
|
59 |
+
*.mo
|
60 |
+
*.pot
|
61 |
+
|
62 |
+
# Django stuff:
|
63 |
+
*.log
|
64 |
+
local_settings.py
|
65 |
+
db.sqlite3
|
66 |
+
db.sqlite3-journal
|
67 |
+
|
68 |
+
# Flask stuff:
|
69 |
+
instance/
|
70 |
+
.webassets-cache
|
71 |
+
|
72 |
+
# Scrapy stuff:
|
73 |
+
.scrapy
|
74 |
+
|
75 |
+
# Sphinx documentation
|
76 |
+
docs/_build/
|
77 |
+
|
78 |
+
# PyBuilder
|
79 |
+
.pybuilder/
|
80 |
+
target/
|
81 |
+
|
82 |
+
# Jupyter Notebook
|
83 |
+
.ipynb_checkpoints
|
84 |
+
|
85 |
+
# IPython
|
86 |
+
profile_default/
|
87 |
+
ipython_config.py
|
88 |
+
|
89 |
+
# pyenv
|
90 |
+
# For a library or package, you might want to ignore these files since the code is
|
91 |
+
# intended to run in multiple environments; otherwise, check them in:
|
92 |
+
# .python-version
|
93 |
+
|
94 |
+
# pipenv
|
95 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
96 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
97 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
98 |
+
# install all needed dependencies.
|
99 |
+
#Pipfile.lock
|
100 |
+
|
101 |
+
# poetry
|
102 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
103 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
104 |
+
# commonly ignored for libraries.
|
105 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
106 |
+
#poetry.lock
|
107 |
+
|
108 |
+
# pdm
|
109 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
110 |
+
#pdm.lock
|
111 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
112 |
+
# in version control.
|
113 |
+
# https://pdm.fming.dev/#use-with-ide
|
114 |
+
.pdm.toml
|
115 |
+
|
116 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
117 |
+
__pypackages__/
|
118 |
+
|
119 |
+
# Celery stuff
|
120 |
+
celerybeat-schedule
|
121 |
+
celerybeat.pid
|
122 |
+
|
123 |
+
# SageMath parsed files
|
124 |
+
*.sage.py
|
125 |
+
|
126 |
+
# Environments
|
127 |
+
.env
|
128 |
+
.venv
|
129 |
+
env/
|
130 |
+
venv/
|
131 |
+
ENV/
|
132 |
+
env.bak/
|
133 |
+
venv.bak/
|
134 |
+
|
135 |
+
# Spyder project settings
|
136 |
+
.spyderproject
|
137 |
+
.spyproject
|
138 |
+
|
139 |
+
# Rope project settings
|
140 |
+
.ropeproject
|
141 |
+
|
142 |
+
# mkdocs documentation
|
143 |
+
/site
|
144 |
+
|
145 |
+
# mypy
|
146 |
+
.mypy_cache/
|
147 |
+
.dmypy.json
|
148 |
+
dmypy.json
|
149 |
+
|
150 |
+
# Pyre type checker
|
151 |
+
.pyre/
|
152 |
+
|
153 |
+
# pytype static type analyzer
|
154 |
+
.pytype/
|
155 |
+
|
156 |
+
# Cython debug symbols
|
157 |
+
cython_debug/
|
158 |
+
|
159 |
+
# PyCharm
|
160 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
161 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
162 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
163 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
164 |
+
#.idea/
|
165 |
+
|
166 |
+
### Python Patch ###
|
167 |
+
# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
|
168 |
+
poetry.toml
|
169 |
+
|
170 |
+
# ruff
|
171 |
+
.ruff_cache/
|
172 |
+
|
173 |
+
# LSP config files
|
174 |
+
pyrightconfig.json
|
175 |
+
|
176 |
+
# End of https://www.toptal.com/developers/gitignore/api/python
|
177 |
+
|
178 |
+
.env
|
179 |
+
.streamlit/secrets.toml
|
.streamlit/secrets.toml.example
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
CLOUDFLARE_ACCOUNT_ID = "Your account id"
|
2 |
+
CLOUDFLARE_API_TOKEN = "Your API key"
|
Inicio.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
st.set_page_config(
|
4 |
+
page_title="Cloudflare AI Playground",
|
5 |
+
page_icon="pages/media/icon.png"
|
6 |
+
)
|
7 |
+
|
8 |
+
"""
|
9 |
+
# Generacion de imagenes con IA
|
10 |
+
|
11 |
+
---
|
12 |
+
Generar de imagenes con AI mediante prompt
|
13 |
+
|
14 |
+
Edita de imagenes con IA
|
15 |
+
|
16 |
+
Genera imagenes apartir de otra imagen
|
17 |
+
|
18 |
+
----
|
19 |
+
"""
|
20 |
+
|
21 |
+
st.markdown("[sixthz](https://instagram.com/sixthxz) was here",unsafe_allow_html=True)
|
README.md
CHANGED
@@ -1,13 +1,25 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Generador texto a imagen usando los workers de cloudflare
|
2 |
+
|
3 |
+
## Instalacion
|
4 |
+
|
5 |
+
Copiar [.streamlit/secrets.toml.example](./.streamlit/secrets.toml.example) a `.streamlit/secrets.toml`.
|
6 |
+
|
7 |
+
```bash
|
8 |
+
python -m venv venv
|
9 |
+
source ./venv/bin/activate
|
10 |
+
python -m pip install -r requirements.txt
|
11 |
+
```
|
12 |
+
|
13 |
+
```cmd
|
14 |
+
python -m venv venv
|
15 |
+
venv\Scripts\activate
|
16 |
+
python -m pip install -r requirements.txt
|
17 |
+
```
|
18 |
+
|
19 |
+
## Correr
|
20 |
+
|
21 |
+
Correr app de Streamlit:
|
22 |
+
|
23 |
+
```bash
|
24 |
+
python -m streamlit run Inicio.py
|
25 |
+
```
|
pages/1_Texto a Imagen.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import streamlit as st
|
3 |
+
|
4 |
+
account_id = st.secrets["CLOUDFLARE_ACCOUNT_ID"]
|
5 |
+
api_token = st.secrets["CLOUDFLARE_API_TOKEN"]
|
6 |
+
|
7 |
+
st.set_page_config(
|
8 |
+
page_title="Texto a Imagen",
|
9 |
+
page_icon="media/icon.png"
|
10 |
+
)
|
11 |
+
|
12 |
+
" ## Texto a Imagen"
|
13 |
+
"""
|
14 |
+
---
|
15 |
+
"""
|
16 |
+
|
17 |
+
with st.form("text_to_image"):
|
18 |
+
model = st.selectbox(
|
19 |
+
"Elige un modelo",
|
20 |
+
options=(
|
21 |
+
"@cf/lykon/dreamshaper-8-lcm",
|
22 |
+
"@cf/bytedance/stable-diffusion-xl-lightning",
|
23 |
+
"@cf/stabilityai/stable-diffusion-xl-base-1.0",
|
24 |
+
),
|
25 |
+
)
|
26 |
+
prompt = st.text_area(label="Describe de manera detallada la imagen que deseas generar")
|
27 |
+
submitted = st.form_submit_button("Generar")
|
28 |
+
if submitted:
|
29 |
+
headers = {
|
30 |
+
"Authorization": f"Bearer {api_token}",
|
31 |
+
}
|
32 |
+
with st.spinner("Generando..."):
|
33 |
+
url = f"https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/run/{model}"
|
34 |
+
response = requests.post(
|
35 |
+
url,
|
36 |
+
headers=headers,
|
37 |
+
json={"prompt": prompt},
|
38 |
+
)
|
39 |
+
st.image(response.content, caption=prompt)
|
pages/2_Editar Imagen.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from base64 import b64encode
|
2 |
+
import io
|
3 |
+
|
4 |
+
from PIL import Image, ImageOps
|
5 |
+
import requests
|
6 |
+
import streamlit as st
|
7 |
+
from streamlit_drawable_canvas import st_canvas
|
8 |
+
|
9 |
+
api_token = st.secrets["CLOUDFLARE_API_TOKEN"]
|
10 |
+
account_id = st.secrets["CLOUDFLARE_ACCOUNT_ID"]
|
11 |
+
|
12 |
+
st.set_page_config(
|
13 |
+
page_title="Editar Imagen",
|
14 |
+
page_icon="media/icon.png"
|
15 |
+
)
|
16 |
+
|
17 |
+
" ## Editar imagen"
|
18 |
+
"""
|
19 |
+
---
|
20 |
+
"""
|
21 |
+
|
22 |
+
image_upload = st.file_uploader("Sube una imagen")
|
23 |
+
|
24 |
+
if not image_upload:
|
25 |
+
st.stop()
|
26 |
+
|
27 |
+
img = Image.open(image_upload)
|
28 |
+
img = ImageOps.contain(img, (600, 600))
|
29 |
+
masking_result = st_canvas(width=img.width, height=img.height, background_image=img)
|
30 |
+
|
31 |
+
def image_to_int_array(image, format="PNG"):
|
32 |
+
"""Current Workers AI REST API consumes an array of unsigned 8 bit integers"""
|
33 |
+
bytes = io.BytesIO()
|
34 |
+
image.save(bytes, format=format)
|
35 |
+
return list(bytes.getvalue())
|
36 |
+
|
37 |
+
if masking_result.image_data is not None:
|
38 |
+
# st.image(masking_result.image_data, caption="Your masking")
|
39 |
+
with st.form("Prompt"):
|
40 |
+
prompt = st.text_input(label="Que te gustaría reemplazar de esta imagen?")
|
41 |
+
submitted = st.form_submit_button("Generar")
|
42 |
+
if submitted:
|
43 |
+
model = "@cf/runwayml/stable-diffusion-v1-5-inpainting"
|
44 |
+
image_array = image_to_int_array(img)
|
45 |
+
# Reverse the order / Boolean array to mark the bits
|
46 |
+
mask = masking_result.image_data[:, :, -1] > 0
|
47 |
+
mask_image = Image.fromarray(mask)
|
48 |
+
mask_array = image_to_int_array(mask_image)
|
49 |
+
with st.spinner("Generando..."):
|
50 |
+
url = f"https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/run/{model}"
|
51 |
+
response = requests.post(
|
52 |
+
url,
|
53 |
+
headers={
|
54 |
+
"Authorization": f"Bearer {api_token}",
|
55 |
+
},
|
56 |
+
json={"prompt": prompt, "image": image_array, "mask": mask_array},
|
57 |
+
)
|
58 |
+
if response.ok:
|
59 |
+
st.image(response.content, caption=prompt)
|
60 |
+
else:
|
61 |
+
st.warning(f"Error {response.status_code}")
|
62 |
+
st.warning(response.reason)
|
63 |
+
st.warning(response.text)
|
64 |
+
f"Image Array is {len(image_array)} entries, first 10:"
|
65 |
+
st.code(image_array[:10])
|
66 |
+
f"Mask Array is {len(mask_array)} entries, first 10:"
|
67 |
+
st.code(mask_array[:10])
|
pages/3_Imagen a Imagen.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from base64 import b64encode
|
2 |
+
import io
|
3 |
+
|
4 |
+
from PIL import Image, ImageOps
|
5 |
+
import requests
|
6 |
+
import streamlit as st
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
+
api_token = st.secrets["CLOUDFLARE_API_TOKEN"]
|
10 |
+
account_id = st.secrets["CLOUDFLARE_ACCOUNT_ID"]
|
11 |
+
|
12 |
+
st.set_page_config(
|
13 |
+
page_title="Imagen a Imagen",
|
14 |
+
page_icon="media/icon.png"
|
15 |
+
)
|
16 |
+
|
17 |
+
" ## Imagen a imagen"
|
18 |
+
st.markdown("*Ajustando parametros del modelo para mejores resultados")
|
19 |
+
"""
|
20 |
+
---
|
21 |
+
"""
|
22 |
+
|
23 |
+
# Cargar la imagen
|
24 |
+
input_image = st.file_uploader("Sube una imagen")
|
25 |
+
if not input_image:
|
26 |
+
st.stop()
|
27 |
+
|
28 |
+
img = Image.open(input_image)
|
29 |
+
img = ImageOps.contain(img, (600, 600))
|
30 |
+
|
31 |
+
# Crear una máscara blanca que cubra toda la imagen
|
32 |
+
masking_result = np.ones((img.height, img.width), dtype=np.uint8) * 255 # Máscara blanca
|
33 |
+
|
34 |
+
def image_to_int_array(image, format="PNG"):
|
35 |
+
"""Convertir la imagen a un array de enteros."""
|
36 |
+
bytes = io.BytesIO()
|
37 |
+
image.save(bytes, format=format)
|
38 |
+
return list(bytes.getvalue())
|
39 |
+
|
40 |
+
with st.form("Prompt"):
|
41 |
+
prompt = st.text_input(label="Describe las modificaciones que quieres")
|
42 |
+
submitted = st.form_submit_button("Generar")
|
43 |
+
if submitted:
|
44 |
+
model = "@cf/runwayml/stable-diffusion-v1-5-img2img"
|
45 |
+
image_array = image_to_int_array(img)
|
46 |
+
|
47 |
+
# Crear la máscara a partir del masking_result
|
48 |
+
mask_array = image_to_int_array(Image.fromarray(masking_result))
|
49 |
+
|
50 |
+
with st.spinner("Generando..."):
|
51 |
+
url = f"https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/run/{model}"
|
52 |
+
response = requests.post(
|
53 |
+
url,
|
54 |
+
headers={
|
55 |
+
"Authorization": f"Bearer {api_token}",
|
56 |
+
},
|
57 |
+
json={"prompt": prompt, "image": image_array, "mask": mask_array, "strength": 0.65, "guidance": 15},
|
58 |
+
)
|
59 |
+
if response.ok:
|
60 |
+
st.image(response.content, caption=prompt)
|
61 |
+
else:
|
62 |
+
st.warning(f"Error {response.status_code}")
|
63 |
+
st.warning(response.reason)
|
64 |
+
st.warning(response.text)
|
65 |
+
st.code(image_array[:10])
|
66 |
+
st.code(mask_array[:10])
|
pages/__pycache__/pagina.cpython-312.pyc
ADDED
Binary file (2 kB). View file
|
|
pages/media/icon.png
ADDED
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
requests
|
2 |
+
streamlit
|
3 |
+
streamlit-drawable-canvas==0.8.0
|
4 |
+
watchdog
|