Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,8 @@ 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")
|
@@ -31,6 +33,28 @@ if not os.path.exists(app_path):
|
|
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,
|
@@ -95,7 +119,12 @@ def run_command(
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
99 |
else:
|
100 |
error_log = process.stderr.read() # Đọc lỗi
|
101 |
logs.append(error_log)
|
|
|
2 |
import requests
|
3 |
import subprocess
|
4 |
import gradio as gr
|
5 |
+
from datetime import datetime
|
6 |
+
from huggingface_hub import HfApi, Repository
|
7 |
|
8 |
# Token Hugging Face từ biến môi trường
|
9 |
hf_token = os.getenv("HF_TOKEN")
|
|
|
33 |
if not os.path.exists(model_path):
|
34 |
download_file(model_url, model_path, hf_token)
|
35 |
|
36 |
+
# Hàm lưu kết quả lên Hugging Face Dataset
|
37 |
+
def save_to_huggingface(image_path, hf_token):
|
38 |
+
# Tạo tên thư mục và tên file dựa trên ngày giờ hiện tại
|
39 |
+
now = datetime.now()
|
40 |
+
folder_name = f"SetImages+{now.strftime('%d/%m/%Y')}"
|
41 |
+
file_name = f"Image+{now.strftime('%S/%M/%H')}.png"
|
42 |
+
|
43 |
+
# Khởi tạo API Hugging Face
|
44 |
+
api = HfApi(token=hf_token)
|
45 |
+
|
46 |
+
# Tải ảnh lên dataset
|
47 |
+
try:
|
48 |
+
api.upload_file(
|
49 |
+
path_or_fileobj=image_path,
|
50 |
+
path_in_repo=f"{folder_name}/{file_name}",
|
51 |
+
repo_id="ArrcttacsrjksX/Deffusion",
|
52 |
+
repo_type="dataset"
|
53 |
+
)
|
54 |
+
print(f"Uploaded: {folder_name}/{file_name}")
|
55 |
+
except Exception as e:
|
56 |
+
print(f"Failed to upload: {e}")
|
57 |
+
|
58 |
# Hàm xử lý chạy ứng dụng
|
59 |
def run_command(
|
60 |
prompt, mode, height, width, steps, seed, cfg_scale, strength, sampling_method,
|
|
|
119 |
# Kiểm tra kết quả và trả về
|
120 |
if process.returncode == 0:
|
121 |
output_path = "./output.png" # Đường dẫn ảnh đầu ra mặc định
|
122 |
+
if os.path.exists(output_path):
|
123 |
+
# Lưu ảnh lên Hugging Face Dataset
|
124 |
+
save_to_huggingface(output_path, hf_token)
|
125 |
+
return output_path, "\n".join(logs)
|
126 |
+
else:
|
127 |
+
return None, "\n".join(logs)
|
128 |
else:
|
129 |
error_log = process.stderr.read() # Đọc lỗi
|
130 |
logs.append(error_log)
|