ChandraP12330/vinm-base64
0
1#fastapi_app.py2from fastapi import FastAPI, HTTPException3from fastapi.middleware.cors import CORSMiddleware4from pydantic import BaseModel5from llm_backend import process_shelf_image6import uvicorn7import os8 9app = FastAPI(title="Retail Shelf Analyzer API")10 11# Add CORS middleware12app.add_middleware(13 CORSMiddleware,14 allow_origins=["*"], # Allows all origins15 allow_credentials=True,16 allow_methods=["*"], # Allows all methods17 allow_headers=["*"], # Allows all headers18)19 20class ImageRequest(BaseModel):21 image_base64: str22 23@app.get("/", summary="Health Check", tags=["System"])24def read_root():25 """26 Checks if the API is running and reachable.27 28 Returns:29 dict: A simple message confirming the API status.30 """31 return {"message": "Retail Shelf Analyzer API is running"}32 33@app.post("/analyze_shelf", summary="Analyze Retail Shelf Image", tags=["Shelf Analysis"])34def analyze_shelf(request: ImageRequest):35 """36 Analyzes a retail shelf image to extract product information.37 38 This endpoint accepts an image as a Base64 string, processes it using a Generative AI model,39 and returns a structured Markdown table containing:40 - **ID**: Unique identifier for each item.41 - **Product_SKU**: Identified product name or type.42 - **Shelf_ID**: Shelf location identifier.43 - **Last_Updated**: Timestamp of the analysis.44 45 If the image is unclear, it returns an error message requesting a re-upload.46 """47 try:48 # Validate Input49 if not request.image_base64:50 raise HTTPException(status_code=400, detail="Image Base64 data is required")51 52 markdown_output = process_shelf_image(request.image_base64)53 return {"markdown_output": markdown_output}54 except Exception as e:55 raise HTTPException(status_code=500, detail=str(e))56 57if __name__ == "__main__":58 port = int(os.getenv("PORT", 7860))59 uvicorn.run("fastapi_app:app", host="0.0.0.0", port=port, reload=True)60 