Spaces:
Running
Running
Prathamesh Sable
commited on
Commit
·
fe15b8d
1
Parent(s):
3c88e9f
setup fast api
Browse files
main.py
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from routes.extract_product_info_from_barcode import router as extract_product_info_router
|
| 3 |
+
from routes.fetch_product_data import router as fetch_product_data_router
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
app.include_router(extract_product_info_router, prefix="/api")
|
| 8 |
+
app.include_router(fetch_product_data_router, prefix="/api")
|
| 9 |
+
|
| 10 |
+
# To run the FastAPI app, use the command: uvicorn main:app --reload
|
models.py
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel
|
| 2 |
+
|
| 3 |
+
class ProductInfo(BaseModel):
|
| 4 |
+
product_name: str
|
| 5 |
+
generic_name: str
|
| 6 |
+
brands: str
|
| 7 |
+
ingredients: list
|
| 8 |
+
ingredients_text: str
|
| 9 |
+
ingredients_analysis: dict
|
| 10 |
+
nutriscore: dict
|
| 11 |
+
nutrient_levels: dict
|
| 12 |
+
nutriments: dict
|
| 13 |
+
data_quality_warnings: list
|
| 14 |
+
|
| 15 |
+
class Barcodes(BaseModel):
|
| 16 |
+
barcodes: dict
|
| 17 |
+
|
| 18 |
+
class ProductData(BaseModel):
|
| 19 |
+
product: dict
|
utils.py
CHANGED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
from fastapi import HTTPException
|
| 5 |
+
|
| 6 |
+
def fetch_product_data_from_api(barcode):
|
| 7 |
+
url = f"https://india.openfoodfacts.org/api/v2/product/{barcode}.json"
|
| 8 |
+
response = requests.get(url)
|
| 9 |
+
if response.status_code != 200:
|
| 10 |
+
raise HTTPException(status_code=response.status_code, detail=f"Failed to fetch data for barcode {barcode}")
|
| 11 |
+
return response.json()
|
| 12 |
+
|
| 13 |
+
def save_json_file(item, data):
|
| 14 |
+
os.makedirs("v2", exist_ok=True)
|
| 15 |
+
with open(f"v2/{item}.json", "w") as file:
|
| 16 |
+
json.dump(data, file)
|
| 17 |
+
print(f"Saved {item}.json")
|