Spaces:
Running
on
Zero
Running
on
Zero
local file llok
Browse files
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 |
-
|
| 166 |
-
original_filename = f"image_{i}"
|
|
|
|
| 167 |
if isinstance(file_data_obj, dict):
|
| 168 |
-
|
| 169 |
original_filename = file_data_obj.get('name', original_filename)
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
|
|
|
| 173 |
|
| 174 |
-
if not
|
| 175 |
-
print(f"Error: Could not extract
|
| 176 |
-
# Skip this item or raise an error, for now, skip
|
| 177 |
continue
|
| 178 |
|
| 179 |
try:
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 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(
|
| 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}")
|