Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import numpy as np
|
3 |
+
import tensorflow as tf
|
4 |
+
from PIL import Image
|
5 |
+
from io import BytesIO
|
6 |
+
import base64
|
7 |
+
|
8 |
+
# Load the model when the script is loaded
|
9 |
+
model = tf.keras.models.load_model("model")
|
10 |
+
|
11 |
+
# Your specific class labels
|
12 |
+
class_labels = {
|
13 |
+
0: "Fake",
|
14 |
+
1: "Low",
|
15 |
+
2: "Medium",
|
16 |
+
3: "High"
|
17 |
+
}
|
18 |
+
|
19 |
+
def preprocess_image(image):
|
20 |
+
"""Preprocess the image for model prediction"""
|
21 |
+
# Resize image to model's expected input dimensions
|
22 |
+
image = image.resize((128, 128))
|
23 |
+
|
24 |
+
# Convert to numpy array and normalize
|
25 |
+
img_array = np.array(image) / 255.0
|
26 |
+
|
27 |
+
# Add batch dimension
|
28 |
+
img_array = np.expand_dims(img_array, axis=0)
|
29 |
+
return img_array
|
30 |
+
|
31 |
+
def predict_image(image):
|
32 |
+
"""Make prediction on a single image"""
|
33 |
+
img_array = preprocess_image(image)
|
34 |
+
predictions = model.predict(img_array)
|
35 |
+
predicted_class_idx = np.argmax(predictions)
|
36 |
+
predicted_class = class_labels[predicted_class_idx]
|
37 |
+
confidence = float(np.max(predictions))
|
38 |
+
|
39 |
+
return {
|
40 |
+
"predicted_class": predicted_class,
|
41 |
+
"confidence": confidence,
|
42 |
+
"class_probabilities": {class_labels[i]: float(prob) for i, prob in enumerate(predictions[0])}
|
43 |
+
}
|
44 |
+
|
45 |
+
def inference(data):
|
46 |
+
"""
|
47 |
+
Inference function for Hugging Face API
|
48 |
+
|
49 |
+
data can be:
|
50 |
+
- File path (string)
|
51 |
+
- URL string
|
52 |
+
- Base64 encoded image
|
53 |
+
- Raw image bytes
|
54 |
+
- Dict with image key containing any of the above
|
55 |
+
"""
|
56 |
+
# Handle different input formats
|
57 |
+
if isinstance(data, dict) and "image" in data:
|
58 |
+
data = data["image"]
|
59 |
+
|
60 |
+
# Handle local file path
|
61 |
+
if isinstance(data, str) and os.path.isfile(data):
|
62 |
+
image = Image.open(data)
|
63 |
+
|
64 |
+
# Handle URL (Hugging Face will download the image)
|
65 |
+
elif isinstance(data, str) and (data.startswith("http://") or data.startswith("https://")):
|
66 |
+
from urllib.request import urlopen
|
67 |
+
with urlopen(data) as response:
|
68 |
+
image_bytes = response.read()
|
69 |
+
image = Image.open(BytesIO(image_bytes))
|
70 |
+
|
71 |
+
# Handle base64 encoded image
|
72 |
+
elif isinstance(data, str) and data.startswith("data:image"):
|
73 |
+
base64_data = data.split(",")[1]
|
74 |
+
image_bytes = base64.b64decode(base64_data)
|
75 |
+
image = Image.open(BytesIO(image_bytes))
|
76 |
+
|
77 |
+
# Handle raw image bytes
|
78 |
+
elif isinstance(data, bytes):
|
79 |
+
image = Image.open(BytesIO(data))
|
80 |
+
|
81 |
+
# Convert RGBA to RGB if needed
|
82 |
+
if image.mode == "RGBA":
|
83 |
+
image = image.convert("RGB")
|
84 |
+
|
85 |
+
# Make prediction
|
86 |
+
return predict_image(image)
|
87 |
+
|
88 |
+
# For local testing
|
89 |
+
if __name__ == "__main__":
|
90 |
+
# Example of using a file path
|
91 |
+
test_image_path = "path/to/test/image.jpg"
|
92 |
+
if os.path.exists(test_image_path):
|
93 |
+
result = inference(test_image_path)
|
94 |
+
print(f"Predicted class: {result['predicted_class']}")
|
95 |
+
print(f"Confidence: {result['confidence']:.4f}")
|