Spaces:
Running
Running
Upload app (5).py
Browse files- app (5).py +138 -0
app (5).py
ADDED
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
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):
|
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 |
+
]
|
55 |
+
|
56 |
+
# Thêm ảnh đầu vào nếu có
|
57 |
+
if mode == "img2img" and init_image_path:
|
58 |
+
command.extend(["-i", init_image_path])
|
59 |
+
|
60 |
+
# Chạy lệnh và hiển thị log theo thời gian thực
|
61 |
+
process = subprocess.Popen(
|
62 |
+
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
|
63 |
+
)
|
64 |
+
logs = []
|
65 |
+
for line in process.stdout:
|
66 |
+
logs.append(line.strip()) # Lưu log vào danh sách
|
67 |
+
print(line, end="") # In log ra màn hình
|
68 |
+
|
69 |
+
process.wait() # Đợi tiến trình hoàn thành
|
70 |
+
|
71 |
+
# Kiểm tra kết quả và trả về
|
72 |
+
if process.returncode == 0:
|
73 |
+
output_path = "./output.png" # Đường dẫn ảnh đầu ra mặc định
|
74 |
+
return output_path if os.path.exists(output_path) else None, "\n".join(logs)
|
75 |
+
else:
|
76 |
+
error_log = process.stderr.read() # Đọc lỗi
|
77 |
+
logs.append(error_log)
|
78 |
+
return None, "\n".join(logs)
|
79 |
+
except Exception as e:
|
80 |
+
return None, str(e)
|
81 |
+
|
82 |
+
# Giao diện Gradio
|
83 |
+
def toggle_image_input(mode):
|
84 |
+
"""Hiển thị hoặc ẩn ô Drop Image dựa trên mode."""
|
85 |
+
return gr.update(visible=(mode == "img2img"))
|
86 |
+
|
87 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
88 |
+
gr.Markdown(
|
89 |
+
"""
|
90 |
+
# 🌟 **Stable Diffusion Interface**
|
91 |
+
Generate stunning images from text or modify existing images with AI-powered tools.
|
92 |
+
"""
|
93 |
+
)
|
94 |
+
|
95 |
+
# Thiết lập giao diện
|
96 |
+
with gr.Row():
|
97 |
+
with gr.Column():
|
98 |
+
prompt = gr.Textbox(
|
99 |
+
label="🎨 Prompt", placeholder="Enter your creative idea here...", lines=2
|
100 |
+
)
|
101 |
+
mode = gr.Radio(
|
102 |
+
choices=["txt2img", "img2img"], value="txt2img", label="Mode", interactive=True
|
103 |
+
)
|
104 |
+
init_image = gr.Image(
|
105 |
+
label="Drop Image (for img2img mode)", type="pil", visible=False
|
106 |
+
)
|
107 |
+
mode.change(toggle_image_input, inputs=mode, outputs=init_image)
|
108 |
+
|
109 |
+
with gr.Column():
|
110 |
+
height = gr.Slider(
|
111 |
+
128, 1024, value=512, step=64, label="Image Height (px)", interactive=True
|
112 |
+
)
|
113 |
+
width = gr.Slider(
|
114 |
+
128, 1024, value=512, step=64, label="Image Width (px)", interactive=True
|
115 |
+
)
|
116 |
+
steps = gr.Slider(
|
117 |
+
1, 100, value=20, step=1, label="Sampling Steps", interactive=True
|
118 |
+
)
|
119 |
+
seed = gr.Slider(
|
120 |
+
-1, 10000, value=42, step=1, label="Random Seed (-1 for random)", interactive=True
|
121 |
+
)
|
122 |
+
|
123 |
+
# Nút chạy và kết quả
|
124 |
+
with gr.Row():
|
125 |
+
run_button = gr.Button("🚀 Run", variant="primary")
|
126 |
+
|
127 |
+
with gr.Row():
|
128 |
+
output_image = gr.File(label="Download Image", interactive=False)
|
129 |
+
log_output = gr.Textbox(label="Logs", interactive=False, lines=10)
|
130 |
+
|
131 |
+
# Kết nối nút Run với hàm xử lý
|
132 |
+
run_button.click(
|
133 |
+
run_command,
|
134 |
+
inputs=[prompt, mode, height, width, steps, seed, init_image],
|
135 |
+
outputs=[output_image, log_output],
|
136 |
+
)
|
137 |
+
|
138 |
+
demo.launch()
|