Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
-
model = tf.keras.models.load_model("MobileNet_model.h5")
|
| 8 |
-
|
| 9 |
-
# Define class names
|
| 10 |
-
class_names = ["Fake", "Low", "Medium", "High"] # Modify if needed
|
| 11 |
-
|
| 12 |
-
# Image Preprocessing Function
|
| 13 |
-
img_size = (128, 128) # Ensure it matches the input size used during training
|
| 14 |
-
|
| 15 |
-
def preprocess_image(image):
|
| 16 |
-
image = image.resize(img_size) # Resize image
|
| 17 |
-
image = np.array(image) / 255.0 # Normalize as done in ImageDataGenerator (rescale=1./255)
|
| 18 |
-
image = np.expand_dims(image, axis=0) # Add batch dimension
|
| 19 |
-
return image
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
return {"Predicted Class": class_names[
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
fn=predict,
|
| 33 |
-
inputs=gr.Image(type="pil"), # Accept image as input
|
| 34 |
-
outputs=gr.JSON(), # Return JSON response
|
| 35 |
-
title="Fire Detection API",
|
| 36 |
-
description="Send an image to classify it into one of four categories: Fake, Low, Medium, or High."
|
| 37 |
-
)
|
| 38 |
|
| 39 |
-
# Launch API
|
| 40 |
-
if __name__ == "__main__":
|
| 41 |
-
interface.launch(share=True)
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # Disable GPU warnings
|
| 3 |
+
|
| 4 |
import gradio as gr
|
| 5 |
import tensorflow as tf
|
| 6 |
import numpy as np
|
| 7 |
from PIL import Image
|
| 8 |
|
| 9 |
+
# Load model
|
| 10 |
+
model = tf.keras.models.load_model("MobileNet_model.h5")
|
| 11 |
+
class_names = ["Fake", "Low", "Medium", "High"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
def predict_image(img):
|
| 14 |
+
img = img.resize((128, 128))
|
| 15 |
+
img_array = np.array(img) / 255.0
|
| 16 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 17 |
+
predictions = model.predict(img_array)
|
| 18 |
+
class_index = np.argmax(predictions, axis=1)[0]
|
| 19 |
+
confidence_scores = {class_names[i]: float(predictions[0][i]) for i in range(len(class_names))}
|
| 20 |
+
return {"Predicted Class": class_names[class_index], "Confidence Scores": confidence_scores}
|
| 21 |
|
| 22 |
+
iface = gr.Interface(fn=predict_image, inputs="image", outputs="json")
|
| 23 |
+
iface.launch(server_name="0.0.0.0", server_port=7860) # Fix for Hugging Face
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
|
|
|
|
|