ArrcttacsrjksX commited on
Commit
3855928
·
verified ·
1 Parent(s): 803809e

Upload app (8).py

Browse files
Files changed (1) hide show
  1. app (8).py +197 -0
app (8).py ADDED
@@ -0,0 +1,197 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import subprocess
4
+ import gradio as gr
5
+
6
+ # Token Hugging Face từ biến môi trường
7
+ hf_token = os.getenv("HF_TOKEN")
8
+
9
+ # URLs cần tải
10
+ app_url = "https://huggingface.co/datasets/ArrcttacsrjksX/Deffusion/resolve/main/RunModelAppp/App/sdmaster-d9b5942LatestJan182025"
11
+ model_url = "https://huggingface.co/datasets/ArrcttacsrjksX/Deffusion/resolve/main/Model/realisticVisionV60B1_v51HyperVAE.safetensors"
12
+
13
+ # Đường dẫn lưu file
14
+ app_path = "sdRundeffusiononhuggingfacemaster-ac54e00"
15
+ model_path = "realisticVisionV60B1_v51HyperVAE.safetensors"
16
+
17
+ # Hàm tải file từ Hugging Face
18
+ def download_file(url, output_path, token):
19
+ headers = {"Authorization": f"Bearer {token}"}
20
+ response = requests.get(url, headers=headers, stream=True)
21
+ response.raise_for_status() # Kiểm tra lỗi
22
+ with open(output_path, "wb") as f:
23
+ for chunk in response.iter_content(chunk_size=8192):
24
+ f.write(chunk)
25
+ print(f"Downloaded: {output_path}")
26
+
27
+ # Tải các file nếu chưa tồn tại
28
+ if not os.path.exists(app_path):
29
+ download_file(app_url, app_path, hf_token)
30
+ subprocess.run(["chmod", "+x", app_path]) # Thay đổi quyền thực thi
31
+ if not os.path.exists(model_path):
32
+ download_file(model_url, model_path, hf_token)
33
+
34
+ # Hàm xử lý chạy ứng dụng
35
+ def run_command(
36
+ prompt, mode, height, width, steps, seed, cfg_scale, strength, sampling_method,
37
+ batch_count, schedule, clip_skip, vae_tiling, vae_on_cpu, clip_on_cpu, diffusion_fa,
38
+ control_net_cpu, canny, verbose, init_image=None
39
+ ):
40
+ try:
41
+ # Lưu ảnh đầu vào nếu được cung cấp
42
+ init_image_path = None
43
+ if init_image is not None:
44
+ init_image_path = "input_image.png"
45
+ init_image.save(init_image_path)
46
+
47
+ # Tạo lệnh chạy
48
+ command = [
49
+ f"./{app_path}",
50
+ "-M", mode,
51
+ "-m", model_path,
52
+ "-p", prompt,
53
+ "-H", str(height),
54
+ "-W", str(width),
55
+ "--steps", str(steps),
56
+ "-s", str(seed),
57
+ "--cfg-scale", str(cfg_scale),
58
+ "--strength", str(strength),
59
+ "--sampling-method", sampling_method,
60
+ "--batch-count", str(batch_count),
61
+ "--schedule", schedule,
62
+ "--clip-skip", str(clip_skip),
63
+ ]
64
+
65
+ # Thêm tùy chọn VAE tiling
66
+ if vae_tiling:
67
+ command.append("--vae-tiling")
68
+ if vae_on_cpu:
69
+ command.append("--vae-on-cpu")
70
+ if clip_on_cpu:
71
+ command.append("--clip-on-cpu")
72
+ if diffusion_fa:
73
+ command.append("--diffusion-fa")
74
+ if control_net_cpu:
75
+ command.append("--control-net-cpu")
76
+ if canny:
77
+ command.append("--canny")
78
+ if verbose:
79
+ command.append("-v")
80
+
81
+ # Thêm ảnh đầu vào nếu có
82
+ if mode == "img2img" and init_image_path:
83
+ command.extend(["-i", init_image_path])
84
+
85
+ # Chạy lệnh và hiển thị log theo thời gian thực
86
+ process = subprocess.Popen(
87
+ command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
88
+ )
89
+ logs = []
90
+ for line in process.stdout:
91
+ logs.append(line.strip()) # Lưu log vào danh sách
92
+ print(line, end="") # In log ra màn hình
93
+
94
+ process.wait() # Đợi tiến trình hoàn thành
95
+ # Kiểm tra kết quả và trả về
96
+ if process.returncode == 0:
97
+ output_path = "./output.png" # Đường dẫn ảnh đầu ra mặc định
98
+ return output_path if os.path.exists(output_path) else None, "\n".join(logs)
99
+ else:
100
+ error_log = process.stderr.read() # Đọc lỗi
101
+ logs.append(error_log)
102
+ return None, "\n".join(logs)
103
+ except Exception as e:
104
+ return None, str(e)
105
+
106
+ # Giao diện Gradio
107
+ def toggle_image_input(mode):
108
+ """Hiển thị hoặc ẩn ô Drop Image dựa trên mode."""
109
+ return gr.update(visible=(mode == "img2img"))
110
+
111
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
112
+ gr.Markdown(
113
+ """
114
+ # 🌟 **Stable Diffusion Interface**
115
+ Generate stunning images from text or modify existing images with AI-powered tools.
116
+ """
117
+ )
118
+
119
+ # Thiết lập giao diện
120
+ with gr.Row():
121
+ with gr.Column():
122
+ prompt = gr.Textbox(
123
+ label="🎨 Prompt", placeholder="Enter your creative idea here...", lines=2
124
+ )
125
+ mode = gr.Radio(
126
+ choices=["txt2img", "img2img"], value="txt2img", label="Mode", interactive=True
127
+ )
128
+ init_image = gr.Image(
129
+ label="Drop Image (for img2img mode)", type="pil", visible=False
130
+ )
131
+ mode.change(toggle_image_input, inputs=mode, outputs=init_image)
132
+
133
+ with gr.Column():
134
+ height = gr.Slider(
135
+ 128, 1024, value=512, step=64, label="Image Height (px)", interactive=True
136
+ )
137
+ width = gr.Slider(
138
+ 128, 1024, value=512, step=64, label="Image Width (px)", interactive=True
139
+ )
140
+ steps = gr.Slider(
141
+ 1, 100, value=20, step=1, label="Sampling Steps", interactive=True
142
+ )
143
+ seed = gr.Slider(
144
+ -1, 10000, value=42, step=1, label="Random Seed (-1 for random)", interactive=True
145
+ )
146
+ cfg_scale = gr.Slider(
147
+ 1, 20, value=7, step=0.1, label="CFG Scale", interactive=True
148
+ )
149
+ strength = gr.Slider(
150
+ 0, 1, value=0.75, step=0.01, label="Strength (img2img only)", interactive=True
151
+ )
152
+
153
+ with gr.Row():
154
+ sampling_method = gr.Dropdown(
155
+ choices=["euler", "euler_a", "heun", "dpm2", "dpm++2s_a", "dpm++2m", "dpm++2mv2", "ipndm", "ipndm_v", "lcm"],
156
+ value="euler_a", label="Sampling Method", interactive=True
157
+ )
158
+ batch_count = gr.Slider(
159
+ 1, 10, value=1, step=1, label="Batch Count", interactive=True
160
+ )
161
+ schedule = gr.Dropdown(
162
+ choices=["discrete", "karras", "exponential", "ays", "gits"],
163
+ value="discrete", label="Denoiser Sigma Schedule", interactive=True
164
+ )
165
+
166
+ with gr.Row():
167
+ clip_skip = gr.Slider(
168
+ -1, 10, value=-1, step=1, label="CLIP Skip Layers", interactive=True
169
+ )
170
+ vae_tiling = gr.Checkbox(label="VAE Tiling", value=False)
171
+ vae_on_cpu = gr.Checkbox(label="VAE on CPU", value=False)
172
+ clip_on_cpu = gr.Checkbox(label="CLIP on CPU", value=False)
173
+ diffusion_fa = gr.Checkbox(label="Diffusion Flash Attention", value=False)
174
+ control_net_cpu = gr.Checkbox(label="ControlNet on CPU", value=False)
175
+ canny = gr.Checkbox(label="Canny Preprocessor", value=False)
176
+ verbose = gr.Checkbox(label="Verbose Logging", value=False)
177
+
178
+ # Nút chạy và kết quả
179
+ with gr.Row():
180
+ run_button = gr.Button("🚀 Run", variant="primary")
181
+
182
+ with gr.Row():
183
+ output_image = gr.File(label="Download Image", interactive=False)
184
+ log_output = gr.Textbox(label="Logs", interactive=False, lines=10)
185
+
186
+ # Kết nối nút Run với hàm xử lý
187
+ run_button.click(
188
+ run_command,
189
+ inputs=[
190
+ prompt, mode, height, width, steps, seed, cfg_scale, strength, sampling_method,
191
+ batch_count, schedule, clip_skip, vae_tiling, vae_on_cpu, clip_on_cpu, diffusion_fa,
192
+ control_net_cpu, canny, verbose, init_image
193
+ ],
194
+ outputs=[output_image, log_output],
195
+ )
196
+
197
+ demo.launch()