remove unused function
This commit is contained in:
parent
1eb545840e
commit
9f2dd0dfc3
@ -411,7 +411,6 @@ The API provides the following main endpoints with their authentication and pagi
|
|||||||
- `collection_id` (optional) - Filter by collection
|
- `collection_id` (optional) - Filter by collection
|
||||||
- **Response includes:** `results`, `total`, `limit`, `threshold`, `query`
|
- **Response includes:** `results`, `total`, `limit`, `threshold`, `query`
|
||||||
- `POST /api/v1/search` - Advanced search with same pagination
|
- `POST /api/v1/search` - Advanced search with same pagination
|
||||||
- `GET /api/v1/search/similar/{image_id}` - Find similar images with pagination
|
|
||||||
|
|
||||||
### 🔑 **Authentication Model**
|
### 🔑 **Authentication Model**
|
||||||
|
|
||||||
|
|||||||
@ -237,122 +237,3 @@ async def search_images_advanced(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error in advanced search: {e}")
|
logger.error(f"Error in advanced search: {e}")
|
||||||
raise HTTPException(status_code=500, detail="Advanced search failed")
|
raise HTTPException(status_code=500, detail="Advanced search failed")
|
||||||
|
|
||||||
@router.get("/similar/{image_id}", response_model=SearchResponse)
|
|
||||||
async def find_similar_images(
|
|
||||||
image_id: str,
|
|
||||||
request: Request,
|
|
||||||
limit: int = Query(10, ge=1, le=50, description="Number of similar images to return"),
|
|
||||||
threshold: float = Query(0.65, ge=0.0, le=1.0, description="Similarity threshold"),
|
|
||||||
current_user: UserModel = Depends(get_current_user)
|
|
||||||
):
|
|
||||||
"""
|
|
||||||
Find images similar to a given image
|
|
||||||
"""
|
|
||||||
log_request(
|
|
||||||
{
|
|
||||||
"path": request.url.path,
|
|
||||||
"method": request.method,
|
|
||||||
"image_id": image_id,
|
|
||||||
"limit": limit,
|
|
||||||
"threshold": threshold
|
|
||||||
},
|
|
||||||
user_id=str(current_user.id),
|
|
||||||
team_id=str(current_user.team_id)
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
|
||||||
from bson import ObjectId
|
|
||||||
|
|
||||||
# Get the reference image
|
|
||||||
try:
|
|
||||||
obj_id = ObjectId(image_id)
|
|
||||||
except:
|
|
||||||
raise HTTPException(status_code=400, detail="Invalid image ID")
|
|
||||||
|
|
||||||
reference_image = await image_repository.get_by_id(obj_id)
|
|
||||||
if not reference_image:
|
|
||||||
raise HTTPException(status_code=404, detail="Reference image not found")
|
|
||||||
|
|
||||||
# Check team access
|
|
||||||
if reference_image.team_id != current_user.team_id:
|
|
||||||
raise HTTPException(status_code=403, detail="Not authorized to access this image")
|
|
||||||
|
|
||||||
# Check if the image has embeddings
|
|
||||||
if not reference_image.has_embedding or not reference_image.embedding_id:
|
|
||||||
raise HTTPException(status_code=400, detail="Reference image does not have embeddings")
|
|
||||||
|
|
||||||
# Get the embedding for the reference image
|
|
||||||
reference_data = get_vector_db_service().get_image_vector(image_id)
|
|
||||||
if not reference_data or not reference_data.get('vector'):
|
|
||||||
raise HTTPException(status_code=400, detail="Failed to get reference image embedding")
|
|
||||||
|
|
||||||
reference_embedding = reference_data['vector']
|
|
||||||
|
|
||||||
# Search for similar images
|
|
||||||
search_results = get_vector_db_service().search_similar_images(
|
|
||||||
query_vector=reference_embedding,
|
|
||||||
limit=limit + 1, # +1 to account for the reference image itself
|
|
||||||
score_threshold=threshold,
|
|
||||||
filter_conditions={"team_id": str(current_user.team_id)} if current_user.team_id else None
|
|
||||||
)
|
|
||||||
|
|
||||||
# Remove the reference image from results
|
|
||||||
search_results = [result for result in search_results if result['image_id'] != image_id][:limit]
|
|
||||||
|
|
||||||
if not search_results:
|
|
||||||
return SearchResponse(
|
|
||||||
query=f"Similar to image {image_id}",
|
|
||||||
results=[],
|
|
||||||
total=0,
|
|
||||||
limit=limit,
|
|
||||||
threshold=threshold
|
|
||||||
)
|
|
||||||
|
|
||||||
# Get image IDs and scores from search results
|
|
||||||
image_ids = [result['image_id'] for result in search_results if result['image_id']]
|
|
||||||
scores = {result['image_id']: result['score'] for result in search_results if result['image_id']}
|
|
||||||
|
|
||||||
# Get image metadata from database
|
|
||||||
images = await image_repository.get_by_ids(image_ids)
|
|
||||||
|
|
||||||
# Convert to response format with similarity scores
|
|
||||||
results = []
|
|
||||||
for image in images:
|
|
||||||
image_id_str = str(image.id)
|
|
||||||
similarity_score = scores.get(image_id_str, 0.0)
|
|
||||||
|
|
||||||
result = ImageResponse(
|
|
||||||
id=image_id_str,
|
|
||||||
filename=image.filename,
|
|
||||||
original_filename=image.original_filename,
|
|
||||||
file_size=image.file_size,
|
|
||||||
content_type=image.content_type,
|
|
||||||
storage_path=image.storage_path,
|
|
||||||
team_id=str(image.team_id),
|
|
||||||
uploader_id=str(image.uploader_id),
|
|
||||||
upload_date=image.upload_date,
|
|
||||||
description=image.description,
|
|
||||||
metadata=image.metadata,
|
|
||||||
has_embedding=image.has_embedding,
|
|
||||||
collection_id=str(image.collection_id) if image.collection_id else None,
|
|
||||||
similarity_score=similarity_score
|
|
||||||
)
|
|
||||||
results.append(result)
|
|
||||||
|
|
||||||
# Sort by similarity score (highest first)
|
|
||||||
results.sort(key=lambda x: x.similarity_score or 0, reverse=True)
|
|
||||||
|
|
||||||
return SearchResponse(
|
|
||||||
query=f"Similar to image {image_id}",
|
|
||||||
results=results,
|
|
||||||
total=len(results),
|
|
||||||
limit=limit,
|
|
||||||
threshold=threshold
|
|
||||||
)
|
|
||||||
|
|
||||||
except HTTPException:
|
|
||||||
raise
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Error finding similar images: {e}")
|
|
||||||
raise HTTPException(status_code=500, detail="Similar image search failed")
|
|
||||||
Loading…
x
Reference in New Issue
Block a user