salomonsky commited on
Commit
1922d6a
1 Parent(s): 3e02b14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -32
app.py CHANGED
@@ -1,41 +1,47 @@
1
- import streamlit as st
 
2
  from huggingface_hub import InferenceClient
 
3
  from PIL import Image
4
- import io
5
 
6
- # Initialize Hugging Face Inference Client
7
- hf_token = st.secrets["HF_TOKEN"] # Use your Hugging Face token
8
- client = InferenceClient(model="enhanceaiteam/Flux-uncensored", token=hf_token)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- # Streamlit UI
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
- uploaded_file = st.sidebar.file_uploader("Sube una imagen base", type=["jpg", "png"])
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
- if st.sidebar.button("Generar Imagen"):
23
- with st.spinner('Generando imagen...'):
24
- # Convert the uploaded image to bytes
25
- img_bytes = io.BytesIO()
26
- init_image.save(img_bytes, format="PNG")
27
- img_bytes = img_bytes.getvalue()
28
-
29
- # Call the API with the prompt and base image
30
- image = client.img2img(prompt=prompt, image=img_bytes) # Returns image bytes
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")