Spaces:
Running
Running
Prathamesh Sable
commited on
Commit
·
7e333d7
1
Parent(s):
da8dc09
router update
Browse files- routers/analysis.py +58 -25
- routers/auth.py +42 -30
- routers/history.py +23 -9
routers/analysis.py
CHANGED
|
@@ -1,36 +1,69 @@
|
|
| 1 |
from fastapi import APIRouter, Depends, HTTPException
|
| 2 |
from sqlalchemy.orm import Session
|
| 3 |
from typing import List, Dict, Any
|
| 4 |
-
from
|
| 5 |
-
from models.user import User
|
| 6 |
-
from models.ingredient import Ingredient
|
| 7 |
-
from models.product import Product
|
| 8 |
-
from services.analysis_agent import analyze_ingredients, provide_personalized_recommendations
|
| 9 |
from services.auth_service import get_current_user
|
| 10 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
router = APIRouter()
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
@router.get("/personalized_recommendations")
|
| 23 |
-
def personalized_recommendations_endpoint(db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
| 24 |
-
try:
|
| 25 |
-
recommendations = provide_personalized_recommendations(db, current_user.id)
|
| 26 |
-
return recommendations
|
| 27 |
-
except Exception as e:
|
| 28 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
try:
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import APIRouter, Depends, HTTPException
|
| 2 |
from sqlalchemy.orm import Session
|
| 3 |
from typing import List, Dict, Any
|
| 4 |
+
from interfaces.ingredientModels import IngredientAnalysisResult, IngredientRequest
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from services.auth_service import get_current_user
|
| 6 |
+
from logger_manager import log_info, log_error,logger
|
| 7 |
+
from db.database import get_db
|
| 8 |
+
from db.repositories import IngredientRepository
|
| 9 |
+
|
| 10 |
+
from services.ingredientFinderAgent import IngredientInfoAgentLangGraph
|
| 11 |
+
|
| 12 |
|
| 13 |
router = APIRouter()
|
| 14 |
|
| 15 |
+
def ingredient_db_to_pydantic(db_ingredient):
|
| 16 |
+
"""Convert a database ingredient model to a Pydantic model."""
|
| 17 |
+
return IngredientAnalysisResult(
|
| 18 |
+
name=db_ingredient.name,
|
| 19 |
+
alternate_names=db_ingredient.alternate_names or [],
|
| 20 |
+
is_found=True,
|
| 21 |
+
safety_rating=db_ingredient.safety_rating or 5,
|
| 22 |
+
description=db_ingredient.description or "No description available",
|
| 23 |
+
health_effects=db_ingredient.health_effects or ["Unknown"],
|
| 24 |
+
details_with_source=[source.data for source in db_ingredient.sources]
|
| 25 |
+
)
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# process single ingredient
|
| 29 |
+
@router.post("/process_ingredient", response_model=IngredientAnalysisResult)
|
| 30 |
+
async def process_ingredient_endpoint(request: IngredientRequest, db: Session = Depends(get_db)):
|
| 31 |
try:
|
| 32 |
+
logger.info(f"Received request to process ingredient: {request.name}")
|
| 33 |
+
|
| 34 |
+
# Check if we already have this ingredient in the database
|
| 35 |
+
repo = IngredientRepository(db)
|
| 36 |
+
db_ingredient = repo.get_ingredient_by_name(request.name)
|
| 37 |
+
|
| 38 |
+
if db_ingredient:
|
| 39 |
+
logger.info(f"Found existing ingredient in database: {request.name}")
|
| 40 |
+
# Convert DB model to Pydantic model
|
| 41 |
+
# (This would need a function to correctly map the data)
|
| 42 |
+
return ingredient_db_to_pydantic(db_ingredient)
|
| 43 |
+
|
| 44 |
+
# If not in database, get from agent
|
| 45 |
+
ingredient_finder = IngredientInfoAgentLangGraph()
|
| 46 |
+
result = ingredient_finder.process_ingredient(request.name)
|
| 47 |
+
|
| 48 |
+
# Save to database
|
| 49 |
+
repo.create_ingredient(result)
|
| 50 |
+
logger.info(f"Saved new ingredient to database: {request.name}")
|
| 51 |
+
|
| 52 |
+
return result
|
| 53 |
except Exception as e:
|
| 54 |
+
logger.error(f"Error processing ingredient: {e}")
|
| 55 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
# @router.post("/process_ingredients")
|
| 59 |
+
# def process_ingredients_endpoint(ingredients: List[str], db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
| 60 |
+
# log_info("process_ingredients_endpoint called")
|
| 61 |
+
# print(ingredients)
|
| 62 |
+
# try:
|
| 63 |
+
# # result = process_ingredients(db, ingredients, current_user.id)
|
| 64 |
+
# result = None
|
| 65 |
+
# log_info("process_ingredients_endpoint completed successfully")
|
| 66 |
+
# return result
|
| 67 |
+
# except Exception as e:
|
| 68 |
+
# log_error(f"Error in process_ingredients_endpoint: {str(e)}")
|
| 69 |
+
# raise HTTPException(status_code=500, detail=str(e))
|
routers/auth.py
CHANGED
|
@@ -1,46 +1,58 @@
|
|
| 1 |
from fastapi import APIRouter, Depends, HTTPException, status
|
| 2 |
from sqlalchemy.orm import Session
|
| 3 |
from fastapi.security import OAuth2PasswordRequestForm
|
| 4 |
-
from
|
| 5 |
-
from database import get_db
|
| 6 |
from services.auth_service import authenticate_user, create_access_token, create_user, get_current_active_user
|
| 7 |
from datetime import timedelta
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
router = APIRouter()
|
| 10 |
|
| 11 |
-
class UserCreate(BaseModel):
|
| 12 |
-
username: str
|
| 13 |
-
email: str
|
| 14 |
-
password: str
|
| 15 |
-
|
| 16 |
-
class Token(BaseModel):
|
| 17 |
-
access_token: str
|
| 18 |
-
token_type: str
|
| 19 |
|
| 20 |
@router.post("/register", response_model=Token)
|
| 21 |
def register(user: UserCreate, db: Session = Depends(get_db)):
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
@router.post("/login", response_model=Token)
|
| 30 |
def login(form_data: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(get_db)):
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
)
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
@router.get("/
|
| 45 |
-
def read_users_me(current_user:
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import APIRouter, Depends, HTTPException, status
|
| 2 |
from sqlalchemy.orm import Session
|
| 3 |
from fastapi.security import OAuth2PasswordRequestForm
|
| 4 |
+
from db.database import get_db
|
|
|
|
| 5 |
from services.auth_service import authenticate_user, create_access_token, create_user, get_current_active_user
|
| 6 |
from datetime import timedelta
|
| 7 |
+
from db.models import User
|
| 8 |
+
from logger_manager import log_info, log_error
|
| 9 |
+
from interfaces.authModels import UserCreate,UserResponse,Token
|
| 10 |
|
| 11 |
router = APIRouter()
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
@router.post("/register", response_model=Token)
|
| 15 |
def register(user: UserCreate, db: Session = Depends(get_db)):
|
| 16 |
+
log_info("Register endpoint called")
|
| 17 |
+
try:
|
| 18 |
+
db_user = create_user(db, user.username, user.email, user.password)
|
| 19 |
+
access_token_expires = timedelta(minutes=30)
|
| 20 |
+
access_token = create_access_token(
|
| 21 |
+
data={"sub": db_user.username}, expires_delta=access_token_expires
|
| 22 |
+
)
|
| 23 |
+
log_info("User registered successfully")
|
| 24 |
+
return {"access_token": access_token, "token_type": "bearer"}
|
| 25 |
+
except Exception as e:
|
| 26 |
+
log_error(f"Error in register endpoint: {str(e)}")
|
| 27 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
| 28 |
|
| 29 |
@router.post("/login", response_model=Token)
|
| 30 |
def login(form_data: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(get_db)):
|
| 31 |
+
log_info("Login endpoint called")
|
| 32 |
+
try:
|
| 33 |
+
user = authenticate_user(db, form_data.username, form_data.password)
|
| 34 |
+
if not user:
|
| 35 |
+
log_error("Incorrect username or password")
|
| 36 |
+
raise HTTPException(
|
| 37 |
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 38 |
+
detail="Incorrect username or password",
|
| 39 |
+
headers={"WWW-Authenticate": "Bearer"},
|
| 40 |
+
)
|
| 41 |
+
access_token_expires = timedelta(weeks=4)
|
| 42 |
+
access_token = create_access_token(
|
| 43 |
+
data={"sub": user.username}, expires_delta=access_token_expires
|
| 44 |
)
|
| 45 |
+
log_info("User logged in successfully")
|
| 46 |
+
return {"access_token": access_token, "token_type": "bearer"}
|
| 47 |
+
except Exception as e:
|
| 48 |
+
log_error(f"Error in login endpoint: {str(e)}")
|
| 49 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
| 50 |
|
| 51 |
+
@router.get("/user", response_model=UserResponse)
|
| 52 |
+
def read_users_me(current_user: User = Depends(get_current_active_user)):
|
| 53 |
+
log_info("Read users/me endpoint called")
|
| 54 |
+
try:
|
| 55 |
+
return current_user
|
| 56 |
+
except Exception as e:
|
| 57 |
+
log_error(f"Error in read_users_me endpoint: {str(e)}")
|
| 58 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
routers/history.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
from fastapi import APIRouter, Depends, HTTPException, status
|
| 2 |
from sqlalchemy.orm import Session
|
| 3 |
from pydantic import BaseModel
|
| 4 |
-
from database import get_db
|
| 5 |
from services.scan_history import record_scan, get_scan_history
|
| 6 |
-
from models
|
|
|
|
| 7 |
|
| 8 |
router = APIRouter()
|
| 9 |
|
|
@@ -19,12 +20,25 @@ class ScanHistoryResponse(BaseModel):
|
|
| 19 |
|
| 20 |
@router.post("/scan", response_model=ScanHistoryResponse)
|
| 21 |
def create_scan(scan: ScanHistoryCreate, db: Session = Depends(get_db)):
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
@router.get("/
|
| 26 |
def read_scan_history(user_id: int, db: Session = Depends(get_db)):
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import APIRouter, Depends, HTTPException, status
|
| 2 |
from sqlalchemy.orm import Session
|
| 3 |
from pydantic import BaseModel
|
| 4 |
+
from db.database import get_db
|
| 5 |
from services.scan_history import record_scan, get_scan_history
|
| 6 |
+
from db.models import ScanHistory
|
| 7 |
+
from logger_manager import log_info, log_error
|
| 8 |
|
| 9 |
router = APIRouter()
|
| 10 |
|
|
|
|
| 20 |
|
| 21 |
@router.post("/scan", response_model=ScanHistoryResponse)
|
| 22 |
def create_scan(scan: ScanHistoryCreate, db: Session = Depends(get_db)):
|
| 23 |
+
log_info("Create scan endpoint called")
|
| 24 |
+
try:
|
| 25 |
+
scan_entry = record_scan(db, scan.user_id, scan.product_id)
|
| 26 |
+
log_info("Scan recorded successfully")
|
| 27 |
+
return scan_entry
|
| 28 |
+
except Exception as e:
|
| 29 |
+
log_error(f"Error in create_scan endpoint: {str(e)}")
|
| 30 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
| 31 |
|
| 32 |
+
@router.get("/scan/{user_id}", response_model=list[ScanHistoryResponse])
|
| 33 |
def read_scan_history(user_id: int, db: Session = Depends(get_db)):
|
| 34 |
+
log_info("Read scan history endpoint called")
|
| 35 |
+
try:
|
| 36 |
+
scan_history = get_scan_history(db, user_id)
|
| 37 |
+
if not scan_history:
|
| 38 |
+
log_error("Scan history not found")
|
| 39 |
+
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Scan history not found")
|
| 40 |
+
log_info("Scan history retrieved successfully")
|
| 41 |
+
return scan_history
|
| 42 |
+
except Exception as e:
|
| 43 |
+
log_error(f"Error in read_scan_history endpoint: {str(e)}")
|
| 44 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|