File size: 1,452 Bytes
523127e
62f57cf
 
 
3e93ea5
2ceb040
 
60fda7b
 
 
2ceb040
 
 
523127e
 
 
62f57cf
 
 
 
3e93ea5
2ceb040
 
3e93ea5
 
e5e59bf
 
 
 
60fda7b
3e93ea5
2ceb040
60fda7b
 
 
 
 
2ceb040
60fda7b
 
 
2ceb040
 
 
60fda7b
2ceb040
62f57cf
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI
from transformers import pipeline
import torch
from pydantic import BaseModel
import os
import numpy as np  # Explicit numpy import

from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
import torch

# Fix numpy initialization
np.zeros(1)  # Force numpy load before model


app = FastAPI()

# Disable xformers if needed
torch.backends.cuda.enable_flash_sdp(False)
torch.backends.cuda.enable_mem_efficient_sdp(False)


# Configure paths and device
os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"
os.makedirs(os.environ["TRANSFORMERS_CACHE"], exist_ok=True)

@app.get("/ready")
def readiness_check():
    return {"status": "ready"}

model_name = "win2win/3-epochs-classifier-ver2"

try:
    model = AutoModelForSequenceClassification.from_pretrained(model_name)
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    
    # Create pipeline with explicit classes
    classifier = pipeline(
        "text-classification",
        model=model,
        tokenizer=tokenizer,
        device="cuda" if torch.cuda.is_available() else "cpu"
    )
    print("Model loaded successfully!")
except Exception as e:
    print(f"Error loading model: {str(e)}")
    raise

class Request(BaseModel):
    text: str

@app.post("/predict")
async def predict(request: Request):
    return model(request.text)

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)