Spaces:
Running
on
Zero
Running
on
Zero
wjm55
commited on
Commit
·
266e1d2
1
Parent(s):
44b7045
download ignores image
Browse files
app.py
CHANGED
|
@@ -256,56 +256,64 @@ with gr.Blocks(css=css) as demo:
|
|
| 256 |
download_output = gr.File(label="Download")
|
| 257 |
|
| 258 |
def create_zip(image, text_data, fname):
|
| 259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
return None
|
| 261 |
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
image
|
| 265 |
-
|
| 266 |
-
# Create a temporary directory
|
| 267 |
-
with tempfile.TemporaryDirectory() as temp_dir:
|
| 268 |
-
# Save image
|
| 269 |
-
img_ext = image.format.lower() if hasattr(image, 'format') else 'png'
|
| 270 |
-
img_path = os.path.join(temp_dir, f"{fname}.{img_ext}")
|
| 271 |
-
image.save(img_path)
|
| 272 |
-
|
| 273 |
-
# Extract text and entities from the HighlightedText output
|
| 274 |
-
text, entities = text_data[0] if isinstance(text_data, list) else (text_data, [])
|
| 275 |
-
|
| 276 |
-
# Save text
|
| 277 |
-
txt_path = os.path.join(temp_dir, f"{fname}.txt")
|
| 278 |
-
with open(txt_path, 'w', encoding='utf-8') as f:
|
| 279 |
-
f.write(text)
|
| 280 |
-
|
| 281 |
-
# Create JSON with text, entities, and image info
|
| 282 |
-
json_data = {
|
| 283 |
-
"text": text,
|
| 284 |
-
"entities": [
|
| 285 |
-
{
|
| 286 |
-
"start": start,
|
| 287 |
-
"end": end,
|
| 288 |
-
"label": label,
|
| 289 |
-
"text": text[start:end]
|
| 290 |
-
}
|
| 291 |
-
for start, end, label in entities
|
| 292 |
-
],
|
| 293 |
-
"image_file": f"{fname}.{img_ext}"
|
| 294 |
-
}
|
| 295 |
-
|
| 296 |
-
# Save JSON
|
| 297 |
-
json_path = os.path.join(temp_dir, f"{fname}.json")
|
| 298 |
-
with open(json_path, 'w', encoding='utf-8') as f:
|
| 299 |
-
json.dump(json_data, f, indent=2, ensure_ascii=False)
|
| 300 |
-
|
| 301 |
-
# Create zip file
|
| 302 |
-
zip_path = os.path.join(temp_dir, f"{fname}.zip")
|
| 303 |
-
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
| 304 |
-
zipf.write(img_path, os.path.basename(img_path))
|
| 305 |
-
zipf.write(txt_path, os.path.basename(txt_path))
|
| 306 |
-
zipf.write(json_path, os.path.basename(json_path))
|
| 307 |
|
| 308 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
|
| 310 |
# Add the click handler for the download button
|
| 311 |
download_btn.click(
|
|
|
|
| 256 |
download_output = gr.File(label="Download")
|
| 257 |
|
| 258 |
def create_zip(image, text_data, fname):
|
| 259 |
+
# Validate inputs
|
| 260 |
+
if not fname:
|
| 261 |
+
return None
|
| 262 |
+
if not text_data:
|
| 263 |
+
return None
|
| 264 |
+
if not isinstance(image, (Image.Image, np.ndarray)):
|
| 265 |
return None
|
| 266 |
|
| 267 |
+
try:
|
| 268 |
+
# Convert numpy array to PIL Image if needed
|
| 269 |
+
if isinstance(image, np.ndarray):
|
| 270 |
+
image = Image.fromarray(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
|
| 272 |
+
# Create a temporary directory
|
| 273 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 274 |
+
# Save image with default png extension
|
| 275 |
+
img_path = os.path.join(temp_dir, f"{fname}.png")
|
| 276 |
+
image.save(img_path)
|
| 277 |
+
|
| 278 |
+
# Extract text content
|
| 279 |
+
text = ""
|
| 280 |
+
entities = []
|
| 281 |
+
if isinstance(text_data, list) and len(text_data) > 0:
|
| 282 |
+
if isinstance(text_data[0], tuple):
|
| 283 |
+
text = text_data[0][0] # Get text from first tuple
|
| 284 |
+
else:
|
| 285 |
+
text = str(text_data[0]) # Convert to string if not tuple
|
| 286 |
+
|
| 287 |
+
# Save text
|
| 288 |
+
txt_path = os.path.join(temp_dir, f"{fname}.txt")
|
| 289 |
+
with open(txt_path, 'w', encoding='utf-8') as f:
|
| 290 |
+
f.write(text)
|
| 291 |
+
|
| 292 |
+
# Create JSON with text and entities
|
| 293 |
+
json_data = {
|
| 294 |
+
"text": text,
|
| 295 |
+
"entities": entities,
|
| 296 |
+
"image_file": f"{fname}.png"
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
# Save JSON
|
| 300 |
+
json_path = os.path.join(temp_dir, f"{fname}.json")
|
| 301 |
+
with open(json_path, 'w', encoding='utf-8') as f:
|
| 302 |
+
json.dump(json_data, f, indent=2, ensure_ascii=False)
|
| 303 |
+
|
| 304 |
+
# Create zip file
|
| 305 |
+
zip_path = os.path.join(temp_dir, f"{fname}.zip")
|
| 306 |
+
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
| 307 |
+
zipf.write(img_path, os.path.basename(img_path))
|
| 308 |
+
zipf.write(txt_path, os.path.basename(txt_path))
|
| 309 |
+
zipf.write(json_path, os.path.basename(json_path))
|
| 310 |
+
|
| 311 |
+
# Read the zip file into memory
|
| 312 |
+
with open(zip_path, 'rb') as f:
|
| 313 |
+
return (f"{fname}.zip", f.read())
|
| 314 |
+
except Exception as e:
|
| 315 |
+
print(f"Error creating zip: {str(e)}")
|
| 316 |
+
return None
|
| 317 |
|
| 318 |
# Add the click handler for the download button
|
| 319 |
download_btn.click(
|