rahul7star commited on
Commit
27cc4b8
·
verified ·
1 Parent(s): 05e9406

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -43
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/{REPO_ID}/resolve/main/"
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
- with tempfile.TemporaryDirectory() as tmpdir:
167
- for idx, orig_path in enumerate(image_files, start=1):
168
- # Download original image to temp dir
169
- local_path = hf_hub_download(repo_id=REPO_ID, filename=orig_path)
170
-
171
- # Determine new image filename - all renamed to .jpeg (can keep original ext if you want)
172
- new_image_name = f"image{idx}.jpeg"
173
- new_image_path = os.path.join(tmpdir, new_image_name)
174
-
175
- # Convert/Copy image to new path (simple copy for now)
176
- # If needed, use PIL to convert formats. Here, just copy
177
- with open(local_path, "rb") as src_file, open(new_image_path, "wb") as dst_file:
178
- dst_file.write(src_file.read())
179
-
180
- # Upload renamed image to HF under filter folder
181
- upload_file(
182
- path_or_fileobj=new_image_path,
183
- path_in_repo=filter_prefix + new_image_name,
184
- repo_id=T_REPO_ID,
185
- repo_type="model",
186
- commit_message=f"Upload renamed image {new_image_name} to {filter_folder}",
187
- token=True,
188
- )
189
- uploaded_files.append(filter_prefix + new_image_name)
190
-
191
- # Create corresponding .txt file with description
192
- txt_filename = f"image{idx}.txt"
193
- txt_path = os.path.join(tmpdir, txt_filename)
194
- with open(txt_path, "w") as txt_file:
195
- txt_file.write(DESCRIPTION_TEXT)
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