Spaces:
Runtime error
Runtime error
Upload 25 files
Browse files- src/.DS_Store +0 -0
- src/firebase/firebase_provider.py +16 -6
- src/utils/helper.py +31 -0
src/.DS_Store
CHANGED
|
Binary files a/src/.DS_Store and b/src/.DS_Store differ
|
|
|
src/firebase/firebase_provider.py
CHANGED
|
@@ -75,12 +75,14 @@ def download_file_from_storage(file_name, destination_path):
|
|
| 75 |
|
| 76 |
async def upload_base64_image_to_storage(base64_image: str, file_name: str) -> str:
|
| 77 |
"""
|
| 78 |
-
Upload a base64 image to Firebase Storage
|
|
|
|
| 79 |
param:
|
| 80 |
-
base64_image: str - The base64 encoded image
|
| 81 |
-
file_name: str - The name of the file to be uploaded
|
|
|
|
| 82 |
return:
|
| 83 |
-
str - The public URL of the uploaded file
|
| 84 |
"""
|
| 85 |
try:
|
| 86 |
# Decode the base64 image
|
|
@@ -88,10 +90,13 @@ async def upload_base64_image_to_storage(base64_image: str, file_name: str) -> s
|
|
| 88 |
|
| 89 |
# Convert the decoded image to a JPG file
|
| 90 |
image = Image.open(io.BytesIO(image_data))
|
|
|
|
|
|
|
| 91 |
temp_file_path = f"{tempfile.gettempdir()}/{file_name}.jpg"
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
|
|
|
| 95 |
|
| 96 |
# Upload the JPG file to Firebase Storage
|
| 97 |
public_url = await upload_file_to_storage(temp_file_path, f"{file_name}.jpg")
|
|
@@ -117,7 +122,12 @@ async def process_images(base64_images: List[str]) -> List[str]:
|
|
| 117 |
base64.urlsafe_b64encode(timestamp.encode()).decode().rstrip("=")
|
| 118 |
)
|
| 119 |
file_name = f"image_{encoded_timestamp}_{idx}"
|
|
|
|
|
|
|
| 120 |
tasks.append(upload_base64_image_to_storage(base64_image, file_name))
|
| 121 |
|
|
|
|
| 122 |
final_output = await asyncio.gather(*tasks)
|
|
|
|
| 123 |
return final_output
|
|
|
|
|
|
| 75 |
|
| 76 |
async def upload_base64_image_to_storage(base64_image: str, file_name: str) -> str:
|
| 77 |
"""
|
| 78 |
+
Upload a base64 image to Firebase Storage.
|
| 79 |
+
|
| 80 |
param:
|
| 81 |
+
base64_image: str - The base64 encoded image.
|
| 82 |
+
file_name: str - The name of the file to be uploaded.
|
| 83 |
+
|
| 84 |
return:
|
| 85 |
+
str - The public URL of the uploaded file.
|
| 86 |
"""
|
| 87 |
try:
|
| 88 |
# Decode the base64 image
|
|
|
|
| 90 |
|
| 91 |
# Convert the decoded image to a JPG file
|
| 92 |
image = Image.open(io.BytesIO(image_data))
|
| 93 |
+
|
| 94 |
+
# Generate a temporary file path
|
| 95 |
temp_file_path = f"{tempfile.gettempdir()}/{file_name}.jpg"
|
| 96 |
|
| 97 |
+
# Since image.save is a blocking operation, run it in a thread pool
|
| 98 |
+
loop = asyncio.get_event_loop()
|
| 99 |
+
await loop.run_in_executor(None, lambda: image.save(temp_file_path, format="JPEG"))
|
| 100 |
|
| 101 |
# Upload the JPG file to Firebase Storage
|
| 102 |
public_url = await upload_file_to_storage(temp_file_path, f"{file_name}.jpg")
|
|
|
|
| 122 |
base64.urlsafe_b64encode(timestamp.encode()).decode().rstrip("=")
|
| 123 |
)
|
| 124 |
file_name = f"image_{encoded_timestamp}_{idx}"
|
| 125 |
+
|
| 126 |
+
# If you're performing image processing, make sure to use a thread pool
|
| 127 |
tasks.append(upload_base64_image_to_storage(base64_image, file_name))
|
| 128 |
|
| 129 |
+
# Gather all async tasks
|
| 130 |
final_output = await asyncio.gather(*tasks)
|
| 131 |
+
print("final_output", final_output)
|
| 132 |
return final_output
|
| 133 |
+
|
src/utils/helper.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
import faiss
|
| 4 |
+
import base64
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from fastapi import FastAPI, HTTPException
|
| 7 |
+
from fastapi.responses import JSONResponse
|
| 8 |
+
from io import BytesIO
|
| 9 |
+
from src.modules import FeatureExtractor
|
| 10 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 11 |
+
import zipfile
|
| 12 |
+
from pydantic import BaseModel, Field
|
| 13 |
+
import json
|
| 14 |
+
from dotenv import load_dotenv
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def base64_to_image(base64_str: str) -> Image.Image:
|
| 20 |
+
try:
|
| 21 |
+
image_data = base64.b64decode(base64_str)
|
| 22 |
+
image = Image.open(BytesIO(image_data)).convert("RGB")
|
| 23 |
+
return image
|
| 24 |
+
except Exception as e:
|
| 25 |
+
raise HTTPException(status_code=400, detail="Invalid Base64 image")
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def image_to_base64(image: Image.Image) -> str:
|
| 29 |
+
buffered = BytesIO()
|
| 30 |
+
image.save(buffered, format="JPEG")
|
| 31 |
+
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|