Spaces:
Running
Running
File size: 5,717 Bytes
09ecaf7 1686de5 d25db6b 1686de5 65933cd 09ecaf7 1686de5 09ecaf7 1686de5 d25db6b 1686de5 d25db6b 09ecaf7 d25db6b 1686de5 09ecaf7 d25db6b 09ecaf7 d25db6b 09ecaf7 d25db6b 65933cd 1686de5 09ecaf7 d25db6b 65933cd 1686de5 d7291ef 394963a d7291ef 65933cd 394963a 09ecaf7 394963a 65933cd 09ecaf7 65933cd 09ecaf7 1686de5 65933cd 1686de5 65933cd 1686de5 65933cd 1686de5 65933cd 1686de5 65933cd 09ecaf7 d25db6b 1686de5 65933cd d25db6b 09ecaf7 65933cd d25db6b 65933cd 1686de5 65933cd 1686de5 d25db6b 1686de5 d25db6b 394963a d7291ef |
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
import io, hashlib
from typing import Optional
from sqlalchemy.orm import Session, joinedload
from . import models, schemas
from fastapi import HTTPException
def hash_bytes(data: bytes) -> str:
"""Compute SHA-256 hex digest of the data."""
return hashlib.sha256(data).hexdigest()
def create_image(db: Session, src, type_code, key, sha, countries: list[str], epsg: Optional[str], image_type: str):
"""Insert into images and image_countries."""
img = models.Images(
source=src, event_type=type_code,
file_key=key, sha256=sha, epsg=epsg, image_type=image_type
)
db.add(img)
db.flush()
for c in countries:
country = db.get(models.Country, c)
if country:
img.countries.append(country)
db.commit()
db.refresh(img)
return img
def get_images(db: Session):
"""Get all images with their countries"""
return (
db.query(models.Images)
.options(
joinedload(models.Images.countries),
)
.all()
)
def get_image(db: Session, image_id: str):
"""Get a single image by ID with its countries"""
return (
db.query(models.Images)
.options(
joinedload(models.Images.countries),
)
.filter(models.Images.image_id == image_id)
.first()
)
def create_caption(db: Session, image_id, title, prompt, model_code, raw_json, text, metadata=None):
print(f"Creating caption for image_id: {image_id}")
print(f"Caption data: title={title}, prompt={prompt}, model={model_code}")
print(f"Database session ID: {id(db)}")
print(f"Database session is active: {db.is_active}")
if metadata:
raw_json["extracted_metadata"] = metadata
img = db.get(models.Images, image_id)
if not img:
raise HTTPException(404, "Image not found")
img.title = title
img.prompt = prompt
img.model = model_code
img.schema_id = "default_caption@1.0.0"
img.raw_json = raw_json
img.generated = text
img.edited = text
print(f"About to commit caption to database...")
db.commit()
print(f"Caption commit successful!")
db.refresh(img)
print(f"Caption created successfully for image: {img.image_id}")
return img
def get_caption(db: Session, image_id: str):
"""Get caption data for a specific image"""
return db.get(models.Images, image_id)
def get_captions_by_image(db: Session, image_id: str):
"""Get caption data for a specific image (now just returns the image)"""
img = db.get(models.Images, image_id)
if img and img.title:
return [img]
return []
def get_all_captions_with_images(db: Session):
"""Get all images that have caption data"""
print(f"DEBUG: Querying database for images with caption data...")
total_images = db.query(models.Images).count()
print(f"DEBUG: Total images in database: {total_images}")
images_with_title = db.query(models.Images).filter(
models.Images.title.isnot(None)
).count()
print(f"DEBUG: Images with title field: {images_with_title}")
images_with_generated = db.query(models.Images).filter(
models.Images.generated.isnot(None)
).count()
print(f"DEBUG: Images with generated field: {images_with_generated}")
images_with_model = db.query(models.Images).filter(
models.Images.model.isnot(None)
).count()
print(f"DEBUG: Images with model field: {images_with_model}")
results = db.query(models.Images).filter(
models.Images.title.isnot(None)
).all()
print(f"DEBUG: Query returned {len(results)} results")
for img in results:
print(f"DEBUG: Image {img.image_id}: title='{img.title}', generated='{img.generated}', model='{img.model}'")
return results
def update_caption(db: Session, image_id: str, update: schemas.CaptionUpdate):
"""Update caption data for an image"""
img = db.get(models.Images, image_id)
if not img:
return None
for field, value in update.dict(exclude_unset=True).items():
setattr(img, field, value)
db.commit()
db.refresh(img)
return img
def delete_caption(db: Session, image_id: str):
"""Delete caption data for an image (sets caption fields to None)"""
img = db.get(models.Images, image_id)
if not img:
return False
img.title = None
img.prompt = None
img.model = None
img.schema_id = None
img.raw_json = None
img.generated = None
img.edited = None
img.accuracy = None
img.context = None
img.usability = None
img.starred = False
db.commit()
return True
def get_sources(db: Session):
"""Get all sources for lookup"""
return db.query(models.Source).all()
def get_regions(db: Session):
"""Get all regions for lookup"""
return db.query(models.Region).all()
def get_types(db: Session):
"""Get all types for lookup"""
return db.query(models.EventType).all()
def get_spatial_references(db: Session):
"""Get all spatial references for lookup"""
return db.query(models.SpatialReference).all()
def get_image_types(db: Session):
"""Get all image types for lookup"""
return db.query(models.ImageTypes).all()
def get_countries(db: Session):
"""Get all countries for lookup"""
return db.query(models.Country).all()
def get_country(db: Session, c_code: str):
"""Get a single country by code"""
return db.get(models.Country, c_code)
def get_models(db: Session):
"""Get all models"""
return db.query(models.Models).all()
def get_model(db: Session, m_code: str):
"""Get a specific model by code"""
return db.get(models.Models, m_code)
|