Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,9 @@ import json
|
|
| 4 |
import random
|
| 5 |
from datetime import datetime
|
| 6 |
import os
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Get access token from environment
|
| 9 |
access_token = os.environ.get("HUGGINGFACE_TOKEN")
|
|
@@ -13,7 +16,31 @@ class DatasetViewer:
|
|
| 13 |
self.dataset = None
|
| 14 |
self.dataset_size = 0
|
| 15 |
self.last_refresh_time = None
|
|
|
|
| 16 |
self.load_dataset()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def load_dataset(self):
|
| 19 |
"""Load the complete dataset into memory"""
|
|
@@ -53,8 +80,11 @@ class DatasetViewer:
|
|
| 53 |
|
| 54 |
# Append the triple (post_info, source_image, edited_image)
|
| 55 |
results.append(markdown_text)
|
| 56 |
-
results
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
return tuple(results)
|
| 60 |
|
|
|
|
| 4 |
import random
|
| 5 |
from datetime import datetime
|
| 6 |
import os
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import io
|
| 9 |
+
import numpy as np
|
| 10 |
|
| 11 |
# Get access token from environment
|
| 12 |
access_token = os.environ.get("HUGGINGFACE_TOKEN")
|
|
|
|
| 16 |
self.dataset = None
|
| 17 |
self.dataset_size = 0
|
| 18 |
self.last_refresh_time = None
|
| 19 |
+
self.max_display_size = (800, 600) # Maximum width and height for displayed images
|
| 20 |
self.load_dataset()
|
| 21 |
+
|
| 22 |
+
def resize_image(self, image):
|
| 23 |
+
"""Resize image keeping aspect ratio with a maximum size constraint"""
|
| 24 |
+
if isinstance(image, np.ndarray):
|
| 25 |
+
# Convert numpy array to PIL Image
|
| 26 |
+
image = Image.fromarray(image)
|
| 27 |
+
elif isinstance(image, bytes):
|
| 28 |
+
# Convert bytes to PIL Image
|
| 29 |
+
image = Image.open(io.BytesIO(image))
|
| 30 |
+
|
| 31 |
+
# Calculate scaling factor to fit within max dimensions
|
| 32 |
+
width_ratio = self.max_display_size[0] / image.width
|
| 33 |
+
height_ratio = self.max_display_size[1] / image.height
|
| 34 |
+
scale_factor = min(width_ratio, height_ratio)
|
| 35 |
+
|
| 36 |
+
# Only resize if image is larger than max dimensions
|
| 37 |
+
if scale_factor < 1:
|
| 38 |
+
new_width = int(image.width * scale_factor)
|
| 39 |
+
new_height = int(image.height * scale_factor)
|
| 40 |
+
image = image.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
| 41 |
+
|
| 42 |
+
# Convert back to numpy array for gradio
|
| 43 |
+
return np.array(image)
|
| 44 |
|
| 45 |
def load_dataset(self):
|
| 46 |
"""Load the complete dataset into memory"""
|
|
|
|
| 80 |
|
| 81 |
# Append the triple (post_info, source_image, edited_image)
|
| 82 |
results.append(markdown_text)
|
| 83 |
+
# Resize images before adding to results
|
| 84 |
+
source_image = self.resize_image(sample["source_image"])
|
| 85 |
+
edited_image = self.resize_image(sample["edited_image"])
|
| 86 |
+
results.append(source_image)
|
| 87 |
+
results.append(edited_image)
|
| 88 |
|
| 89 |
return tuple(results)
|
| 90 |
|