File size: 1,883 Bytes
1922d6a
 
6084ee7
1922d6a
6084ee7
667dfe4
1922d6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f84b19
1922d6a
3e02b14
 
1922d6a
 
9940a0d
1922d6a
 
3e02b14
 
1922d6a
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import base64
import io
from huggingface_hub import InferenceClient
import streamlit as st
from PIL import Image

# Configuraci贸n
hf_token = st.secrets["HF_TOKEN"]
client = InferenceClient(repo_id="black-forest-labs/FLUX.1-schnell", token=hf_token)

def get_image_result(prompt, init_image=None):
    if init_image:
        img_buffer = io.BytesIO()
        init_image.save(img_buffer, format="PNG")
        img_bytes = img_buffer.getvalue()
        encoded_image = base64.b64encode(img_bytes).decode('utf-8')
    else:
        encoded_image = None

    try:
        output = client.img2img(prompt=prompt, image=encoded_image, strength=0.75)
        img_data = base64.b64decode(output['generated_image'])
        return Image.open(io.BytesIO(img_data))
    except Exception as e:
        st.error(f"Error en la generaci贸n de la imagen: {str(e)}")
        return None

# Interfaz de Streamlit
st.title("Generaci贸n de Im谩genes img2img con Flux")
st.sidebar.header("Opciones de generaci贸n")
prompt = st.sidebar.text_input("Escribe tu prompt:", value="Una escena futurista en una ciudad verde")
uploaded_file = st.sidebar.file_uploader("Sube una imagen base (opcional)", type=["jpg", "png"])

if st.sidebar.button("Generar Imagen"):
    if uploaded_file:
        init_image = Image.open(uploaded_file).convert("RGB")
        st.image(init_image, caption="Imagen Base", use_column_width=True)
    else:
        init_image = None

    with st.spinner('Generando imagen...'):
        generated_image = get_image_result(prompt, init_image)
        if generated_image:
            st.image(generated_image, caption="Imagen Generada", use_column_width=True)
            img_bytes = io.BytesIO()
            generated_image.save(img_bytes, format="PNG")
            st.download_button(label="Descargar imagen", data=img_bytes.getvalue(), file_name="generated_image.png", mime="image/png")