Update model.py
Browse files2 unmodified lines34567897 unmodified lines17181920212223242526272829303132333417 unmodified lines5253545556575859606162632 unmodified linesimport torchimport torchvision.transforms as transformsimport requestsfrom fastapi import FastAPI# Class labelsCLASS_LABELS = [7 unmodified lines]# Model setupMODEL_URL = "https://huggingface.co/helviz/swin_model/resolve/main/model.pth"MODEL_LOCAL_PATH = "/data/model.pth" # Persistent directory in Hugging Face Spacesdevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")# Function to download the model from Hugging Facedef download_model(url, local_path):# Ensure /data directory existsos.makedirs(os.path.dirname(local_path), exist_ok=True)if not os.path.exists(local_path):print(f"Downloading model from {url}...")response = requests.get(url, stream=True)response.raise_for_status()with open(local_path, "wb") as f:f.write(response.content)print(f"Model downloaded to {local_path}")17 unmodified linesmodel.eval()return model# FastAPI appapp = FastAPI()# Load model at startupmodel = load_model()@app.get("/")async def root():return {"message": "FastAPI app is running, model loaded successfully"}2 unmodified lines3456787 unmodified lines16171819202122232425262728293031323317 unmodified lines51525354552 unmodified linesimport torchimport torchvision.transforms as transformsimport requests# Class labelsCLASS_LABELS = [7 unmodified lines]# Model setupMODEL_URL = "https://huggingface.co/helviz/swin_model/resolve/main/model.pth"MODEL_LOCAL_PATH = "/data/model.pth" # Use persistent /data directory in Hugging Face Spacesdevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")# Function to download the model from Hugging Facedef download_model(url, local_path):# Ensure the directory existsos.makedirs(os.path.dirname(local_path), exist_ok=True)if not os.path.exists(local_path):print(f"Downloading model from {url}...")response = requests.get(url, stream=True)response.raise_for_status() # Check for HTTP errorswith open(local_path, "wb") as f:f.write(response.content)print(f"Model downloaded to {local_path}")17 unmodified linesmodel.eval()return model# Load the modelmodel = load_model()
model.py+7-15