Spaces:
Running
Running
Prathamesh Sable
commited on
Commit
·
1b050e5
1
Parent(s):
d10bf47
Add new utility modules and update imports in `services/ai_agent.py`
Browse files* **New utility modules**
- Add `fetch_product_data_from_api` function to `utils/fetch_data.py`
- Add `save_json_file` function to `utils/file_operations.py`
- Add `extract_text_from_image` and `detect_barcode_from_image` functions to `utils/image_processing.py`
* **Update imports**
- Update import statement for `fetch_product_data_from_api` to `utils/fetch_data.py`
- Update import statement for `save_json_file` to `utils/file_operations.py`
* **Remove old utility module**
- Delete `utils.py`
- services/ai_agent.py +2 -1
- utils/fetch_data.py +9 -0
- utils/file_operations.py +8 -0
- utils.py → utils/image_processing.py +0 -17
services/ai_agent.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
from sqlalchemy.orm import Session
|
| 2 |
from fastapi import HTTPException
|
| 3 |
-
from utils import fetch_product_data_from_api
|
|
|
|
| 4 |
from models.ingredient import Ingredient
|
| 5 |
from models.product import Product
|
| 6 |
from services.ingredients import get_ingredient_by_name, save_ingredient_data, fetch_ingredient_data_from_api
|
|
|
|
| 1 |
from sqlalchemy.orm import Session
|
| 2 |
from fastapi import HTTPException
|
| 3 |
+
from utils.fetch_data import fetch_product_data_from_api
|
| 4 |
+
from utils.file_operations import save_json_file
|
| 5 |
from models.ingredient import Ingredient
|
| 6 |
from models.product import Product
|
| 7 |
from services.ingredients import get_ingredient_by_name, save_ingredient_data, fetch_ingredient_data_from_api
|
utils/fetch_data.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from fastapi import HTTPException
|
| 3 |
+
|
| 4 |
+
def fetch_product_data_from_api(barcode):
|
| 5 |
+
url = f"https://india.openfoodfacts.org/api/v2/product/{barcode}.json"
|
| 6 |
+
response = requests.get(url)
|
| 7 |
+
if response.status_code != 200:
|
| 8 |
+
raise HTTPException(status_code=response.status_code, detail=f"Failed to fetch data for barcode {barcode}")
|
| 9 |
+
return response.json()
|
utils/file_operations.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
def save_json_file(item, data):
|
| 5 |
+
os.makedirs("v2", exist_ok=True)
|
| 6 |
+
with open(f"v2/{item}.json", "w") as file:
|
| 7 |
+
json.dump(data, file)
|
| 8 |
+
print(f"Saved {item}.json")
|
utils.py → utils/image_processing.py
RENAMED
|
@@ -1,23 +1,6 @@
|
|
| 1 |
-
import json
|
| 2 |
-
import os
|
| 3 |
-
import requests
|
| 4 |
-
from fastapi import HTTPException
|
| 5 |
import cv2
|
| 6 |
import pytesseract
|
| 7 |
|
| 8 |
-
def fetch_product_data_from_api(barcode):
|
| 9 |
-
url = f"https://india.openfoodfacts.org/api/v2/product/{barcode}.json"
|
| 10 |
-
response = requests.get(url)
|
| 11 |
-
if response.status_code != 200:
|
| 12 |
-
raise HTTPException(status_code=response.status_code, detail=f"Failed to fetch data for barcode {barcode}")
|
| 13 |
-
return response.json()
|
| 14 |
-
|
| 15 |
-
def save_json_file(item, data):
|
| 16 |
-
os.makedirs("v2", exist_ok=True)
|
| 17 |
-
with open(f"v2/{item}.json", "w") as file:
|
| 18 |
-
json.dump(data, file)
|
| 19 |
-
print(f"Saved {item}.json")
|
| 20 |
-
|
| 21 |
def extract_text_from_image(image_path):
|
| 22 |
image = cv2.imread(image_path)
|
| 23 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import pytesseract
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
def extract_text_from_image(image_path):
|
| 5 |
image = cv2.imread(image_path)
|
| 6 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|