Spaces:
Runtime error
Runtime error
random filename generator
Browse files
utils.py
CHANGED
@@ -8,7 +8,7 @@ from datetime import datetime
|
|
8 |
from threading import Thread
|
9 |
from huggingface_hub import Repository
|
10 |
import subprocess
|
11 |
-
|
12 |
import requests
|
13 |
import streamlit as st
|
14 |
import torch
|
@@ -82,12 +82,15 @@ def clear_memory(preserve):
|
|
82 |
|
83 |
from huggingface_hub import Repository
|
84 |
|
|
|
|
|
|
|
85 |
def save_to_hub(image, current_datetime, metadata, output_path):
|
86 |
"""Saves an image to Hugging Face Hub"""
|
87 |
try:
|
88 |
# Convert image to byte array
|
89 |
byte_arr = io.BytesIO()
|
90 |
-
|
91 |
# Check if the image has metadata
|
92 |
if image.info:
|
93 |
# Save as PNG
|
@@ -95,13 +98,12 @@ def save_to_hub(image, current_datetime, metadata, output_path):
|
|
95 |
else:
|
96 |
# Save as JPG
|
97 |
image.save(byte_arr, format='JPEG')
|
98 |
-
|
99 |
byte_arr = byte_arr.getvalue()
|
100 |
|
101 |
# Create a repository object
|
102 |
token = os.getenv("HF_TOKEN")
|
103 |
api = HfApi()
|
104 |
-
|
105 |
username = "FFusion"
|
106 |
repo_name = "FF"
|
107 |
try:
|
@@ -109,6 +111,12 @@ def save_to_hub(image, current_datetime, metadata, output_path):
|
|
109 |
except RepositoryNotFoundError:
|
110 |
repo = Repository(f"{username}/{repo_name}", clone_from=f"{username}/{repo_name}", use_auth_token=token, repo_type="dataset")
|
111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
# Create the directory if it does not exist
|
113 |
os.makedirs(os.path.dirname(f"{repo.local_dir}/{output_path}"), exist_ok=True)
|
114 |
|
@@ -126,7 +134,6 @@ def save_to_hub(image, current_datetime, metadata, output_path):
|
|
126 |
print(f"Pushing changes to {username}/{repo_name}...")
|
127 |
repo.git_push()
|
128 |
print(f"Image saved to {username}/{repo_name}/{output_path}")
|
129 |
-
|
130 |
except Exception as e:
|
131 |
print(f"Failed to save image to Hugging Face Hub: {e}")
|
132 |
|
@@ -141,8 +148,6 @@ def save_to_hub(image, current_datetime, metadata, output_path):
|
|
141 |
|
142 |
|
143 |
|
144 |
-
|
145 |
-
|
146 |
|
147 |
|
148 |
|
|
|
8 |
from threading import Thread
|
9 |
from huggingface_hub import Repository
|
10 |
import subprocess
|
11 |
+
import random
|
12 |
import requests
|
13 |
import streamlit as st
|
14 |
import torch
|
|
|
82 |
|
83 |
from huggingface_hub import Repository
|
84 |
|
85 |
+
|
86 |
+
|
87 |
+
|
88 |
def save_to_hub(image, current_datetime, metadata, output_path):
|
89 |
"""Saves an image to Hugging Face Hub"""
|
90 |
try:
|
91 |
# Convert image to byte array
|
92 |
byte_arr = io.BytesIO()
|
93 |
+
|
94 |
# Check if the image has metadata
|
95 |
if image.info:
|
96 |
# Save as PNG
|
|
|
98 |
else:
|
99 |
# Save as JPG
|
100 |
image.save(byte_arr, format='JPEG')
|
101 |
+
|
102 |
byte_arr = byte_arr.getvalue()
|
103 |
|
104 |
# Create a repository object
|
105 |
token = os.getenv("HF_TOKEN")
|
106 |
api = HfApi()
|
|
|
107 |
username = "FFusion"
|
108 |
repo_name = "FF"
|
109 |
try:
|
|
|
111 |
except RepositoryNotFoundError:
|
112 |
repo = Repository(f"{username}/{repo_name}", clone_from=f"{username}/{repo_name}", use_auth_token=token, repo_type="dataset")
|
113 |
|
114 |
+
# Generate a random 10-digit number
|
115 |
+
random_number = random.randint(1000000000, 9999999999)
|
116 |
+
|
117 |
+
# Replace "0.png" in output_path with the random number
|
118 |
+
output_path = output_path.replace("0.png", f"{random_number}.png")
|
119 |
+
|
120 |
# Create the directory if it does not exist
|
121 |
os.makedirs(os.path.dirname(f"{repo.local_dir}/{output_path}"), exist_ok=True)
|
122 |
|
|
|
134 |
print(f"Pushing changes to {username}/{repo_name}...")
|
135 |
repo.git_push()
|
136 |
print(f"Image saved to {username}/{repo_name}/{output_path}")
|
|
|
137 |
except Exception as e:
|
138 |
print(f"Failed to save image to Hugging Face Hub: {e}")
|
139 |
|
|
|
148 |
|
149 |
|
150 |
|
|
|
|
|
151 |
|
152 |
|
153 |
|