|
|
from fastapi import FastAPI |
|
|
from pydantic import BaseModel |
|
|
from typing import Union, List |
|
|
import uvicorn |
|
|
import logging |
|
|
from datetime import datetime |
|
|
import pytz |
|
|
import torch |
|
|
|
|
|
from main import ImageProcessor |
|
|
image_processor = ImageProcessor() |
|
|
|
|
|
logging.basicConfig(filename="drinksLog.log", filemode='w') |
|
|
logger = logging.getLogger("drinks") |
|
|
logger.setLevel(logging.DEBUG) |
|
|
file_handler = logging.FileHandler("drinksLog.log") |
|
|
logger.addHandler(file_handler) |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
class RequestBody(BaseModel): |
|
|
cat: str |
|
|
img: str |
|
|
|
|
|
class RequestData(BaseModel): |
|
|
body: Union[RequestBody, List[RequestBody]] |
|
|
|
|
|
@app.get("/status") |
|
|
async def status(): |
|
|
return {"status": "AI Server is running"} |
|
|
|
|
|
|
|
|
async def process_image(item: RequestBody): |
|
|
category = item.cat |
|
|
img_url = item.img |
|
|
|
|
|
if category == "posm": |
|
|
result = await image_processor.process_image(img_url) |
|
|
return result |
|
|
elif category == "planogram": |
|
|
result = await image_processor.process_image(img_url) |
|
|
return result |
|
|
else: |
|
|
return {"error": f"Unsupported category {category}"} |
|
|
|
|
|
|
|
|
@app.post("/bats") |
|
|
async def detect_items(request_data: RequestData): |
|
|
try: |
|
|
|
|
|
results = [] |
|
|
|
|
|
|
|
|
if isinstance(request_data.body, list): |
|
|
|
|
|
for item in request_data.body: |
|
|
|
|
|
processed_result = await process_image(item) |
|
|
results.append(processed_result) |
|
|
else: |
|
|
|
|
|
processed_result = await process_image(request_data.body) |
|
|
results.append(processed_result) |
|
|
|
|
|
|
|
|
return results |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"Error during detection: {str(e)}") |
|
|
return {"error": "An error occurred during detection"} |
|
|
|
|
|
if __name__ == "__main__": |
|
|
try: |
|
|
uvicorn.run(app, host="127.0.0.1", port=4444) |
|
|
finally: |
|
|
torch.cuda.empty_cache() |
|
|
|