File size: 2,945 Bytes
e166dfe
5dafacd
b27eba8
e166dfe
 
78451dd
964a38f
a84d7e6
e166dfe
 
 
5dafacd
e166dfe
acf2c5a
e166dfe
5dafacd
6b72774
964a38f
a7b3e91
9309e46
b27eba8
 
acf2c5a
b27eba8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be4849c
acf2c5a
 
9309e46
212a667
308e61b
9309e46
b9d5063
308e61b
5dafacd
41bff10
 
 
 
 
 
9309e46
 
78451dd
5dafacd
 
9309e46
 
e166dfe
 
5dafacd
e166dfe
 
9309e46
5dafacd
a7b3e91
f80a1cb
cfc9560
9309e46
5dc3a63
9309e46
 
 
 
6b72774
5dafacd
6b72774
 
5dafacd
6b72774
9309e46
 
6b72774
 
5dafacd
 
e166dfe
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import gradio as gr
import torch
import json
from diffusers import DiffusionPipeline
from datasets import load_dataset
from PIL import Image


# Configurações do dispositivo para uso apenas da CPU
device = "cpu"
model_repo_id = "stabilityai/sdxl-turbo"  # Continuando com o modelo especificado

# Carregar o pipeline configurado para CPU
pipe = DiffusionPipeline.from_pretrained(model_repo_id)
pipe = pipe.to(device)

# Carregando o dataset do Hugging Face
dataset = load_dataset("LEIDIA/Data_Womleimg")


# Carrega o dataset de imagens
dataset = load_dataset("LEIDIA/Data_Womleimg", split="train")

# Acessando as descrições
for example in dataset:
    image_id = example["image"].filename
    description = example["descricoes"]  # Acessando a nova feature
    print(f"ID da imagem: {image_id}, Descrição: {description}")

    # Cria um dicionário para mapear IDs de imagem às descrições
    descriptions = {item['image_id']: item['description'] for item in descricoes_json}

    # Adiciona as descrições ao dataset
    dataset = dataset.map(lambda example: {'description': descriptions.get(example['image'], '')})

    # Agora você pode acessar as descrições usando dataset[i]['description']

    # Imprime algumas amostras com as descrições
    for i in range(5):  # Imprime as primeiras 5 amostras
        example = dataset[i]
        print(f"Descrição: {example['description']}")
else:
    print("Dataset checksums not found, are not in the expected format, or empty. Skipping description loading.")


    
# Definir parâmetros padrão para geração rápida
DEFAULT_PROMPT = "A beautiful brunette woman wearing a blue leather pants "
DEFAULT_INFERENCE_STEPS = 6
IMAGE_WIDTH = 512
IMAGE_HEIGHT = 816
GUIDANCE_SCALE = 5.5

def resize_to_divisible_by_8(image):
    width, height = image.size
    new_width = width + (8 - width % 8) if width % 8 != 0 else width
    new_height = height + (8 - height % 8) if height % 8 != 0 else height
    return image.resize((new_width, new_height))

# Função simples para gerar imagem
def infer_simple(prompt):
    # Geração da imagem
    image = pipe(
        prompt=prompt,
        num_inference_steps=DEFAULT_INFERENCE_STEPS,
        guidance_scale=GUIDANCE_SCALE,
        height=IMAGE_HEIGHT,
        width=IMAGE_WIDTH,
    ).images[0]
    # Redimensionar a imagem para ser divisível por 8
    image = resize_to_divisible_by_8(image)
    return image

# Interface Gradio
with gr.Blocks() as demo:
    with gr.Row():
        gr.Markdown("## Text-to-Image Wom Test - Quick CPU Version")

    prompt = gr.Textbox(
        label="Prompt",
        value=DEFAULT_PROMPT,
        placeholder="Describe the image you want to generate",
    )

    generate_button = gr.Button("Generate")
    result = gr.Image(label="Generated Image")

    generate_button.click(
        fn=infer_simple,
        inputs=prompt,
        outputs=result,
    )

if __name__ == "__main__":
    demo.launch()