dkatz2391 commited on
Commit
1b6d4e0
·
verified ·
1 Parent(s): 07891e3

local file llok

Browse files
Files changed (1) hide show
  1. app.py +22 -31
app.py CHANGED
@@ -162,43 +162,34 @@ def generate_model_from_images_and_upload(
162
  # print(f"Received image_inputs: {image_inputs}, input_type: {input_type}") # Original log, can be removed if too verbose
163
 
164
  for i, file_data_obj in enumerate(image_inputs):
165
- img_data_url = None
166
- original_filename = f"image_{i}" # Default filename
 
167
  if isinstance(file_data_obj, dict):
168
- img_data_url = file_data_obj.get('path') or file_data_obj.get('url') # Prefer path, fallback to url
169
  original_filename = file_data_obj.get('name', original_filename)
170
- elif isinstance(file_data_obj, str): # Fallback if it somehow still comes as a plain string
171
- img_data_url = file_data_obj
172
- original_filename = os.path.basename(img_data_url).split('.')[0] or original_filename
 
173
 
174
- if not img_data_url:
175
- print(f"Error: Could not extract URL from file_data_obj {i}: {file_data_obj}")
176
- # Skip this item or raise an error, for now, skip
177
  continue
178
 
179
  try:
180
- print(f"Processing image {i+1}/{len(image_inputs)} from URL: {img_data_url} (original name: {original_filename}) with type '{input_type}'")
181
- if input_type == "url": # This check is still relevant based on what Node.js sends as input_type
182
- # print(f"Fetching image from URL: {img_data_url}") # Already printed
183
- response_img = requests.get(img_data_url, stream=True, timeout=30)
184
- response_img.raise_for_status()
185
- img = Image.open(response_img.raw)
186
- image_basenames.append(os.path.basename(img_data_url).split('.')[0] or f"image_{i}")
187
- # Note: The base64 and filepath cases from original code might be less relevant now
188
- # if the server always sends FileData-like objects with type "url".
189
- # Consider if you still need to support direct base64/filepath strings in image_inputs list here.
190
- elif input_type == "base64":
191
- print(f"Decoding base64 image data (first 30 chars): {img_data_url[:30]}...")
192
- missing_padding = len(img_data_url) % 4
193
- if missing_padding:
194
- img_data_url += '=' * (4 - missing_padding)
195
- img_bytes = base64.b64decode(img_data_url)
196
- img = Image.open(io.BytesIO(img_bytes))
197
- image_basenames.append(f"base64_image_{i}")
198
- else:
199
- print(f"Unsupported input_type: {input_type} for FileData object flow")
200
- raise ValueError(f"Unsupported input_type: {input_type} for FileData object flow")
201
 
 
 
 
 
 
 
202
  print(f"Image {i+1} loaded, mode: {img.mode}, size: {img.size}. Preprocessing...")
203
  # Ensure image is in RGB format if it's not, e.g. RGBA or P
204
  if img.mode == 'RGBA' or img.mode == 'P':
@@ -210,7 +201,7 @@ def generate_model_from_images_and_upload(
210
  print(f"Image {i+1} processed and added.")
211
 
212
  except Exception as e:
213
- print(f"Error processing image {i} ('{str(img_data_url)[:50]}...'): {e}")
214
  import traceback
215
  traceback.print_exc()
216
  raise gr.Error(f"Failed to load or process input image {i} ({input_type}): {e}")
 
162
  # print(f"Received image_inputs: {image_inputs}, input_type: {input_type}") # Original log, can be removed if too verbose
163
 
164
  for i, file_data_obj in enumerate(image_inputs):
165
+ img_local_path = None
166
+ original_filename = f"image_{i}"
167
+
168
  if isinstance(file_data_obj, dict):
169
+ img_local_path = file_data_obj.get('path')
170
  original_filename = file_data_obj.get('name', original_filename)
171
+ print(f"DEBUG: Extracted local path: {img_local_path}, original_filename: {original_filename} from dict: {file_data_obj}")
172
+ # elif isinstance(file_data_obj, str): # This fallback may not be needed if Gradio always gives a dict with gr.JSON
173
+ # img_local_path = file_data_obj
174
+ # original_filename = os.path.basename(img_local_path).split('.')[0] or original_filename
175
 
176
+ if not img_local_path:
177
+ print(f"Error: Could not extract local path from file_data_obj {i}: {file_data_obj}")
 
178
  continue
179
 
180
  try:
181
+ # The input_type check is less about HOW to load (it's always local now), but can be kept for logging/context
182
+ print(f"Processing image {i+1}/{len(image_inputs)} from LOCAL PATH: {img_local_path} (original name: {original_filename}), received input_type: '{input_type}'")
183
+
184
+ # DIRECTLY OPEN THE LOCAL FILE provided by Gradio
185
+ img = Image.open(img_local_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
 
187
+ # Use original_filename (which might have been extracted from the FileData object)
188
+ image_basenames.append(original_filename.split('.')[0] or f"image_{i}")
189
+
190
+ # The old requests.get() and base64 decoding sections are no longer needed here
191
+ # as Gradio handles the initial download/provisioning of the file to a local path.
192
+
193
  print(f"Image {i+1} loaded, mode: {img.mode}, size: {img.size}. Preprocessing...")
194
  # Ensure image is in RGB format if it's not, e.g. RGBA or P
195
  if img.mode == 'RGBA' or img.mode == 'P':
 
201
  print(f"Image {i+1} processed and added.")
202
 
203
  except Exception as e:
204
+ print(f"Error processing image {i} ('{str(img_local_path)[:50]}...'): {e}")
205
  import traceback
206
  traceback.print_exc()
207
  raise gr.Error(f"Failed to load or process input image {i} ({input_type}): {e}")