Spaces:
Running
Running
File size: 6,027 Bytes
073edba 7f8bfb2 073edba 76d149a 7f8bfb2 073edba 7f8bfb2 073edba 7f8bfb2 073edba 7f8bfb2 073edba 7f8bfb2 073edba 7f8bfb2 76d149a |
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 191 192 193 194 195 196 197 198 199 200 201 202 203 |
import time
from loguru import logger
from fastapi import FastAPI, HTTPException
from contextlib import asynccontextmanager
from models import RerankRequest, RerankResponse, RerankResult
from core import ModelManager
model_manager = None
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan manager with model preloading."""
global model_manager
# Startup
logger.info("Starting reranking API...")
try:
model_manager = ModelManager("config.yaml")
await model_manager.preload_all_models()
logger.success("Reranking API startup complete!")
except Exception as e:
logger.error(f"Failed to initialize models: {e}")
raise
yield
# Shutdown
logger.info("Shutting down reranking API...")
app = FastAPI(
title="Multi-Model Reranking API",
description="""
High-performance API for document reranking using multiple state-of-the-art models.
β
**Supported Models:**
- **Jina Reranker V2**: Multilingual reranker optimized for search
- **BGE Reranker V2**: High-performance multilingual reranking
- **Qwen3 Reranker**: Instruction-based reranking with reasoning
π **Features:**
- Multiple reranking models preloaded at startup
- Batch document reranking with relevance scoring
- Fast prototyping app
- Optional instruction-based reranking (Qwen3)
- Comprehensive performance metrics
- Zero cold start delay
π **Input/Output:**
- Input: Query + documents + optional instruction
- Output: Ranked documents with relevance scores
**Warning**: Not use production!.
""",
version="1.0.0",
lifespan=lifespan
)
# -------------------------
# API Endpoints
# -------------------------
@app.post("/rerank", response_model=RerankResponse, tags=["Reranking"])
async def rerank_documents(request: RerankRequest):
"""
Rerank documents based on relevance to query.
This endpoint takes a query and list of documents, then returns them
ranked by relevance using the specified reranking model.
Args:
request: RerankRequest containing query, documents, and model info
Returns:
RerankResponse with ranked documents, scores, and metadata
Example:
```json
{
"query": "machine learning algorithms",
"documents": [
"Deep learning uses neural networks",
"Weather forecast for tomorrow",
"Supervised learning with labeled data"
],
"model_id": "jina-reranker-v2"
}
```
"""
if not request.query.strip():
raise HTTPException(400, "Query cannot be empty")
if not request.documents:
raise HTTPException(400, "Documents list cannot be empty")
valid_docs = [(i, doc.strip()) for i, doc in enumerate(request.documents) if doc.strip()]
if not valid_docs:
raise HTTPException(400, "No valid documents found after filtering empty strings")
try:
start_time = time.time()
model = model_manager.get_model(request.model_id)
original_indices, documents = zip(*valid_docs)
logger.info(f"Query: {request.query.strip()}")
logger.info(f"Document: {list(documents)}")
scores = model.rerank(
query=request.query.strip(),
documents=list(documents),
instruction=request.instruction
)
results = []
for i, (orig_idx, doc, score) in enumerate(zip(original_indices, documents, scores)):
results.append(RerankResult(
text=doc,
score=score,
index=orig_idx
))
results.sort(key=lambda x: x.score, reverse=True)
if request.top_k:
results = results[:request.top_k]
processing_time = time.time() - start_time
logger.info(
f"Reranked {len(documents)} documents in {processing_time:.3f}s "
f"using {request.model_id}"
)
return RerankResponse(
results=results,
query=request.query.strip(),
model_id=request.model_id,
processing_time=processing_time,
total_documents=len(request.documents),
returned_documents=len(results)
)
except ValueError as e:
raise HTTPException(400, str(e))
except Exception as e:
logger.error(f"Reranking failed: {e}")
raise HTTPException(500, f"Reranking failed: {str(e)}")
@app.get("/models", tags=["Models"])
async def list_models():
"""
List all available reranking models.
Returns information about all configured models including their
loading status and capabilities.
Returns:
List of model information dictionaries
"""
try:
return model_manager.list_models()
except Exception as e:
logger.error(f"Failed to list models: {e}")
raise HTTPException(500, str(e))
@app.get("/health", tags=["Monitoring"])
async def health_check():
"""
Check API health and model status.
Returns comprehensive health information including model loading
status and system metrics.
Returns:
Health status dictionary
"""
try:
models = model_manager.list_models()
loaded_models = [m for m in models if m['loaded']]
return {
"status": "ok",
"total_models": len(models),
"loaded_models": len(loaded_models),
"available_models": [m['id'] for m in loaded_models],
"models_info": models
}
except Exception as e:
logger.error(f"Health check failed: {e}")
return {
"status": "error",
"error": str(e)
}
@app.get("/")
async def root():
return {"message": "Welcome to the Multi-Model Reranking API. Visit /docs for API documentation.", "version": "1.0.0"}
|