Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -134,14 +134,18 @@ async def upload_image(file: UploadFile = File(...)):
|
|
134 |
|
135 |
|
136 |
#Tranining Data set start fitering data for traninig
|
|
|
|
|
|
|
137 |
T_REPO_ID = "rahul7star/ohamlab"
|
138 |
T_FOLDER = "demo"
|
139 |
-
T_BASE_URL = f"https://huggingface.co/{
|
140 |
|
141 |
DESCRIPTION_TEXT = (
|
142 |
"Ra3hul is wearing a black jacket over a striped white t-shirt with blue jeans. "
|
143 |
"He is standing near a lake with his arms spread wide open, with mountains and cloudy skies in the background."
|
144 |
)
|
|
|
145 |
@app.post("/filter-images")
|
146 |
def filter_and_rename_images():
|
147 |
try:
|
@@ -150,7 +154,6 @@ def filter_and_rename_images():
|
|
150 |
filter_folder = f"filter-{T_FOLDER}"
|
151 |
filter_prefix = filter_folder + "/"
|
152 |
|
153 |
-
# Get images only directly in original folder
|
154 |
image_files = [
|
155 |
f for f in all_files
|
156 |
if f.startswith(folder_prefix)
|
@@ -163,50 +166,40 @@ def filter_and_rename_images():
|
|
163 |
|
164 |
uploaded_files = []
|
165 |
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
)
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
# Upload text file
|
198 |
-
upload_file(
|
199 |
-
path_or_fileobj=txt_path,
|
200 |
-
path_in_repo=filter_prefix + txt_filename,
|
201 |
-
repo_id=T_REPO_ID,
|
202 |
-
repo_type="model",
|
203 |
-
commit_message=f"Upload text file {txt_filename} to {filter_folder}",
|
204 |
-
token=True,
|
205 |
-
)
|
206 |
-
uploaded_files.append(filter_prefix + txt_filename)
|
207 |
|
208 |
return {"message": f"Processed and uploaded {len(image_files)} images and text files.", "files": uploaded_files}
|
209 |
|
210 |
except Exception as e:
|
211 |
return {"error": str(e)}
|
|
|
212 |
|
|
|
134 |
|
135 |
|
136 |
#Tranining Data set start fitering data for traninig
|
137 |
+
|
138 |
+
|
139 |
+
|
140 |
T_REPO_ID = "rahul7star/ohamlab"
|
141 |
T_FOLDER = "demo"
|
142 |
+
T_BASE_URL = f"https://huggingface.co/{T_REPO_ID}/resolve/main/"
|
143 |
|
144 |
DESCRIPTION_TEXT = (
|
145 |
"Ra3hul is wearing a black jacket over a striped white t-shirt with blue jeans. "
|
146 |
"He is standing near a lake with his arms spread wide open, with mountains and cloudy skies in the background."
|
147 |
)
|
148 |
+
|
149 |
@app.post("/filter-images")
|
150 |
def filter_and_rename_images():
|
151 |
try:
|
|
|
154 |
filter_folder = f"filter-{T_FOLDER}"
|
155 |
filter_prefix = filter_folder + "/"
|
156 |
|
|
|
157 |
image_files = [
|
158 |
f for f in all_files
|
159 |
if f.startswith(folder_prefix)
|
|
|
166 |
|
167 |
uploaded_files = []
|
168 |
|
169 |
+
for idx, orig_path in enumerate(image_files, start=1):
|
170 |
+
# Download file content into bytes (no local disk)
|
171 |
+
local_path = hf_hub_download(repo_id=T_REPO_ID, filename=orig_path)
|
172 |
+
with open(local_path, "rb") as f:
|
173 |
+
file_bytes = f.read()
|
174 |
+
|
175 |
+
new_image_name = f"image{idx}.jpeg" # rename all to .jpeg
|
176 |
+
|
177 |
+
# Upload image bytes directly from memory
|
178 |
+
upload_file(
|
179 |
+
path_or_fileobj=io.BytesIO(file_bytes),
|
180 |
+
path_in_repo=filter_prefix + new_image_name,
|
181 |
+
repo_id=T_REPO_ID,
|
182 |
+
repo_type="model",
|
183 |
+
commit_message=f"Upload renamed image {new_image_name} to {filter_folder}",
|
184 |
+
token=True,
|
185 |
+
)
|
186 |
+
uploaded_files.append(filter_prefix + new_image_name)
|
187 |
+
|
188 |
+
# Create and upload text file from string bytes
|
189 |
+
txt_filename = f"image{idx}.txt"
|
190 |
+
upload_file(
|
191 |
+
path_or_fileobj=io.BytesIO(DESCRIPTION_TEXT.encode("utf-8")),
|
192 |
+
path_in_repo=filter_prefix + txt_filename,
|
193 |
+
repo_id=T_REPO_ID,
|
194 |
+
repo_type="model",
|
195 |
+
commit_message=f"Upload text file {txt_filename} to {filter_folder}",
|
196 |
+
token=True,
|
197 |
+
)
|
198 |
+
uploaded_files.append(filter_prefix + txt_filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
|
200 |
return {"message": f"Processed and uploaded {len(image_files)} images and text files.", "files": uploaded_files}
|
201 |
|
202 |
except Exception as e:
|
203 |
return {"error": str(e)}
|
204 |
+
|
205 |
|