Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import ImageClassificationPipeline, PerceiverForImageClassificationConvProcessing, PerceiverFeatureExtractor
|
| 3 |
+
|
| 4 |
+
feature_extractor = PerceiverFeatureExtractor()
|
| 5 |
+
model = PerceiverForImageClassificationConvProcessing.from_pretrained("deepmind/vision-perceiver-conv")
|
| 6 |
+
|
| 7 |
+
# define custom pipeline as Perceiver expects "inputs" rather than "pixel_values"
|
| 8 |
+
class CustomPipeline(ImageClassificationPipeline):
|
| 9 |
+
def _forward(self, model_inputs):
|
| 10 |
+
inputs = model_inputs["pixel_values"]
|
| 11 |
+
model_outputs = self.model(inputs=inputs)
|
| 12 |
+
return model_outputs
|
| 13 |
+
|
| 14 |
+
image_pipe = CustomPipeline(model=model, feature_extractor=feature_extractor)
|
| 15 |
+
|
| 16 |
+
def classify_image(image):
|
| 17 |
+
results = image_pipe(image)
|
| 18 |
+
# convert to format Gradio expects
|
| 19 |
+
output = {}
|
| 20 |
+
for prediction in results:
|
| 21 |
+
predicted_label = prediction['label']
|
| 22 |
+
score = prediction['score']
|
| 23 |
+
output[predicted_label] = score
|
| 24 |
+
return output
|
| 25 |
+
|
| 26 |
+
image = gr.inputs.Image(type="pil")
|
| 27 |
+
label = gr.outputs.Label(num_top_classes=5)
|
| 28 |
+
|
| 29 |
+
gr.Interface(fn=classify_image, inputs=image, outputs=label, enable_queue=True).launch(debug=True)
|