Spaces:
Running
Running
salomonsky
commited on
Commit
•
1922d6a
1
Parent(s):
3e02b14
Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,47 @@
|
|
1 |
-
import
|
|
|
2 |
from huggingface_hub import InferenceClient
|
|
|
3 |
from PIL import Image
|
4 |
-
import io
|
5 |
|
6 |
-
#
|
7 |
-
hf_token = st.secrets["HF_TOKEN"]
|
8 |
-
client = InferenceClient(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
# Streamlit
|
11 |
st.title("Generación de Imágenes img2img con Flux")
|
12 |
st.sidebar.header("Opciones de generación")
|
13 |
-
prompt = st.sidebar.text_input("Escribe tu prompt:", value="")
|
|
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
if uploaded_file is not None:
|
18 |
-
try:
|
19 |
init_image = Image.open(uploaded_file).convert("RGB")
|
20 |
st.image(init_image, caption="Imagen Base", use_column_width=True)
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
# Display the generated image
|
33 |
-
st.image(image, caption="Imagen Generada")
|
34 |
-
|
35 |
-
# Option to download the generated image
|
36 |
-
st.download_button(label="Descargar imagen", data=image, file_name="generated_image.png", mime="image/png")
|
37 |
-
|
38 |
-
except Exception as e:
|
39 |
-
st.error(f"Error en la generación de la imagen: {e}")
|
40 |
-
else:
|
41 |
-
st.warning("Por favor, sube una imagen base para continuar.")
|
|
|
1 |
+
import base64
|
2 |
+
import io
|
3 |
from huggingface_hub import InferenceClient
|
4 |
+
import streamlit as st
|
5 |
from PIL import Image
|
|
|
6 |
|
7 |
+
# Configuración
|
8 |
+
hf_token = st.secrets["HF_TOKEN"]
|
9 |
+
client = InferenceClient(repo_id="black-forest-labs/FLUX.1-schnell", token=hf_token)
|
10 |
+
|
11 |
+
def get_image_result(prompt, init_image=None):
|
12 |
+
if init_image:
|
13 |
+
img_buffer = io.BytesIO()
|
14 |
+
init_image.save(img_buffer, format="PNG")
|
15 |
+
img_bytes = img_buffer.getvalue()
|
16 |
+
encoded_image = base64.b64encode(img_bytes).decode('utf-8')
|
17 |
+
else:
|
18 |
+
encoded_image = None
|
19 |
+
|
20 |
+
try:
|
21 |
+
output = client.img2img(prompt=prompt, image=encoded_image, strength=0.75)
|
22 |
+
img_data = base64.b64decode(output['generated_image'])
|
23 |
+
return Image.open(io.BytesIO(img_data))
|
24 |
+
except Exception as e:
|
25 |
+
st.error(f"Error en la generación de la imagen: {str(e)}")
|
26 |
+
return None
|
27 |
|
28 |
+
# Interfaz de Streamlit
|
29 |
st.title("Generación de Imágenes img2img con Flux")
|
30 |
st.sidebar.header("Opciones de generación")
|
31 |
+
prompt = st.sidebar.text_input("Escribe tu prompt:", value="Una escena futurista en una ciudad verde")
|
32 |
+
uploaded_file = st.sidebar.file_uploader("Sube una imagen base (opcional)", type=["jpg", "png"])
|
33 |
|
34 |
+
if st.sidebar.button("Generar Imagen"):
|
35 |
+
if uploaded_file:
|
|
|
|
|
36 |
init_image = Image.open(uploaded_file).convert("RGB")
|
37 |
st.image(init_image, caption="Imagen Base", use_column_width=True)
|
38 |
+
else:
|
39 |
+
init_image = None
|
40 |
+
|
41 |
+
with st.spinner('Generando imagen...'):
|
42 |
+
generated_image = get_image_result(prompt, init_image)
|
43 |
+
if generated_image:
|
44 |
+
st.image(generated_image, caption="Imagen Generada", use_column_width=True)
|
45 |
+
img_bytes = io.BytesIO()
|
46 |
+
generated_image.save(img_bytes, format="PNG")
|
47 |
+
st.download_button(label="Descargar imagen", data=img_bytes.getvalue(), file_name="generated_image.png", mime="image/png")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|