Update app.py
Browse files
app.py
CHANGED
|
@@ -35,10 +35,11 @@ lora_models = {
|
|
| 35 |
},
|
| 36 |
"Nanda": {
|
| 37 |
"repo": "vcollos/Nanda",
|
| 38 |
-
"weights": "
|
| 39 |
"trigger_word": "A photo of Nanda, RAW photo, (hyperrealistic portrait:1.3) of a [man/woman], (detailed eyes:1.2), (skin texture:1.4), (natural lighting:1.1), (soft shadows:1.1), (intricate hair details:1.3), (film grain:0.8), (8k:1.2), (depth of field:1.1), (sharp focus:1.1),"
|
| 40 |
}
|
| 41 |
}
|
|
|
|
| 42 |
# Carrega os LoRAs
|
| 43 |
for name, details in lora_models.items():
|
| 44 |
try:
|
|
@@ -51,35 +52,40 @@ for name, details in lora_models.items():
|
|
| 51 |
MAX_SEED = 2**32 - 1
|
| 52 |
|
| 53 |
def upload_image_to_supabase(image, filename):
|
| 54 |
-
""" Faz upload da imagem
|
| 55 |
img_bytes = io.BytesIO()
|
| 56 |
image.save(img_bytes, format="PNG")
|
| 57 |
-
img_bytes.seek(0)
|
| 58 |
|
| 59 |
storage_path = f"images/{filename}"
|
| 60 |
-
response = supabase.storage.from_("images").upload(storage_path, img_bytes, {"content-type": "image/png"})
|
| 61 |
-
|
| 62 |
-
if response.get("error"):
|
| 63 |
-
raise Exception(f"Erro ao salvar no Supabase: {response['error']}")
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
@spaces.GPU(duration=80)
|
| 69 |
-
def run_lora(prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale_1, lora_scale_2,
|
| 70 |
-
# Define uma seed aleatória se necessário
|
| 71 |
if randomize_seed:
|
| 72 |
seed = random.randint(0, MAX_SEED)
|
| 73 |
generator = torch.Generator(device="cuda").manual_seed(seed)
|
| 74 |
|
| 75 |
-
# Aplica os
|
| 76 |
-
pipe.set_adapters([
|
| 77 |
|
| 78 |
-
# Adiciona
|
| 79 |
-
if
|
| 80 |
-
prompt = f"{lora_models[
|
| 81 |
|
| 82 |
-
# Gera a imagem
|
| 83 |
image = pipe(
|
| 84 |
prompt=prompt,
|
| 85 |
num_inference_steps=steps,
|
|
@@ -92,7 +98,6 @@ def run_lora(prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora
|
|
| 92 |
# Define um nome único para a imagem
|
| 93 |
filename = f"image_{seed}_{datetime.utcnow().strftime('%Y%m%d%H%M%S')}.png"
|
| 94 |
|
| 95 |
-
# Faz upload da imagem para o Supabase Storage
|
| 96 |
try:
|
| 97 |
image_url = upload_image_to_supabase(image, filename)
|
| 98 |
print(f"✅ Imagem salva no Supabase: {image_url}")
|
|
@@ -100,16 +105,17 @@ def run_lora(prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora
|
|
| 100 |
print(f"❌ Erro ao fazer upload da imagem: {e}")
|
| 101 |
image_url = None
|
| 102 |
|
| 103 |
-
# Salva os metadados no banco de dados Supabase
|
| 104 |
-
|
| 105 |
-
"
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
| 113 |
|
| 114 |
return image, seed
|
| 115 |
|
|
@@ -129,20 +135,18 @@ with gr.Blocks(theme=gr_theme) as app:
|
|
| 129 |
randomize_seed = gr.Checkbox(False, label="Randomize seed")
|
| 130 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=556215326)
|
| 131 |
|
| 132 |
-
#
|
| 133 |
-
lora_scale_1 = gr.Slider(label="LoRA Scale (
|
| 134 |
lora_scale_2 = gr.Slider(label="LoRA Scale (Nanda)", minimum=0, maximum=1, step=0.01, value=1)
|
| 135 |
|
| 136 |
-
selected_lora = gr.Dropdown(label="Selecionar LoRA", choices=["vgn", "Nanda"], value="Nanda")
|
| 137 |
-
|
| 138 |
with gr.Column(scale=2):
|
| 139 |
result = gr.Image(label="Generated Image")
|
| 140 |
-
gr.Markdown("Gere imagens usando
|
| 141 |
|
| 142 |
-
#
|
| 143 |
generate_button.click(
|
| 144 |
run_lora,
|
| 145 |
-
inputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale_1, lora_scale_2
|
| 146 |
outputs=[result, seed],
|
| 147 |
)
|
| 148 |
|
|
|
|
| 35 |
},
|
| 36 |
"Nanda": {
|
| 37 |
"repo": "vcollos/Nanda",
|
| 38 |
+
"weights": "nanda",
|
| 39 |
"trigger_word": "A photo of Nanda, RAW photo, (hyperrealistic portrait:1.3) of a [man/woman], (detailed eyes:1.2), (skin texture:1.4), (natural lighting:1.1), (soft shadows:1.1), (intricate hair details:1.3), (film grain:0.8), (8k:1.2), (depth of field:1.1), (sharp focus:1.1),"
|
| 40 |
}
|
| 41 |
}
|
| 42 |
+
|
| 43 |
# Carrega os LoRAs
|
| 44 |
for name, details in lora_models.items():
|
| 45 |
try:
|
|
|
|
| 52 |
MAX_SEED = 2**32 - 1
|
| 53 |
|
| 54 |
def upload_image_to_supabase(image, filename):
|
| 55 |
+
""" Faz upload da imagem para o Supabase Storage e retorna a URL pública. """
|
| 56 |
img_bytes = io.BytesIO()
|
| 57 |
image.save(img_bytes, format="PNG")
|
| 58 |
+
img_bytes.seek(0) # Move para o início do arquivo
|
| 59 |
|
| 60 |
storage_path = f"images/{filename}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
try:
|
| 63 |
+
# Agora passando os bytes corretamente para o upload
|
| 64 |
+
response = supabase.storage.from_("images").upload(storage_path, img_bytes.getvalue(), {"content-type": "image/png"})
|
| 65 |
+
|
| 66 |
+
if response.get("error"):
|
| 67 |
+
raise Exception(f"Erro ao salvar no Supabase: {response['error']}")
|
| 68 |
+
|
| 69 |
+
base_url = f"{url}/storage/v1/object/public/images"
|
| 70 |
+
return f"{base_url}/{filename}"
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print(f"❌ Erro no upload da imagem: {e}")
|
| 73 |
+
return None
|
| 74 |
|
| 75 |
@spaces.GPU(duration=80)
|
| 76 |
+
def run_lora(prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale_1, lora_scale_2, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
| 77 |
if randomize_seed:
|
| 78 |
seed = random.randint(0, MAX_SEED)
|
| 79 |
generator = torch.Generator(device="cuda").manual_seed(seed)
|
| 80 |
|
| 81 |
+
# Aplica os dois LoRAs combinados
|
| 82 |
+
pipe.set_adapters(["vgn", "Nanda"], adapter_weights=[lora_scale_1, lora_scale_2])
|
| 83 |
|
| 84 |
+
# Adiciona trigger words apenas se Nanda estiver ativado
|
| 85 |
+
if lora_scale_2 > 0:
|
| 86 |
+
prompt = f"{lora_models['Nanda']['trigger_word']} {prompt}"
|
| 87 |
|
| 88 |
+
# Gera a imagem
|
| 89 |
image = pipe(
|
| 90 |
prompt=prompt,
|
| 91 |
num_inference_steps=steps,
|
|
|
|
| 98 |
# Define um nome único para a imagem
|
| 99 |
filename = f"image_{seed}_{datetime.utcnow().strftime('%Y%m%d%H%M%S')}.png"
|
| 100 |
|
|
|
|
| 101 |
try:
|
| 102 |
image_url = upload_image_to_supabase(image, filename)
|
| 103 |
print(f"✅ Imagem salva no Supabase: {image_url}")
|
|
|
|
| 105 |
print(f"❌ Erro ao fazer upload da imagem: {e}")
|
| 106 |
image_url = None
|
| 107 |
|
| 108 |
+
# Salva os metadados no banco de dados Supabase apenas se `image_url` for válido
|
| 109 |
+
if image_url:
|
| 110 |
+
supabase.table("images").insert({
|
| 111 |
+
"prompt": prompt,
|
| 112 |
+
"cfg_scale": cfg_scale,
|
| 113 |
+
"steps": steps,
|
| 114 |
+
"seed": seed,
|
| 115 |
+
"lora_scale_1": lora_scale_1,
|
| 116 |
+
"lora_scale_2": lora_scale_2,
|
| 117 |
+
"image_url": image_url
|
| 118 |
+
}).execute()
|
| 119 |
|
| 120 |
return image, seed
|
| 121 |
|
|
|
|
| 135 |
randomize_seed = gr.Checkbox(False, label="Randomize seed")
|
| 136 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=556215326)
|
| 137 |
|
| 138 |
+
# Sliders para os pesos dos LoRAs
|
| 139 |
+
lora_scale_1 = gr.Slider(label="LoRA Scale (vgn)", minimum=0, maximum=1, step=0.01, value=0.5)
|
| 140 |
lora_scale_2 = gr.Slider(label="LoRA Scale (Nanda)", minimum=0, maximum=1, step=0.01, value=1)
|
| 141 |
|
|
|
|
|
|
|
| 142 |
with gr.Column(scale=2):
|
| 143 |
result = gr.Image(label="Generated Image")
|
| 144 |
+
gr.Markdown("Gere imagens usando Collos LoRA e um prompt de texto.")
|
| 145 |
|
| 146 |
+
# Botão para gerar imagem combinando os LoRAs
|
| 147 |
generate_button.click(
|
| 148 |
run_lora,
|
| 149 |
+
inputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale_1, lora_scale_2],
|
| 150 |
outputs=[result, seed],
|
| 151 |
)
|
| 152 |
|