vinm-base64 / fastapi_app.py
ChandraP12330's picture
Update fastapi_app.py
4ca3a60 verified
#fastapi_app.py
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from llm_backend import process_shelf_image
import uvicorn
import os
app = FastAPI(title="Retail Shelf Analyzer API")
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
class ImageRequest(BaseModel):
image_base64: str
@app.get("/", summary="Health Check", tags=["System"])
def read_root():
"""
Checks if the API is running and reachable.
Returns:
dict: A simple message confirming the API status.
"""
return {"message": "Retail Shelf Analyzer API is running"}
@app.post("/analyze_shelf", summary="Analyze Retail Shelf Image", tags=["Shelf Analysis"])
def analyze_shelf(request: ImageRequest):
"""
Analyzes a retail shelf image to extract product information.
This endpoint accepts an image as a Base64 string, processes it using a Generative AI model,
and returns a structured Markdown table containing:
- **ID**: Unique identifier for each item.
- **Product_SKU**: Identified product name or type.
- **Shelf_ID**: Shelf location identifier.
- **Last_Updated**: Timestamp of the analysis.
If the image is unclear, it returns an error message requesting a re-upload.
"""
try:
# Validate Input
if not request.image_base64:
raise HTTPException(status_code=400, detail="Image Base64 data is required")
markdown_output = process_shelf_image(request.image_base64)
return {"markdown_output": markdown_output}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
port = int(os.getenv("PORT", 7860))
uvicorn.run("fastapi_app:app", host="0.0.0.0", port=port, reload=True)