Files changed (1) hide show
  1. app.py +71 -1
app.py CHANGED
@@ -1 +1,71 @@
1
- import os; exec(os.getenv('EXEC'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ import numpy as np
4
+ from diffusers import FluxKontextPipeline
5
+ from gfpgan import GFPGANer
6
+ from PIL import Image
7
+
8
+ # Device setup
9
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10
+
11
+ # Load FLUX in-context editing pipeline
12
+ pipe = FluxKontextPipeline.from_pretrained(
13
+ "black-forest-labs/FLUX.1-Kontext-dev",
14
+ torch_dtype=torch.bfloat16 if device.type=="cuda" else torch.float32
15
+ ).to(device)
16
+
17
+ # Load face enhancement model
18
+ gfpgan = GFPGANer(
19
+ model_path="https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/GFPGANv1.3.pth",
20
+ upscale=1,
21
+ arch="clean",
22
+ channel_multiplier=2,
23
+ bg_upsampler=None,
24
+ device=device.type
25
+ )
26
+
27
+ def enhance_face(input_img: Image.Image) -> Image.Image:
28
+ img_np = np.array(input_img)
29
+ _, _, output = gfpgan.enhance(img_np, has_aligned=False, only_center_face=False, paste_back=True)
30
+ return Image.fromarray(output)
31
+
32
+ def infer(input_image, prompt, beautify, seed, randomize, steps, guidance_scale):
33
+ # Set random seed
34
+ generator = torch.Generator(device=device).manual_seed(seed if not randomize else torch.randint(0, 2**32-1, ()).item())
35
+
36
+ # In-context editing
37
+ out = pipe(
38
+ image=input_image.convert("RGB"),
39
+ prompt=prompt,
40
+ num_inference_steps=steps,
41
+ guidance_scale=guidance_scale,
42
+ generator=generator
43
+ ).images[0]
44
+
45
+ # Apply face enhancement if selected
46
+ if beautify:
47
+ out = enhance_face(out)
48
+
49
+ return out
50
+
51
+ # UI setup
52
+ with gr.Blocks() as demo:
53
+ gr.Markdown("# FLUX Kontekt Editor + Beautify")
54
+ with gr.Row():
55
+ input_image = gr.Image(label="Upload Image", type="pil")
56
+ result = gr.Image(label="Edited Output")
57
+ prompt = gr.Textbox(label="Edit Prompt", placeholder="e.g., 'change background to beach'")
58
+ beautify = gr.Checkbox(label="Beautify Face", value=True)
59
+ seed = gr.Slider(0, 2**32-1, value=0, step=1, label="Seed")
60
+ randomize = gr.Checkbox(label="Randomize Seed", value=True)
61
+ steps = gr.Slider(1, 30, value=28, label="Steps")
62
+ guidance = gr.Slider(1.0, 10.0, value=2.5, step=0.1, label="Guidance Scale")
63
+ run = gr.Button("Run")
64
+
65
+ run.click(
66
+ fn=infer,
67
+ inputs=[input_image, prompt, beautify, seed, randomize, steps, guidance],
68
+ outputs=result
69
+ )
70
+
71
+ demo.launch()