Add helper functions for FoodImage
This commit is contained in:
parent
6838aa993a
commit
5a6488ccc6
104
helpers/foodimage_helpers.py
Normal file
104
helpers/foodimage_helpers.py
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
from typing import Optional, Dict, List, Union
|
||||||
|
from enum import Enum
|
||||||
|
from datetime import datetime
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
import re
|
||||||
|
import requests
|
||||||
|
from PIL import Image
|
||||||
|
from io import BytesIO
|
||||||
|
|
||||||
|
class ImageStatus(Enum):
|
||||||
|
PENDING = "pending"
|
||||||
|
GENERATING = "generating"
|
||||||
|
COMPLETED = "completed"
|
||||||
|
FAILED = "failed"
|
||||||
|
|
||||||
|
def validate_image_url(image_url: str) -> bool:
|
||||||
|
"""
|
||||||
|
Validates if the provided URL is a valid image URL.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
image_url: URL string to validate
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if valid image URL, False otherwise
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
response = requests.head(image_url)
|
||||||
|
return response.headers.get('content-type', '').startswith('image/')
|
||||||
|
except:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def validate_prompt(prompt: str) -> bool:
|
||||||
|
"""
|
||||||
|
Validates if the provided prompt meets requirements.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
prompt: The prompt string to validate
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if valid prompt, False otherwise
|
||||||
|
"""
|
||||||
|
# Prompt should be between 3 and 500 chars and not just whitespace
|
||||||
|
if not prompt or len(prompt.strip()) < 3 or len(prompt) > 500:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def get_food_image_by_status(
|
||||||
|
db: Session,
|
||||||
|
status: ImageStatus
|
||||||
|
) -> List['FoodImage']:
|
||||||
|
"""
|
||||||
|
Retrieves food images by status.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db: Database session
|
||||||
|
status: Status to filter by
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List of FoodImage objects matching the status
|
||||||
|
"""
|
||||||
|
return db.query(FoodImage).filter(FoodImage.status == status.value).all()
|
||||||
|
|
||||||
|
def update_image_status(
|
||||||
|
db: Session,
|
||||||
|
food_image_id: int,
|
||||||
|
new_status: ImageStatus
|
||||||
|
) -> Optional['FoodImage']:
|
||||||
|
"""
|
||||||
|
Updates the status of a food image.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db: Database session
|
||||||
|
food_image_id: ID of image to update
|
||||||
|
new_status: New status to set
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Updated FoodImage object if found, None otherwise
|
||||||
|
"""
|
||||||
|
food_image = db.query(FoodImage).filter(FoodImage.id == food_image_id).first()
|
||||||
|
if food_image:
|
||||||
|
food_image.status = new_status.value
|
||||||
|
db.commit()
|
||||||
|
db.refresh(food_image)
|
||||||
|
return food_image
|
||||||
|
|
||||||
|
def verify_image_dimensions(image_url: str, min_width: int = 512, min_height: int = 512) -> bool:
|
||||||
|
"""
|
||||||
|
Verifies if image meets minimum dimension requirements.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
image_url: URL of image to verify
|
||||||
|
min_width: Minimum required width
|
||||||
|
min_height: Minimum required height
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if image meets requirements, False otherwise
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
response = requests.get(image_url)
|
||||||
|
img = Image.open(BytesIO(response.content))
|
||||||
|
width, height = img.size
|
||||||
|
return width >= min_width and height >= min_height
|
||||||
|
except:
|
||||||
|
return False
|
Loading…
x
Reference in New Issue
Block a user