File size: 2,000 Bytes
2946a21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ca3a60
2946a21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ca3a60
2946a21
 
 
 
 
 
 
 
 
4ca3a60
 
 
2946a21
4ca3a60
2946a21
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#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)