File size: 2,086 Bytes
4225683
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field
from typing import List
from model import model_instance
import time
import logging

# Logging setup
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = FastAPI(
    title="Sentence Embedding API",
    description="Aapke trained model se text embedding nikaalne ka API",
    version="1.0.0"
)

# Request body ka structure
class TextInput(BaseModel):
    text: str = Field(..., min_length=1, max_length=512, example="Mera naam Bahadur hai")
    
class EmbeddingResponse(BaseModel):
    embedding: List[float]
    input_text: str
    inference_time_ms: float

# Health check endpoint
@app.get("/")
def root():
    return {"message": "API is running! Go to /docs for Swagger UI"}

@app.get("/health")
def health_check():
    return {"status": "healthy", "model_loaded": True}

# Main prediction endpoint
@app.post("/embed", response_model=EmbeddingResponse)
async def get_embedding(input_data: TextInput):
    try:
        logger.info(f"Processing text: {input_data.text[:50]}...")
        
        start_time = time.time()
        embedding = model_instance.get_embedding(input_data.text)
        inference_time = (time.time() - start_time) * 1000  # milliseconds
        
        return EmbeddingResponse(
            embedding=embedding,
            input_text=input_data.text,
            inference_time_ms=round(inference_time, 2)
        )
    
    except Exception as e:
        logger.error(f"Error: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))

# Batch processing (optional)
class BatchTextInput(BaseModel):
    texts: List[str]

@app.post("/embed/batch")
async def get_batch_embeddings(input_data: BatchTextInput):
    results = []
    for text in input_data.texts:
        embedding = model_instance.get_embedding(text)
        results.append({
            "text": text,
            "embedding": embedding
        })
    return {"results": results, "count": len(results)}