VaneshDev commited on
Commit
085f757
·
verified ·
1 Parent(s): fd393fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -3
app.py CHANGED
@@ -3,9 +3,10 @@ import torch
3
  from torchvision import models, transforms
4
  import os
5
  import time
 
 
6
 
7
  # Set up logging (optional for debugging)
8
- import logging
9
  logging.basicConfig(level=logging.DEBUG)
10
  logger = logging.getLogger(__name__)
11
 
@@ -19,9 +20,16 @@ conditions = [
19
  "Appendicitis", "Gallstones", "Kidney Stones", "Infections", "Abdominal Aortic Aneurysm", "Diverticulitis"
20
  ]
21
 
 
 
 
 
 
 
 
 
22
  # Function to load the model efficiently
23
  def load_model():
24
- model_path = "/mnt/data/densenet121-a639ec97.pth" # Set the model path
25
  if os.path.exists(model_path):
26
  model = models.densenet121()
27
  model.load_state_dict(torch.load(model_path)) # Load from cached path
@@ -30,13 +38,16 @@ def load_model():
30
  else:
31
  model = models.densenet121(weights="IMAGENET1K_V1") # If not cached, download model
32
  torch.save(model.state_dict(), model_path) # Cache the model locally
33
- model.eval()
34
  logger.info("Downloaded and cached the model.")
35
  return model
36
 
37
  # Load the model at the beginning (this will take time but only happens once)
38
  model = load_model()
39
 
 
 
 
 
40
  # Define image preprocessing function
41
  def preprocess_image(image):
42
  transform = transforms.Compose([
 
3
  from torchvision import models, transforms
4
  import os
5
  import time
6
+ import logging
7
+ import fitz # PyMuPDF for better PDF parsing
8
 
9
  # Set up logging (optional for debugging)
 
10
  logging.basicConfig(level=logging.DEBUG)
11
  logger = logging.getLogger(__name__)
12
 
 
20
  "Appendicitis", "Gallstones", "Kidney Stones", "Infections", "Abdominal Aortic Aneurysm", "Diverticulitis"
21
  ]
22
 
23
+ # Define path to store the model manually
24
+ model_path = "/home/user/.cache/torch/hub/checkpoints/densenet121-a639ec97.pth" # Adjusted to a valid path
25
+
26
+ # Ensure the parent directory exists
27
+ parent_dir = os.path.dirname(model_path)
28
+ if not os.path.exists(parent_dir):
29
+ os.makedirs(parent_dir) # Create the parent directory if it doesn't exist
30
+
31
  # Function to load the model efficiently
32
  def load_model():
 
33
  if os.path.exists(model_path):
34
  model = models.densenet121()
35
  model.load_state_dict(torch.load(model_path)) # Load from cached path
 
38
  else:
39
  model = models.densenet121(weights="IMAGENET1K_V1") # If not cached, download model
40
  torch.save(model.state_dict(), model_path) # Cache the model locally
 
41
  logger.info("Downloaded and cached the model.")
42
  return model
43
 
44
  # Load the model at the beginning (this will take time but only happens once)
45
  model = load_model()
46
 
47
+ # Define device for model inference
48
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
49
+ model = model.to(device)
50
+
51
  # Define image preprocessing function
52
  def preprocess_image(image):
53
  transform = transforms.Compose([