Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,37 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
-
|
| 8 |
-
processor = BlipProcessor.from_pretrained(model_name)
|
| 9 |
-
model = BlipForConditionalGeneration.from_pretrained(model_name)
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# Preprocess the image
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
generated_ids = model.generate(pixel_values=pixel_values)
|
| 20 |
-
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 21 |
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
iface = gr.Interface(
|
| 26 |
-
fn=classify_image,
|
| 27 |
-
inputs=gr.Image(type="pil"),
|
| 28 |
-
outputs=gr.
|
| 29 |
-
title="Image
|
| 30 |
-
description="Upload an image
|
| 31 |
)
|
| 32 |
|
| 33 |
# Launch the interface
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from tensorflow.keras.applications import ResNet50
|
| 4 |
+
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
|
| 5 |
from PIL import Image
|
| 6 |
+
import numpy as np
|
| 7 |
|
| 8 |
+
# Load pre-trained ResNet50 model + higher level layers
|
| 9 |
+
model = ResNet50(weights='imagenet')
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
def classify_image(img):
|
| 12 |
+
# Resize the image to 224x224 pixels (required input size for ResNet50)
|
| 13 |
+
img = img.resize((224, 224))
|
| 14 |
+
# Convert the image to array
|
| 15 |
+
img_array = np.array(img)
|
| 16 |
+
# Expand dimensions to match the shape required by the model
|
| 17 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 18 |
# Preprocess the image
|
| 19 |
+
img_array = preprocess_input(img_array)
|
| 20 |
+
# Predict the classification
|
| 21 |
+
predictions = model.predict(img_array)
|
| 22 |
+
# Decode predictions into readable labels
|
| 23 |
+
decoded_predictions = decode_predictions(predictions, top=3)[0]
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# Format the output
|
| 26 |
+
return {label: float(probability) for (_, label, probability) in decoded_predictions}
|
| 27 |
|
| 28 |
+
# Gradio interface
|
| 29 |
iface = gr.Interface(
|
| 30 |
+
fn=classify_image, # Function to call for predictions
|
| 31 |
+
inputs=gr.Image(type="pil"), # Input is an image
|
| 32 |
+
outputs=gr.Label(num_top_classes=3), # Output is a label with top 3 predictions
|
| 33 |
+
title="Contextual Image Classification",
|
| 34 |
+
description="Upload an image, and the model will classify it based on the context."
|
| 35 |
)
|
| 36 |
|
| 37 |
# Launch the interface
|