Spaces:
Running
Running
import os | |
import requests | |
import subprocess | |
import gradio as gr | |
# Token Hugging Face từ biến môi trường | |
hf_token = os.getenv("HF_TOKEN") | |
# URLs cần tải | |
app_url = "https://huggingface.co/datasets/ArrcttacsrjksX/Deffusion/resolve/main/RunModelAppp/App/sdRundeffusiononhuggingfacemaster-ac54e00" | |
model_url = "https://huggingface.co/datasets/ArrcttacsrjksX/Deffusion/resolve/main/Model/realisticVisionV60B1_v51HyperVAE.safetensors" | |
# Đường dẫn lưu file | |
app_path = "sdRundeffusiononhuggingfacemaster-ac54e00" | |
model_path = "realisticVisionV60B1_v51HyperVAE.safetensors" | |
# Hàm tải file từ Hugging Face | |
def download_file(url, output_path, token): | |
headers = {"Authorization": f"Bearer {token}"} | |
response = requests.get(url, headers=headers, stream=True) | |
response.raise_for_status() # Kiểm tra lỗi | |
with open(output_path, "wb") as f: | |
for chunk in response.iter_content(chunk_size=8192): | |
f.write(chunk) | |
print(f"Downloaded: {output_path}") | |
# Tải các file nếu chưa tồn tại | |
if not os.path.exists(app_path): | |
download_file(app_url, app_path, hf_token) | |
subprocess.run(["chmod", "+x", app_path]) # Thay đổi quyền thực thi | |
if not os.path.exists(model_path): | |
download_file(model_url, model_path, hf_token) | |
# Hàm xử lý chạy ứng dụng | |
def run_command(prompt, mode, height, width, steps, seed, init_image=None): | |
try: | |
# Lưu ảnh đầu vào nếu được cung cấp | |
init_image_path = None | |
if init_image: | |
init_image_path = "input_image.png" | |
init_image.save(init_image_path) | |
# Chạy lệnh | |
command = [ | |
f"./{app_path}", | |
"-M", mode, | |
"-m", model_path, | |
"-p", prompt, | |
"-H", str(height), | |
"-W", str(width), | |
"--steps", str(steps), | |
"-s", str(seed), | |
] | |
# Thêm ảnh đầu vào nếu có | |
if mode == "img2img" and init_image_path: | |
command.extend(["-i", init_image_path]) | |
result = subprocess.run(command, capture_output=True, text=True) | |
# Kiểm tra kết quả và trả về | |
if result.returncode == 0: | |
output_path = "./output.png" # Đường dẫn ảnh đầu ra mặc định | |
return output_path if os.path.exists(output_path) else "Output image not found." | |
else: | |
return f"Error: {result.stderr}" | |
except Exception as e: | |
return str(e) | |
# Giao diện Gradio | |
with gr.Blocks() as demo: | |
gr.Markdown("# Stable Diffusion Interface") | |
# Cấu hình tham số | |
with gr.Row(): | |
prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here", lines=2) | |
mode = gr.Radio(choices=["txt2img", "img2img"], value="txt2img", label="Mode") | |
with gr.Row(): | |
height = gr.Slider(128, 1024, value=512, step=64, label="Height (px)") | |
width = gr.Slider(128, 1024, value=512, step=64, label="Width (px)") | |
with gr.Row(): | |
steps = gr.Slider(1, 100, value=20, step=1, label="Steps") | |
seed = gr.Slider(-1, 10000, value=42, step=1, label="Seed (-1 for random)") | |
init_image = gr.Image(label="Initial Image (for img2img mode, optional)", type="pil", optional=True) | |
output_path = gr.Textbox(label="Output Image Path or Logs", interactive=False) | |
run_button = gr.Button("Run") | |
# Kết nối hàm xử lý | |
run_button.click( | |
run_command, | |
inputs=[prompt, mode, height, width, steps, seed, init_image], | |
outputs=output_path, | |
) | |
demo.launch() | |