rahul7star commited on
Commit
685a3eb
·
verified ·
1 Parent(s): c7f856b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -14
app.py CHANGED
@@ -144,44 +144,52 @@ async def upload_image(file: UploadFile = File(...)):
144
 
145
 
146
 
147
- T_REPO_ID = "rahul7star/ohamlab"
148
- T_FOLDER = "demo"
149
- T_BASE_URL = f"https://huggingface.co/{T_REPO_ID}/resolve/main/"
150
 
 
 
 
151
  DESCRIPTION_TEXT = (
152
  "Ra3hul is wearing a black jacket over a striped white t-shirt with blue jeans. "
153
  "He is standing near a lake with his arms spread wide open, with mountains and cloudy skies in the background."
154
  )
155
 
 
 
 
156
  @app.post("/filter-images")
157
- def filter_and_rename_images():
158
  try:
159
  all_files = list_repo_files(T_REPO_ID)
160
- folder_prefix = T_FOLDER.rstrip("/") + "/"
161
- filter_folder = f"filter-{T_FOLDER}"
162
  filter_prefix = filter_folder + "/"
163
 
 
164
  image_files = [
165
  f for f in all_files
166
  if f.startswith(folder_prefix)
167
- and "/" not in f[len(folder_prefix):]
168
- and f.lower().endswith((".png", ".jpg", ".jpeg", ".webp"))
169
  ]
170
 
171
  if not image_files:
172
- return {"error": f"No images found in folder '{T_FOLDER}'"}
173
 
174
  uploaded_files = []
175
 
176
  for idx, orig_path in enumerate(image_files, start=1):
177
- # Download file content into bytes (no local disk)
178
  local_path = hf_hub_download(repo_id=T_REPO_ID, filename=orig_path)
179
  with open(local_path, "rb") as f:
180
  file_bytes = f.read()
181
 
182
- new_image_name = f"image{idx}.jpeg" # rename all to .jpeg
 
183
 
184
- # Upload image bytes directly from memory
185
  upload_file(
186
  path_or_fileobj=io.BytesIO(file_bytes),
187
  path_in_repo=filter_prefix + new_image_name,
@@ -192,7 +200,7 @@ def filter_and_rename_images():
192
  )
193
  uploaded_files.append(filter_prefix + new_image_name)
194
 
195
- # Create and upload text file from string bytes
196
  txt_filename = f"image{idx}.txt"
197
  upload_file(
198
  path_or_fileobj=io.BytesIO(DESCRIPTION_TEXT.encode("utf-8")),
@@ -204,7 +212,10 @@ def filter_and_rename_images():
204
  )
205
  uploaded_files.append(filter_prefix + txt_filename)
206
 
207
- return {"message": f"Processed and uploaded {len(image_files)} images and text files.", "files": uploaded_files}
 
 
 
208
 
209
  except Exception as e:
210
  return {"error": str(e)}
 
144
 
145
 
146
 
147
+ import io
148
+ from fastapi import FastAPI, Query
149
+ from huggingface_hub import list_repo_files, hf_hub_download, upload_file
150
 
151
+ app = FastAPI()
152
+
153
+ T_REPO_ID = "rahul7star/ohamlab"
154
  DESCRIPTION_TEXT = (
155
  "Ra3hul is wearing a black jacket over a striped white t-shirt with blue jeans. "
156
  "He is standing near a lake with his arms spread wide open, with mountains and cloudy skies in the background."
157
  )
158
 
159
+ def is_image_file(filename: str) -> bool:
160
+ return filename.lower().endswith((".png", ".jpg", ".jpeg", ".webp"))
161
+
162
  @app.post("/filter-images")
163
+ def filter_and_rename_images(folder: str = Query("demo", description="Folder path in repo to scan")):
164
  try:
165
  all_files = list_repo_files(T_REPO_ID)
166
+ folder_prefix = folder.rstrip("/") + "/"
167
+ filter_folder = f"filter-{folder.rstrip('/')}"
168
  filter_prefix = filter_folder + "/"
169
 
170
+ # Filter images only directly in the folder (no subfolders)
171
  image_files = [
172
  f for f in all_files
173
  if f.startswith(folder_prefix)
174
+ and "/" not in f[len(folder_prefix):] # no deeper path
175
+ and is_image_file(f)
176
  ]
177
 
178
  if not image_files:
179
+ return {"error": f"No images found in folder '{folder}'"}
180
 
181
  uploaded_files = []
182
 
183
  for idx, orig_path in enumerate(image_files, start=1):
184
+ # Download image content bytes (uses local cache)
185
  local_path = hf_hub_download(repo_id=T_REPO_ID, filename=orig_path)
186
  with open(local_path, "rb") as f:
187
  file_bytes = f.read()
188
 
189
+ # Rename images as image1.jpeg, image2.jpeg, ...
190
+ new_image_name = f"image{idx}.jpeg"
191
 
192
+ # Upload renamed image from memory
193
  upload_file(
194
  path_or_fileobj=io.BytesIO(file_bytes),
195
  path_in_repo=filter_prefix + new_image_name,
 
200
  )
201
  uploaded_files.append(filter_prefix + new_image_name)
202
 
203
+ # Create and upload text file for each image
204
  txt_filename = f"image{idx}.txt"
205
  upload_file(
206
  path_or_fileobj=io.BytesIO(DESCRIPTION_TEXT.encode("utf-8")),
 
212
  )
213
  uploaded_files.append(filter_prefix + txt_filename)
214
 
215
+ return {
216
+ "message": f"Processed and uploaded {len(image_files)} images and text files.",
217
+ "files": uploaded_files,
218
+ }
219
 
220
  except Exception as e:
221
  return {"error": str(e)}