Upload app.py with huggingface_hub
Browse files
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
|
| 8 |
-
|
| 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(".
|
| 18 |
-
model = EfficientNetForImageClassification.from_pretrained(".
|
| 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("
|
| 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(
|
| 37 |
-
return
|
|
|
|
|
|
|
| 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)
|