pinkieseb commited on
Commit
027b8fc
1 Parent(s): 776cd33

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +9 -16
app.py CHANGED
@@ -1,27 +1,20 @@
1
- import torch
2
  from transformers import AutoFeatureExtractor, EfficientNetForImageClassification
 
3
  from PIL import Image
4
- import base64
5
  import io
 
6
 
7
- def preprocess_image(image_data):
8
- if image_data.startswith('data:image'):
9
- image_data = image_data.split(',')[1]
10
- image_bytes = base64.b64decode(image_data)
11
- image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
12
- return image
13
-
14
- def predict(image_data):
15
- image = preprocess_image(image_data)
16
 
17
- feature_extractor = AutoFeatureExtractor.from_pretrained("./huggingface_model")
18
- model = EfficientNetForImageClassification.from_pretrained("./huggingface_model")
19
 
20
  # Replace the classification head with a regression head
21
  model.classifier = torch.nn.Linear(model.classifier.in_features, 1)
22
 
23
  # Load the custom weights
24
- model.load_state_dict(torch.load("./model.pt", map_location=torch.device('cpu')))
25
  model.eval()
26
 
27
  inputs = feature_extractor(images=image, return_tensors="pt")
@@ -33,5 +26,5 @@ def predict(image_data):
33
 
34
  return {"prediction": float(prediction)}
35
 
36
- def run(raw_image_data):
37
- return predict(raw_image_data)
 
 
1
  from transformers import AutoFeatureExtractor, EfficientNetForImageClassification
2
+ import torch
3
  from PIL import Image
 
4
  import io
5
+ import base64
6
 
7
+ def pipeline(image_bytes):
8
+ image = Image.open(io.BytesIO(base64.b64decode(image_bytes))).convert('RGB')
 
 
 
 
 
 
 
9
 
10
+ feature_extractor = AutoFeatureExtractor.from_pretrained(".")
11
+ model = EfficientNetForImageClassification.from_pretrained(".")
12
 
13
  # Replace the classification head with a regression head
14
  model.classifier = torch.nn.Linear(model.classifier.in_features, 1)
15
 
16
  # Load the custom weights
17
+ model.load_state_dict(torch.load("model.pt", map_location=torch.device('cpu')))
18
  model.eval()
19
 
20
  inputs = feature_extractor(images=image, return_tensors="pt")
 
26
 
27
  return {"prediction": float(prediction)}
28
 
29
+ def run(raw_image_bytes):
30
+ return pipeline(raw_image_bytes)