Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -126,3 +126,86 @@ async def upload_image(file: UploadFile = File(...)):
|
|
126 |
return {"message": f"β
{filename} uploaded successfully!"}
|
127 |
except Exception as e:
|
128 |
return {"error": f"β Upload failed: {str(e)}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
return {"message": f"β
{filename} uploaded successfully!"}
|
127 |
except Exception as e:
|
128 |
return {"error": f"β Upload failed: {str(e)}"}
|
129 |
+
|
130 |
+
|
131 |
+
|
132 |
+
|
133 |
+
|
134 |
+
|
135 |
+
#Tranining Data set start fitering data for traninig
|
136 |
+
T_REPO_ID = "rahul7star/ohamlab"
|
137 |
+
T_FOLDER = "demo"
|
138 |
+
T_BASE_URL = f"https://huggingface.co/{REPO_ID}/resolve/main/"
|
139 |
+
|
140 |
+
DESCRIPTION_TEXT = (
|
141 |
+
"Ra3hul is wearing a black jacket over a striped white t-shirt with blue jeans. "
|
142 |
+
"He is standing near a lake with his arms spread wide open, with mountains and cloudy skies in the background."
|
143 |
+
)
|
144 |
+
@app.post("/filter-images")
|
145 |
+
def filter_and_rename_images():
|
146 |
+
try:
|
147 |
+
all_files = list_repo_files(T_REPO_ID)
|
148 |
+
folder_prefix = T_FOLDER.rstrip("/") + "/"
|
149 |
+
filter_folder = f"filter-{T_FOLDER}"
|
150 |
+
filter_prefix = filter_folder + "/"
|
151 |
+
|
152 |
+
# Get images only directly in original folder
|
153 |
+
image_files = [
|
154 |
+
f for f in all_files
|
155 |
+
if f.startswith(folder_prefix)
|
156 |
+
and "/" not in f[len(folder_prefix):]
|
157 |
+
and f.lower().endswith((".png", ".jpg", ".jpeg", ".webp"))
|
158 |
+
]
|
159 |
+
|
160 |
+
if not image_files:
|
161 |
+
return {"error": f"No images found in folder '{T_FOLDER}'"}
|
162 |
+
|
163 |
+
uploaded_files = []
|
164 |
+
|
165 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
166 |
+
for idx, orig_path in enumerate(image_files, start=1):
|
167 |
+
# Download original image to temp dir
|
168 |
+
local_path = hf_hub_download(repo_id=REPO_ID, filename=orig_path)
|
169 |
+
|
170 |
+
# Determine new image filename - all renamed to .jpeg (can keep original ext if you want)
|
171 |
+
new_image_name = f"image{idx}.jpeg"
|
172 |
+
new_image_path = os.path.join(tmpdir, new_image_name)
|
173 |
+
|
174 |
+
# Convert/Copy image to new path (simple copy for now)
|
175 |
+
# If needed, use PIL to convert formats. Here, just copy
|
176 |
+
with open(local_path, "rb") as src_file, open(new_image_path, "wb") as dst_file:
|
177 |
+
dst_file.write(src_file.read())
|
178 |
+
|
179 |
+
# Upload renamed image to HF under filter folder
|
180 |
+
upload_file(
|
181 |
+
path_or_fileobj=new_image_path,
|
182 |
+
path_in_repo=filter_prefix + new_image_name,
|
183 |
+
repo_id=REPO_ID,
|
184 |
+
repo_type="model",
|
185 |
+
commit_message=f"Upload renamed image {new_image_name} to {filter_folder}",
|
186 |
+
token=True,
|
187 |
+
)
|
188 |
+
uploaded_files.append(filter_prefix + new_image_name)
|
189 |
+
|
190 |
+
# Create corresponding .txt file with description
|
191 |
+
txt_filename = f"image{idx}.txt"
|
192 |
+
txt_path = os.path.join(tmpdir, txt_filename)
|
193 |
+
with open(txt_path, "w") as txt_file:
|
194 |
+
txt_file.write(DESCRIPTION_TEXT)
|
195 |
+
|
196 |
+
# Upload text file
|
197 |
+
upload_file(
|
198 |
+
path_or_fileobj=txt_path,
|
199 |
+
path_in_repo=filter_prefix + txt_filename,
|
200 |
+
repo_id=T_REPO_ID,
|
201 |
+
repo_type="model",
|
202 |
+
commit_message=f"Upload text file {txt_filename} to {filter_folder}",
|
203 |
+
token=True,
|
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)}
|
211 |
+
|