Akhmad123 commited on
Commit
7eebe2c
Β·
verified Β·
1 Parent(s): 18464a3

Create tambah img2img - app.py

Browse files
Files changed (1) hide show
  1. tambah img2img - app.py +148 -0
tambah img2img - app.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ import gc
4
+ from diffusers import StableDiffusionPipeline
5
+ from PIL import Image
6
+
7
+ device = "cpu"
8
+
9
+ current_pipe = None
10
+ current_model = None
11
+ history = []
12
+
13
+
14
+ def load_model(model_choice):
15
+ global current_pipe, current_model
16
+
17
+ if current_model == model_choice and current_pipe is not None:
18
+ return current_pipe
19
+
20
+ if current_pipe is not None:
21
+ del current_pipe
22
+ gc.collect()
23
+
24
+ if model_choice == "SD 1.5":
25
+ model_id = "runwayml/stable-diffusion-v1-5"
26
+ else:
27
+ model_id = "Lykon/dreamshaper-8"
28
+
29
+ print(f"πŸ”„ Loading model: {model_id}")
30
+
31
+ pipe = StableDiffusionPipeline.from_pretrained(
32
+ model_id,
33
+ torch_dtype=torch.float32,
34
+ low_cpu_mem_usage=True
35
+ )
36
+
37
+ pipe.to(device)
38
+ pipe.enable_attention_slicing()
39
+ pipe.safety_checker = None
40
+
41
+ current_pipe = pipe
42
+ current_model = model_choice
43
+
44
+ return pipe
45
+
46
+
47
+ # βœ… Upscale sederhana (2x)
48
+ def upscale_image(image):
49
+ if image is None:
50
+ return None
51
+ w, h = image.size
52
+ return image.resize((w * 2, h * 2), Image.LANCZOS)
53
+
54
+
55
+ # βœ… Generate function (txt2img + img2img)
56
+ def generate(prompt, model_choice, steps, input_image, strength, do_upscale):
57
+ pipe = load_model(model_choice)
58
+
59
+ try:
60
+ full_prompt = prompt + ", masterpiece, ultra detailed, 4k"
61
+
62
+ if input_image is not None:
63
+ # img2img
64
+ image = pipe(
65
+ prompt=full_prompt,
66
+ image=input_image,
67
+ strength=strength,
68
+ num_inference_steps=steps,
69
+ guidance_scale=7
70
+ ).images[0]
71
+ else:
72
+ # txt2img
73
+ image = pipe(
74
+ prompt=full_prompt,
75
+ negative_prompt="blurry, low quality, bad anatomy",
76
+ num_inference_steps=steps,
77
+ guidance_scale=7,
78
+ height=512,
79
+ width=512
80
+ ).images[0]
81
+
82
+ # βœ… auto upscale
83
+ if do_upscale:
84
+ image = upscale_image(image)
85
+
86
+ # βœ… simpan ke history
87
+ history.append(image)
88
+
89
+ return image, history
90
+
91
+ except Exception as e:
92
+ return None, history
93
+
94
+
95
+ # βœ… UI MODERN
96
+ with gr.Blocks() as demo:
97
+
98
+ gr.Markdown("# 🎨 AI Image Generator (CPU Optimized)")
99
+ gr.Markdown("SD 1.5 & DreamShaper + Img2Img + Upscale + Gallery")
100
+
101
+ with gr.Row():
102
+ prompt = gr.Textbox(
103
+ label="Prompt",
104
+ placeholder="Contoh: a cyberpunk city at night",
105
+ lines=3
106
+ )
107
+
108
+ with gr.Row():
109
+ model_choice = gr.Radio(
110
+ choices=["SD 1.5", "DreamShaper"],
111
+ value="DreamShaper",
112
+ label="Model"
113
+ )
114
+
115
+ steps = gr.Slider(10, 30, value=20, step=1, label="Steps")
116
+
117
+ with gr.Row():
118
+ input_image = gr.Image(type="pil", label="Upload Image (optional - img2img)")
119
+
120
+ strength = gr.Slider(
121
+ 0.1, 1.0,
122
+ value=0.5,
123
+ step=0.1,
124
+ label="Strength (img2img saja)"
125
+ )
126
+
127
+ with gr.Row():
128
+ upscale = gr.Checkbox(label="Auto Upscale 2x", value=False)
129
+ generate_btn = gr.Button("πŸš€ Generate")
130
+
131
+ output = gr.Image(type="pil", label="Hasil")
132
+
133
+ gr.Markdown("## πŸ–ΌοΈ History")
134
+ gallery = gr.Gallery(label="Hasil Sebelumnya", columns=3)
135
+
136
+ def run(prompt, model_choice, steps, input_image, strength, upscale):
137
+ img, hist = generate(prompt, model_choice, steps, input_image, strength, upscale)
138
+ return img, hist
139
+
140
+ generate_btn.click(
141
+ fn=run,
142
+ inputs=[prompt, model_choice, steps, input_image, strength, upscale],
143
+ outputs=[output, gallery]
144
+ )
145
+
146
+
147
+ if __name__ == "__main__":
148
+ demo.launch()