better structures
Browse files
app.py
CHANGED
|
@@ -1,29 +1,16 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
from huggingface_hub import InferenceClient
|
| 4 |
-
from PIL import Image
|
| 5 |
-
from io import BytesIO
|
| 6 |
-
|
| 7 |
-
import os
|
| 8 |
import traceback
|
|
|
|
|
|
|
| 9 |
import gradio as gr
|
|
|
|
| 10 |
|
| 11 |
-
def main():
|
| 12 |
-
try:
|
| 13 |
-
# seu código normal: init, load model, pipeline, interface
|
| 14 |
-
...
|
| 15 |
-
demo = gr.Interface(...) # ou Blocks
|
| 16 |
-
demo.launch(enable_queue=True, debug=True)
|
| 17 |
-
except Exception as e:
|
| 18 |
-
print("Error Int:", file=sys.stderr)
|
| 19 |
-
traceback.print_exc()
|
| 20 |
-
sys.exit(1)
|
| 21 |
-
|
| 22 |
-
if __name__ == "__main__":
|
| 23 |
-
main()
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
# Checa token
|
| 27 |
hf_token = os.environ.get("HF_TOKEN")
|
| 28 |
token_missing = hf_token is None
|
| 29 |
|
|
@@ -32,18 +19,24 @@ client = None if token_missing else InferenceClient(
|
|
| 32 |
api_key=hf_token,
|
| 33 |
)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
def aspect_ratio_preview(img, max_dim=256):
|
| 36 |
img_copy = img.copy()
|
| 37 |
img_copy.thumbnail((max_dim, max_dim))
|
| 38 |
return img_copy
|
| 39 |
|
|
|
|
| 40 |
def generate_images(img1, img2, img3, prompt="", n_outputs=1, aspect_ratio="4:3"):
|
| 41 |
if token_missing:
|
| 42 |
return []
|
| 43 |
|
| 44 |
images = [img for img in [img1, img2, img3] if img is not None]
|
| 45 |
if not images:
|
| 46 |
-
return [Image.new("RGB", (256,256),
|
| 47 |
|
| 48 |
n_outputs = min(int(n_outputs), 3)
|
| 49 |
outputs = []
|
|
@@ -58,104 +51,87 @@ def generate_images(img1, img2, img3, prompt="", n_outputs=1, aspect_ratio="4:3"
|
|
| 58 |
model="Qwen/Qwen-Image-Edit"
|
| 59 |
)
|
| 60 |
except Exception:
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
outputs.append(aspect_ratio_preview(image))
|
| 63 |
|
| 64 |
return outputs
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
with gr.Blocks(title="Qwen Image Multi-Edit By Chalenger") as demo:
|
| 67 |
|
| 68 |
if token_missing:
|
| 69 |
-
gr.Markdown(
|
| 70 |
-
|
| 71 |
-
|
| 72 |
|
| 73 |
-
gr.Markdown("## Multi-Image Editor for Qwen Image Edit -- Max 3 Images")
|
| 74 |
-
|
| 75 |
with gr.Row():
|
| 76 |
img1 = gr.Image(label="Image Upload 1", type="pil")
|
| 77 |
img2 = gr.Image(label="Image Upload 2", type="pil")
|
| 78 |
img3 = gr.Image(label="Image Upload 3", type="pil")
|
| 79 |
-
|
| 80 |
-
prompt = gr.Textbox(
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
nout = gr.Dropdown(
|
| 83 |
label="Outputs / Saída",
|
| 84 |
-
choices=[1,2,3],
|
| 85 |
value=1
|
| 86 |
)
|
| 87 |
|
| 88 |
aspect_ratio_state = gr.State("4:3")
|
|
|
|
| 89 |
with gr.Row():
|
| 90 |
btn_4_3 = gr.Button("📺 4:3 CRT")
|
| 91 |
btn_16_9 = gr.Button("🖥️ 16:9 LED")
|
| 92 |
btn_9_16 = gr.Button("📱 9:16 Phone")
|
| 93 |
btn_1_1 = gr.Button("⬛ 1:1 Square")
|
| 94 |
|
| 95 |
-
btn_4_3.click(lambda: "4:3",
|
| 96 |
-
btn_16_9.click(lambda: "16:9",
|
| 97 |
-
btn_9_16.click(lambda: "9:16",
|
| 98 |
-
btn_1_1.click(lambda: "1:1",
|
| 99 |
|
| 100 |
-
# Botões Gradio reais
|
| 101 |
with gr.Row():
|
| 102 |
play_btn = gr.Button("▶️ Start Engine")
|
| 103 |
stop_btn = gr.Button("⏹️ Kill Tasks")
|
| 104 |
|
| 105 |
-
gallery = gr.Gallery(
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
"""
|
| 110 |
-
<script>
|
| 111 |
-
const playBtnEl = document.querySelector('button:contains("▶️ Gerar")');
|
| 112 |
-
const stopBtnEl = document.querySelector('button:contains("⏹️ Parar")');
|
| 113 |
-
|
| 114 |
-
// Cria LED para Gerar
|
| 115 |
-
const ledPlay = document.createElement('span');
|
| 116 |
-
ledPlay.style.cssText = 'width:12px; height:12px; background:#400; display:inline-block; margin-right:8px; border-radius:2px;';
|
| 117 |
-
playBtnEl.prepend(ledPlay);
|
| 118 |
-
|
| 119 |
-
// Cria LED para Parar
|
| 120 |
-
const ledStop = document.createElement('span');
|
| 121 |
-
ledStop.style.cssText = 'width:12px; height:12px; background:#400; display:inline-block; margin-right:8px; border-radius:2px;';
|
| 122 |
-
stopBtnEl.prepend(ledStop);
|
| 123 |
-
|
| 124 |
-
let blinkInterval = null;
|
| 125 |
-
|
| 126 |
-
playBtnEl.addEventListener('click', () => {
|
| 127 |
-
if(blinkInterval) clearInterval(blinkInterval);
|
| 128 |
-
blinkInterval = setInterval(() => {
|
| 129 |
-
ledPlay.style.background = ledPlay.style.background === 'red' ? '#800' : 'red';
|
| 130 |
-
}, 500);
|
| 131 |
-
ledStop.style.background = '#400';
|
| 132 |
-
});
|
| 133 |
-
|
| 134 |
-
stopBtnEl.addEventListener('click', () => {
|
| 135 |
-
if(blinkInterval) clearInterval(blinkInterval);
|
| 136 |
-
ledStop.style.background = 'red';
|
| 137 |
-
ledPlay.style.background = '#400';
|
| 138 |
-
});
|
| 139 |
-
|
| 140 |
-
if (%s){
|
| 141 |
-
playBtnEl.disabled = true;
|
| 142 |
-
stopBtnEl.disabled = true;
|
| 143 |
-
playBtnEl.title = 'HF_TOKEN not set! Add it in Secrets to enable.';
|
| 144 |
-
stopBtnEl.title = 'HF_TOKEN not set! Add it in Secrets to enable.';
|
| 145 |
-
}
|
| 146 |
-
</script>
|
| 147 |
-
""" % ("true" if token_missing else "false"),
|
| 148 |
-
visible=False
|
| 149 |
)
|
| 150 |
|
| 151 |
-
# Conecta botão Gradio à função
|
| 152 |
play_btn.click(
|
| 153 |
-
generate_images,
|
| 154 |
inputs=[img1, img2, img3, prompt, nout, aspect_ratio_state],
|
| 155 |
-
outputs=gallery
|
|
|
|
| 156 |
)
|
| 157 |
|
| 158 |
-
stop_btn.click(lambda: None,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
if __name__ == "__main__":
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import traceback
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
from PIL import Image
|
| 6 |
import gradio as gr
|
| 7 |
+
from huggingface_hub import InferenceClient
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# ============================
|
| 11 |
+
# TOKEN / CLIENTE
|
| 12 |
+
# ============================
|
| 13 |
|
|
|
|
| 14 |
hf_token = os.environ.get("HF_TOKEN")
|
| 15 |
token_missing = hf_token is None
|
| 16 |
|
|
|
|
| 19 |
api_key=hf_token,
|
| 20 |
)
|
| 21 |
|
| 22 |
+
|
| 23 |
+
# ============================
|
| 24 |
+
# FUNÇÕES PRINCIPAIS
|
| 25 |
+
# ============================
|
| 26 |
+
|
| 27 |
def aspect_ratio_preview(img, max_dim=256):
|
| 28 |
img_copy = img.copy()
|
| 29 |
img_copy.thumbnail((max_dim, max_dim))
|
| 30 |
return img_copy
|
| 31 |
|
| 32 |
+
|
| 33 |
def generate_images(img1, img2, img3, prompt="", n_outputs=1, aspect_ratio="4:3"):
|
| 34 |
if token_missing:
|
| 35 |
return []
|
| 36 |
|
| 37 |
images = [img for img in [img1, img2, img3] if img is not None]
|
| 38 |
if not images:
|
| 39 |
+
return [Image.new("RGB", (256, 256), "gray")]
|
| 40 |
|
| 41 |
n_outputs = min(int(n_outputs), 3)
|
| 42 |
outputs = []
|
|
|
|
| 51 |
model="Qwen/Qwen-Image-Edit"
|
| 52 |
)
|
| 53 |
except Exception:
|
| 54 |
+
traceback.print_exc()
|
| 55 |
+
image = Image.new("RGB", (256, 256), "gray")
|
| 56 |
+
|
| 57 |
outputs.append(aspect_ratio_preview(image))
|
| 58 |
|
| 59 |
return outputs
|
| 60 |
|
| 61 |
+
|
| 62 |
+
# ============================
|
| 63 |
+
# INTERFACE GRADIO
|
| 64 |
+
# ============================
|
| 65 |
+
|
| 66 |
with gr.Blocks(title="Qwen Image Multi-Edit By Chalenger") as demo:
|
| 67 |
|
| 68 |
if token_missing:
|
| 69 |
+
gr.Markdown("⚠️ **HF_TOKEN not set!** Adicione o token em *Settings → Secrets*.")
|
| 70 |
+
|
| 71 |
+
gr.Markdown("## Multi-Image Editor for Qwen Image Edit — Max 3 Images")
|
| 72 |
|
|
|
|
|
|
|
| 73 |
with gr.Row():
|
| 74 |
img1 = gr.Image(label="Image Upload 1", type="pil")
|
| 75 |
img2 = gr.Image(label="Image Upload 2", type="pil")
|
| 76 |
img3 = gr.Image(label="Image Upload 3", type="pil")
|
| 77 |
+
|
| 78 |
+
prompt = gr.Textbox(
|
| 79 |
+
label="Prompt (opcional)",
|
| 80 |
+
placeholder="Description / Descrição",
|
| 81 |
+
lines=2
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
nout = gr.Dropdown(
|
| 85 |
label="Outputs / Saída",
|
| 86 |
+
choices=[1, 2, 3],
|
| 87 |
value=1
|
| 88 |
)
|
| 89 |
|
| 90 |
aspect_ratio_state = gr.State("4:3")
|
| 91 |
+
|
| 92 |
with gr.Row():
|
| 93 |
btn_4_3 = gr.Button("📺 4:3 CRT")
|
| 94 |
btn_16_9 = gr.Button("🖥️ 16:9 LED")
|
| 95 |
btn_9_16 = gr.Button("📱 9:16 Phone")
|
| 96 |
btn_1_1 = gr.Button("⬛ 1:1 Square")
|
| 97 |
|
| 98 |
+
btn_4_3.click(lambda: "4:3", None, aspect_ratio_state)
|
| 99 |
+
btn_16_9.click(lambda: "16:9", None, aspect_ratio_state)
|
| 100 |
+
btn_9_16.click(lambda: "9:16", None, aspect_ratio_state)
|
| 101 |
+
btn_1_1.click(lambda: "1:1", None, aspect_ratio_state)
|
| 102 |
|
|
|
|
| 103 |
with gr.Row():
|
| 104 |
play_btn = gr.Button("▶️ Start Engine")
|
| 105 |
stop_btn = gr.Button("⏹️ Kill Tasks")
|
| 106 |
|
| 107 |
+
gallery = gr.Gallery(
|
| 108 |
+
label="Outputs / Saídas",
|
| 109 |
+
columns=2,
|
| 110 |
+
height="500"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
)
|
| 112 |
|
|
|
|
| 113 |
play_btn.click(
|
| 114 |
+
fn=generate_images,
|
| 115 |
inputs=[img1, img2, img3, prompt, nout, aspect_ratio_state],
|
| 116 |
+
outputs=gallery,
|
| 117 |
+
queue=True
|
| 118 |
)
|
| 119 |
|
| 120 |
+
stop_btn.click(lambda: None, None, gallery)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
# ============================
|
| 124 |
+
# LAUNCH CORRETO
|
| 125 |
+
# ============================
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|
| 128 |
+
try:
|
| 129 |
+
demo.launch(
|
| 130 |
+
server_name="0.0.0.0",
|
| 131 |
+
server_port=7860,
|
| 132 |
+
enable_queue=True,
|
| 133 |
+
debug=True
|
| 134 |
+
)
|
| 135 |
+
except Exception:
|
| 136 |
+
traceback.print_exc()
|
| 137 |
+
sys.exit(1)
|