ArrcttacsrjksX commited on
Commit
3964dd9
·
verified ·
1 Parent(s): 6259d02

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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):
37
+ try:
38
+ # Lưu ảnh đầu vào nếu được cung cấp
39
+ init_image_path = None
40
+ if init_image:
41
+ init_image_path = "input_image.png"
42
+ init_image.save(init_image_path)
43
+
44
+ # Chạy lệnh
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
+ result = subprocess.run(command, capture_output=True, text=True)
61
+
62
+ # Kiểm tra kết quả và trả về
63
+ if result.returncode == 0:
64
+ output_path = "./output.png" # Đường dẫn ảnh đầu ra mặc định
65
+ return output_path if os.path.exists(output_path) else "Output image not found."
66
+ else:
67
+ return f"Error: {result.stderr}"
68
+ except Exception as e:
69
+ return str(e)
70
+
71
+ # Giao diện Gradio
72
+ with gr.Blocks() as demo:
73
+ gr.Markdown("# Stable Diffusion Interface")
74
+
75
+ # Cấu hình tham số
76
+ with gr.Row():
77
+ prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here", lines=2)
78
+ mode = gr.Radio(choices=["txt2img", "img2img"], value="txt2img", label="Mode")
79
+
80
+ with gr.Row():
81
+ height = gr.Slider(128, 1024, value=512, step=64, label="Height (px)")
82
+ width = gr.Slider(128, 1024, value=512, step=64, label="Width (px)")
83
+
84
+ with gr.Row():
85
+ steps = gr.Slider(1, 100, value=20, step=1, label="Steps")
86
+ seed = gr.Slider(-1, 10000, value=42, step=1, label="Seed (-1 for random)")
87
+
88
+ init_image = gr.Image(label="Initial Image (for img2img mode, optional)", type="pil", optional=True)
89
+ output_path = gr.Textbox(label="Output Image Path or Logs", interactive=False)
90
+ run_button = gr.Button("Run")
91
+
92
+ # Kết nối hàm xử lý
93
+ run_button.click(
94
+ run_command,
95
+ inputs=[prompt, mode, height, width, steps, seed, init_image],
96
+ outputs=output_path,
97
+ )
98
+
99
+ demo.launch()