Download main.py from Gaykar/CNN: direct link, hf CLI and curl.
- Browser
- Download file 850 Bytes
-
https://huggingface.co/spaces/Gaykar/CNN/resolve/main/main.py
- Command line
-
hf download hf://spaces/Gaykar/CNN/main.py
-
curl -L -o main.py https://huggingface.co/spaces/Gaykar/CNN/resolve/main/main.py
850 Bytes
| import asyncio | |
| from concurrent.futures import ThreadPoolExecutor | |
| from fastapi import FastAPI, UploadFile, File | |
| from cnn_pipeline.pipeline import classify_tumor | |
| from contextlib import asynccontextmanager | |
| async def lifespan(app: FastAPI): | |
| yield # The app is running at this point | |
| executor.shutdown() | |
| app = FastAPI(title="Brain Tumor CNN Service",lifespan=lifespan) | |
| # its okay to use thead pool (prototype level) as pytorch surpasses GIL | |
| executor=ThreadPoolExecutor(max_workers=2) | |
| async def classify(file: UploadFile = File(...)): | |
| # read the fiel content | |
| image_bytes = await file.read() | |
| # get the event Loop | |
| loop = asyncio.get_running_loop() | |
| result = await loop.run_in_executor(executor, classify_tumor, image_bytes) | |
| return result | |