ArrcttacsrjksX commited on
Commit
8342297
·
verified ·
1 Parent(s): a242ae5

Upload app(7).py

Browse files
Files changed (1) hide show
  1. app(7).py +179 -0
app(7).py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/sdRundeffusiononhuggingfacemaster-ac54e00"
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
+
32
+ if not os.path.exists(model_path):
33
+ download_file(model_url, model_path, hf_token)
34
+
35
+ # Hàm xử lý chạy ứng dụng
36
+ def run_command(prompt, mode, height, width, steps, seed, init_image=None, threads=-1, weight_type="f32", negative_prompt="", cfg_scale=7.0, strength=0.75, style_ratio=0.2, control_strength=0.9, sampling_method="euler_a", batch_count=1, schedule="discrete", clip_skip=-1, vae_tiling=False, vae_on_cpu=False, clip_on_cpu=False, control_net_cpu=False, canny=False, color=False, verbose=False, rng="cuda"):
37
+ try:
38
+ # Lưu ảnh đầu vào nếu được cung cấp
39
+ init_image_path = None
40
+ if init_image is not None:
41
+ init_image_path = "input_image.png"
42
+ init_image.save(init_image_path)
43
+
44
+ # Tạo lệnh chạy
45
+ command = [
46
+ f"./{app_path}",
47
+ "-M", mode,
48
+ "-m", model_path,
49
+ "-p", prompt,
50
+ "-H", str(height),
51
+ "-W", str(width),
52
+ "--steps", str(steps),
53
+ "-s", str(seed),
54
+ "-t", str(threads),
55
+ "--type", weight_type,
56
+ "--cfg-scale", str(cfg_scale),
57
+ "--strength", str(strength),
58
+ "--style-ratio", str(style_ratio),
59
+ "--control-strength", str(control_strength),
60
+ "--sampling-method", sampling_method,
61
+ "--batch-count", str(batch_count),
62
+ "--schedule", schedule,
63
+ "--clip-skip", str(clip_skip),
64
+ "--vae-tiling" if vae_tiling else None,
65
+ "--vae-on-cpu" if vae_on_cpu else None,
66
+ "--clip-on-cpu" if clip_on_cpu else None,
67
+ "--control-net-cpu" if control_net_cpu else None,
68
+ "--canny" if canny else None,
69
+ "--color" if color else None,
70
+ "-v" if verbose else None,
71
+ "--rng", rng
72
+ ]
73
+
74
+ # Loại bỏ các giá trị None trong danh sách lệnh
75
+ command = [arg for arg in command if arg is not None]
76
+
77
+ # Thêm ảnh đầu vào nếu có
78
+ if mode == "img2img" and init_image_path:
79
+ command.extend(["-i", init_image_path])
80
+
81
+ # Chạy lệnh và hiển thị log theo thời gian thực
82
+ process = subprocess.Popen(
83
+ command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
84
+ )
85
+ logs = []
86
+ for line in process.stdout:
87
+ logs.append(line.strip()) # Lưu log vào danh sách
88
+ print(line, end="") # In log ra màn hình
89
+
90
+ process.wait() # Đợi tiến trình hoàn thành
91
+
92
+ # Kiểm tra kết quả và trả về
93
+ if process.returncode == 0:
94
+ output_path = "./output.png" # Đường dẫn ảnh đầu ra mặc định
95
+ return output_path if os.path.exists(output_path) else None, "\n".join(logs)
96
+ else:
97
+ error_log = process.stderr.read() # Đọc lỗi
98
+ logs.append(error_log)
99
+ return None, "\n".join(logs)
100
+ except Exception as e:
101
+ return None, str(e)
102
+
103
+ # Giao diện Gradio
104
+ def toggle_image_input(mode):
105
+ """Hiển thị hoặc ẩn ô Drop Image dựa trên mode."""
106
+ return gr.update(visible=(mode == "img2img"))
107
+
108
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
109
+ gr.Markdown(
110
+ """
111
+ # 🌟 **Stable Diffusion Interface**
112
+ Generate stunning images from text or modify existing images with AI-powered tools.
113
+ """
114
+ )
115
+
116
+ # Thiết lập giao diện
117
+ with gr.Row():
118
+ with gr.Column():
119
+ prompt = gr.Textbox(
120
+ label="🎨 Prompt", placeholder="Enter your creative idea here...", lines=2
121
+ )
122
+ mode = gr.Radio(
123
+ choices=["txt2img", "img2img"], value="txt2img", label="Mode", interactive=True
124
+ )
125
+ init_image = gr.Image(
126
+ label="Drop Image (for img2img mode)", type="pil", visible=False
127
+ )
128
+ mode.change(toggle_image_input, inputs=mode, outputs=init_image)
129
+ negative_prompt = gr.Textbox(
130
+ label="Negative Prompt", placeholder="Anything to avoid in the image", lines=2
131
+ )
132
+ threads = gr.Slider(-1, 64, value=-1, step=1, label="Threads", interactive=True)
133
+ weight_type = gr.Dropdown(choices=["f32", "f16", "q4_0", "q4_1", "q5_0", "q5_1", "q8_0", "q2_k", "q3_k", "q4_k"], value="f32", label="Weight Type")
134
+ cfg_scale = gr.Slider(0, 20, value=7.0, step=0.1, label="CFG Scale", interactive=True)
135
+ strength = gr.Slider(0, 1, value=0.75, step=0.01, label="Strength", interactive=True)
136
+ style_ratio = gr.Slider(0, 1, value=0.2, step=0.01, label="Style Ratio", interactive=True)
137
+ control_strength = gr.Slider(0, 1, value=0.9, step=0.01, label="Control Strength", interactive=True)
138
+ sampling_method = gr.Dropdown(choices=["euler", "euler_a", "heun", "dpm2", "dpm++2s_a", "dpm++2m", "dpm++2mv2", "ipndm", "ipndm_v", "lcm"], value="euler_a", label="Sampling Method")
139
+ batch_count = gr.Slider(1, 10, value=1, step=1, label="Batch Count", interactive=True)
140
+ schedule = gr.Dropdown(choices=["discrete", "karras", "exponential", "ays", "gits"], value="discrete", label="Denoiser Schedule")
141
+ clip_skip = gr.Slider(-1, 2, value=-1, step=1, label="Clip Skip", interactive=True)
142
+ vae_tiling = gr.Checkbox(label="VAE Tiling", interactive=True)
143
+ vae_on_cpu = gr.Checkbox(label="VAE on CPU", interactive=True)
144
+ clip_on_cpu = gr.Checkbox(label="CLIP on CPU", interactive=True)
145
+ control_net_cpu = gr.Checkbox(label="Control Net on CPU", interactive=True)
146
+ canny = gr.Checkbox(label="Apply Canny Preprocessor", interactive=True)
147
+ color = gr.Checkbox(label="Color Logs", interactive=True)
148
+ verbose = gr.Checkbox(label="Verbose Output", interactive=True)
149
+ rng = gr.Radio(choices=["std_default", "cuda"], value="cuda", label="Random Number Generator", interactive=True)
150
+
151
+ with gr.Column():
152
+ height = gr.Slider(
153
+ 128, 1024, value=512, step=64, label="Image Height (px)", interactive=True
154
+ )
155
+ width = gr.Slider(
156
+ 128, 1024, value=512, step=64, label="Image Width (px)", interactive=True
157
+ )
158
+ steps = gr.Slider(
159
+ 1, 100, value=20, step=1, label="Sampling Steps", interactive=True
160
+ )
161
+ seed = gr.Slider(
162
+ 1, 10000, value=42, step=1, label="Seed", interactive=True
163
+ )
164
+
165
+ generate_btn = gr.Button("Run")
166
+ output_image = gr.Image(label="Generated Image")
167
+ logs_output = gr.Textbox(label="Logs", interactive=False, lines=15)
168
+
169
+ generate_btn.click(
170
+ run_command,
171
+ inputs=[
172
+ prompt, mode, height, width, steps, seed, init_image, threads, weight_type, negative_prompt,
173
+ cfg_scale, strength, style_ratio, control_strength, sampling_method, batch_count, schedule,
174
+ clip_skip, vae_tiling, vae_on_cpu, clip_on_cpu, control_net_cpu, canny, color, verbose, rng
175
+ ],
176
+ outputs=[output_image, logs_output],
177
+ )
178
+
179
+ demo.launch()