Gaykar commited on
Commit
80a76d6
·
1 Parent(s): 98e25e8

use threads

Browse files
app/TumorClassificationModel/{.gitkeep → __init__.py} RENAMED
File without changes
app/cnnPipeline/pipeline.py CHANGED
@@ -1,12 +1,17 @@
1
- import io
2
  import torch
3
  import torch.nn as nn
4
  from PIL import Image
5
  from torchvision import models, transforms
 
 
 
 
6
 
7
  CLASS_NAMES = ['glioma', 'meningioma', 'no tumor', 'pituitary']
8
- CHECKPOINT = "TumorClassificationModel/model_file.pth"
9
 
 
 
10
 
11
  def load_cnn_model(checkpoint=CHECKPOINT, num_classes=len(CLASS_NAMES)):
12
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
1
+ from pathlib import Path
2
  import torch
3
  import torch.nn as nn
4
  from PIL import Image
5
  from torchvision import models, transforms
6
+ import io
7
+
8
+
9
+ BASE_DIR = Path(__file__).resolve().parent.parent
10
 
11
  CLASS_NAMES = ['glioma', 'meningioma', 'no tumor', 'pituitary']
 
12
 
13
+ # Construct an absolute path to the checkpoint
14
+ CHECKPOINT = BASE_DIR / "TumorClassificationModel" / "model_file.pth"
15
 
16
  def load_cnn_model(checkpoint=CHECKPOINT, num_classes=len(CLASS_NAMES)):
17
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
app/core/config.py CHANGED
@@ -18,11 +18,6 @@ class Settings(BaseSettings):
18
  NEON_BUCKET:str
19
  REDIS_URL:str # For redis queueing
20
 
21
- # --- Database Configuration ---
22
- # Automatically falls back to local if not provided (useful for AWS RDS later)
23
- DATABASE_URL: str
24
-
25
- LANGGRAPH_DB_URL:str
26
  # --- File Paths (Dynamic for Local/Cloud compatibility) ---
27
 
28
  model_config = SettingsConfigDict(
 
18
  NEON_BUCKET:str
19
  REDIS_URL:str # For redis queueing
20
 
 
 
 
 
 
21
  # --- File Paths (Dynamic for Local/Cloud compatibility) ---
22
 
23
  model_config = SettingsConfigDict(
app/main.py CHANGED
@@ -3,13 +3,14 @@ import httpx
3
  import redis
4
  import torch
5
  from celery import Celery
 
6
  from app.cnnPipeline.pipeline import CNNPipeline, load_cnn_model
7
  from app.core.config import settings
8
  from app.core.s3 import presigned_download_url
9
 
10
  cfg = {
11
- "broker": settings.REDIS_URL,
12
- "task": "tasks.process_scan",
13
  "dedupe_ttl": 86400,
14
  "http_timeout": 60,
15
  }
@@ -26,13 +27,12 @@ r = redis.Redis.from_url(cfg["broker"])
26
  _pipeline: CNNPipeline | None = None
27
 
28
 
29
- def get_pipeline() -> CNNPipeline:
30
- """Load the model once per worker process, on first task."""
 
31
  global _pipeline
32
- if _pipeline is None:
33
- torch.set_num_threads(1)
34
- _pipeline = CNNPipeline(load_cnn_model())
35
- return _pipeline
36
 
37
 
38
  async def fetch_image(user_id: str, file_key: str) -> bytes:
@@ -51,7 +51,8 @@ def process_scan(file_key: str, user_id: str):
51
  return
52
  try:
53
  image_bytes = asyncio.run(fetch_image(user_id, file_key))
54
- result = get_pipeline().classify(image_bytes)
 
55
  print(f"[user {user_id}] {file_key} -> {result}")
56
  except Exception:
57
  r.delete(f"scan:{file_key}") # allow a retry
 
3
  import redis
4
  import torch
5
  from celery import Celery
6
+ from celery.signals import worker_init
7
  from app.cnnPipeline.pipeline import CNNPipeline, load_cnn_model
8
  from app.core.config import settings
9
  from app.core.s3 import presigned_download_url
10
 
11
  cfg = {
12
+ "broker": settings.REDIS_URL,
13
+ "task": "tasks.process_scan",
14
  "dedupe_ttl": 86400,
15
  "http_timeout": 60,
16
  }
 
27
  _pipeline: CNNPipeline | None = None
28
 
29
 
30
+ @worker_init.connect
31
+ def load_model(**_):
32
+ """Runs once in the main worker process before the thread pool starts."""
33
  global _pipeline
34
+ torch.set_num_threads(1)
35
+ _pipeline = CNNPipeline(load_cnn_model())
 
 
36
 
37
 
38
  async def fetch_image(user_id: str, file_key: str) -> bytes:
 
51
  return
52
  try:
53
  image_bytes = asyncio.run(fetch_image(user_id, file_key))
54
+ print(len(image_bytes), image_bytes[:16]) # debug: remove once verified
55
+ result = _pipeline.classify(image_bytes)
56
  print(f"[user {user_id}] {file_key} -> {result}")
57
  except Exception:
58
  r.delete(f"scan:{file_key}") # allow a retry