Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -40,6 +40,45 @@ def decode_base64_image(base64_str: str) -> Optional[Image.Image]:
|
|
| 40 |
return None
|
| 41 |
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
app = FastAPI(title="Kimi Service", version="1.5.0")
|
| 44 |
inference = Inference()
|
| 45 |
router = APIRouter()
|
|
@@ -61,7 +100,13 @@ async def classify(request: ClassificationRequest):
|
|
| 61 |
images.append(img)
|
| 62 |
log.info(f"Decoded {len(images)} images successfully")
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
if res is None:
|
| 66 |
raise HTTPException(status_code=500, detail="Classification failed")
|
| 67 |
return res
|
|
|
|
| 40 |
return None
|
| 41 |
|
| 42 |
|
| 43 |
+
def save_images_to_disk(images: List[Image.Image], output_dir: str = "temp_images") -> List[str]:
|
| 44 |
+
"""
|
| 45 |
+
Save PIL Image objects to disk and return their file paths.
|
| 46 |
+
|
| 47 |
+
Args:
|
| 48 |
+
images (List[Image.Image]): List of PIL Image objects to save
|
| 49 |
+
output_dir (str): Directory where images will be saved (default: "temp_images")
|
| 50 |
+
|
| 51 |
+
Returns:
|
| 52 |
+
List[str]: List of file paths where images were saved
|
| 53 |
+
|
| 54 |
+
Raises:
|
| 55 |
+
Exception: If there's an error saving images to disk
|
| 56 |
+
"""
|
| 57 |
+
try:
|
| 58 |
+
# Create output directory if it doesn't exist
|
| 59 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 60 |
+
|
| 61 |
+
saved_paths = []
|
| 62 |
+
for i, image in enumerate(images):
|
| 63 |
+
if image is None:
|
| 64 |
+
log.warning(f"Skipping None image at index {i}")
|
| 65 |
+
continue
|
| 66 |
+
|
| 67 |
+
# Generate unique filename
|
| 68 |
+
filename = f"image_{uuid.uuid4().hex}.png"
|
| 69 |
+
file_path = os.path.join(output_dir, filename)
|
| 70 |
+
|
| 71 |
+
# Save image to disk
|
| 72 |
+
image.save(file_path, "PNG")
|
| 73 |
+
saved_paths.append(file_path)
|
| 74 |
+
log.info(f"Saved image to: {file_path}")
|
| 75 |
+
|
| 76 |
+
return saved_paths
|
| 77 |
+
|
| 78 |
+
except Exception as e:
|
| 79 |
+
log.error(f"Error saving images to disk: {str(e)}")
|
| 80 |
+
raise
|
| 81 |
+
|
| 82 |
app = FastAPI(title="Kimi Service", version="1.5.0")
|
| 83 |
inference = Inference()
|
| 84 |
router = APIRouter()
|
|
|
|
| 100 |
images.append(img)
|
| 101 |
log.info(f"Decoded {len(images)} images successfully")
|
| 102 |
|
| 103 |
+
# Save images and get their paths using a helper method
|
| 104 |
+
output_dir = os.environ.get("IMAGE_OUTPUT_DIR", "/tmp/temp_images")
|
| 105 |
+
saved_image_paths = save_images_to_disk(images, output_dir)
|
| 106 |
+
|
| 107 |
+
# Send images to inference
|
| 108 |
+
res = inference.classify_building(images, saved_image_paths)
|
| 109 |
+
|
| 110 |
if res is None:
|
| 111 |
raise HTTPException(status_code=500, detail="Classification failed")
|
| 112 |
return res
|