win2win's picture
Update app.py
e5e59bf verified
raw
history blame
1.3 kB
from fastapi import FastAPI
from transformers import pipeline
import torch
from pydantic import BaseModel
import os
import numpy as np # Explicit numpy import
# 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"}
# Load model with error handling
try:
model = pipeline(
"text-classification",
model="win2win/3-epochs-classifier-ver2",
device="cpu", # Force CPU for free tier
device_map="auto",
torch_dtype=torch.float32, # Avoid mixed precision
offline_mode=False, # Force fresh download
max_retries=3
)
print("Model loaded successfully!")
except Exception as e:
print(f"Model loading failed: {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)