im commited on
Commit
d933c69
·
1 Parent(s): e9eeafb
Files changed (1) hide show
  1. app.py +79 -5
app.py CHANGED
@@ -82,20 +82,94 @@
82
 
83
 
84
 
85
- import os
 
 
 
86
  import gradio as gr
87
  from encoder import FashionCLIPEncoder
88
- from process import batch_process_images
 
 
 
 
 
89
 
90
  # Initialize encoder
91
  encoder = FashionCLIPEncoder()
92
 
93
- # Constants
94
- BATCH_SIZE = 30 # Define batch size for processing
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
  # Gradio Interface
97
  iface = gr.Interface(
98
- fn=lambda image_urls: batch_process_images(image_urls, encoder, BATCH_SIZE),
99
  inputs=gr.Textbox(
100
  lines=5,
101
  placeholder="Enter image URLs separated by commas",
 
82
 
83
 
84
 
85
+ import uuid
86
+ import requests
87
+ from PIL import Image
88
+ import numpy as np
89
  import gradio as gr
90
  from encoder import FashionCLIPEncoder
91
+
92
+ # Constants
93
+ REQUESTS_HEADERS = {
94
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
95
+ }
96
+ BATCH_SIZE = 30 # Define batch size for processing
97
 
98
  # Initialize encoder
99
  encoder = FashionCLIPEncoder()
100
 
101
+ # Helper function to download images
102
+ def download_image_as_pil(url: str, timeout: int = 10) -> Image.Image:
103
+ try:
104
+ response = requests.get(url, stream=True, headers=REQUESTS_HEADERS, timeout=timeout)
105
+ if response.status_code == 200:
106
+ return Image.open(response.raw).convert("RGB") # Ensure consistent format
107
+ return None
108
+ except Exception as e:
109
+ print(f"Error downloading image: {e}")
110
+ return None
111
+
112
+ # Embedding function for a batch of images
113
+ def batch_process_images(image_urls: str):
114
+ # Split the input string by commas and strip whitespace
115
+ urls = [url.strip() for url in image_urls.split(",") if url.strip()]
116
+
117
+ if not urls:
118
+ return {"error": "No valid image URLs provided."}
119
+
120
+ results = []
121
+ batch_urls, batch_images = [], []
122
+
123
+ for url in urls:
124
+ try:
125
+ # Download image
126
+ image = download_image_as_pil(url)
127
+ if not image:
128
+ results.append({"image_url": url, "error": "Failed to download image"})
129
+ continue
130
+
131
+ batch_urls.append(url)
132
+ batch_images.append(image)
133
+
134
+ # Process batch when reaching batch size
135
+ if len(batch_images) == BATCH_SIZE:
136
+ process_batch(batch_urls, batch_images, results)
137
+ batch_urls, batch_images = [], []
138
+
139
+ except Exception as e:
140
+ results.append({"image_url": url, "error": str(e)})
141
+
142
+ # Process remaining images in the last batch
143
+ if batch_images:
144
+ process_batch(batch_urls, batch_images, results)
145
+
146
+ return results
147
+
148
+
149
+ # Helper function to process a batch
150
+ def process_batch(batch_urls, batch_images, results):
151
+ try:
152
+ # Generate embeddings
153
+ embeddings = encoder.encode_images(batch_images)
154
+
155
+ for url, embedding in zip(batch_urls, embeddings):
156
+ # Normalize embedding
157
+ embedding_normalized = embedding / np.linalg.norm(embedding)
158
+
159
+ # Append results
160
+ results.append({
161
+ "image_url": url,
162
+ "embedding_preview": embedding_normalized[:5].tolist(), # First 5 values for preview
163
+ "success": True
164
+ })
165
+ except Exception as e:
166
+ for url in batch_urls:
167
+ results.append({"image_url": url, "error": str(e)})
168
+
169
 
170
  # Gradio Interface
171
  iface = gr.Interface(
172
+ fn=batch_process_images,
173
  inputs=gr.Textbox(
174
  lines=5,
175
  placeholder="Enter image URLs separated by commas",