Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,58 +1,60 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
import
|
4 |
-
from
|
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 |
-
model_payload['
|
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 |
return {"message": "Embedding Service is running. Use the /embed endpoint."}
|
|
|
1 |
+
import os
|
2 |
+
os.environ['TRANSFORMERS_CACHE'] = '/tmp/cache'
|
3 |
+
from fastapi import FastAPI
|
4 |
+
from pydantic import BaseModel
|
5 |
+
import torch
|
6 |
+
from transformers import AutoModel, AutoTokenizer
|
7 |
+
from typing import List
|
8 |
+
|
9 |
+
# --- Configuration ---
|
10 |
+
EMBEDDING_MODEL_NAME = 'krutrim-ai-labs/vyakyarth'
|
11 |
+
|
12 |
+
# --- Helper Function for Mean Pooling ---
|
13 |
+
def mean_pooling(model_output, attention_mask):
|
14 |
+
token_embeddings = model_output[0]
|
15 |
+
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
16 |
+
sum_embeddings = torch.sum(token_embeddings * input_mask_expanded, 1)
|
17 |
+
sum_mask = torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
18 |
+
return sum_embeddings / sum_mask
|
19 |
+
|
20 |
+
# --- FastAPI App Initialization ---
|
21 |
+
app = FastAPI(title="Embedding Service")
|
22 |
+
|
23 |
+
# --- Load Model on Startup ---
|
24 |
+
# This dictionary will hold the loaded model and tokenizer
|
25 |
+
model_payload = {}
|
26 |
+
|
27 |
+
@app.on_event("startup")
|
28 |
+
def load_model():
|
29 |
+
"""Load the model and tokenizer when the server starts."""
|
30 |
+
print(f"Loading embedding model: {EMBEDDING_MODEL_NAME}...")
|
31 |
+
model_payload['tokenizer'] = AutoTokenizer.from_pretrained(EMBEDDING_MODEL_NAME)
|
32 |
+
model_payload['model'] = AutoModel.from_pretrained(EMBEDDING_MODEL_NAME)
|
33 |
+
model_payload['model'].eval()
|
34 |
+
print("Model loaded successfully.")
|
35 |
+
|
36 |
+
# --- Pydantic Models for Request/Response ---
|
37 |
+
class EmbeddingRequest(BaseModel):
|
38 |
+
text: str
|
39 |
+
|
40 |
+
class EmbeddingResponse(BaseModel):
|
41 |
+
embedding: List[float]
|
42 |
+
|
43 |
+
# --- API Endpoint ---
|
44 |
+
@app.post("/embed", response_model=EmbeddingResponse)
|
45 |
+
def create_embedding(request: EmbeddingRequest):
|
46 |
+
"""Takes text and returns its vector embedding."""
|
47 |
+
tokenizer = model_payload['tokenizer']
|
48 |
+
model = model_payload['model']
|
49 |
+
|
50 |
+
encoded_input = tokenizer(request.text, padding=True, truncation=True, return_tensors='pt')
|
51 |
+
with torch.no_grad():
|
52 |
+
model_output = model(**encoded_input)
|
53 |
+
|
54 |
+
embedding = mean_pooling(model_output, encoded_input['attention_mask']).tolist()[0]
|
55 |
+
|
56 |
+
return {"embedding": embedding}
|
57 |
+
|
58 |
+
@app.get("/")
|
59 |
+
def read_root():
|
60 |
return {"message": "Embedding Service is running. Use the /embed endpoint."}
|