Spaces:
Sleeping
Sleeping
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def load_label_to_name_mapping(json_file_path):
|
| 7 |
+
"""Load the label-to-name mapping from a JSON file."""
|
| 8 |
+
with open(json_file_path, 'r') as f:
|
| 9 |
+
mapping = json.load(f)
|
| 10 |
+
return {int(k): v for k, v in mapping.items()}
|
| 11 |
+
|
| 12 |
+
def infer_flower_name(classifier, image):
|
| 13 |
+
"""Perform inference on an image and return the flower name."""
|
| 14 |
+
# Perform inference
|
| 15 |
+
# Load the model checkpoint for inference
|
| 16 |
+
|
| 17 |
+
result = classifier(image)
|
| 18 |
+
# Get the label from the inference result
|
| 19 |
+
label = result[0]['label'].split('_')[-1] # The label is usually in the format 'LABEL_#'
|
| 20 |
+
label = int(label)
|
| 21 |
+
|
| 22 |
+
# Map the integer label to the flower name
|
| 23 |
+
json_file_path = 'label_to_name.json'
|
| 24 |
+
label_to_name = load_label_to_name_mapping(json_file_path)
|
| 25 |
+
flower_name = label_to_name.get(label, "Unknown")
|
| 26 |
+
|
| 27 |
+
return flower_name
|
| 28 |
+
|
| 29 |
+
def predict(flower):# would call a model to make a prediction on an input and return the output.
|
| 30 |
+
classifier = pipeline("image-classification", model="checkpoint-160")
|
| 31 |
+
flower_name = infer_flower_name(classifier, flower)
|
| 32 |
+
return flower_name
|
| 33 |
+
|
| 34 |
+
#def predict2(flower2): # output top 3 with prob?
|
| 35 |
+
# classifier = pipeline("image-classification", model="checkpoint-160")
|
| 36 |
+
# result = classifier(flower2)
|
| 37 |
+
# print(result)
|
| 38 |
+
# return result
|
| 39 |
+
|
| 40 |
+
description = "Upload an image of a flower and discover its species!"
|
| 41 |
+
title = "Bloom Classifier"
|
| 42 |
+
examples = ["example.jpg", "image_00293.jpg","image_02828.jpg"]
|
| 43 |
+
demo = gr.Interface(fn=predict,
|
| 44 |
+
inputs=gr.Image(type="pil"),
|
| 45 |
+
outputs=gr.Label(num_top_classes=3),
|
| 46 |
+
description=description,
|
| 47 |
+
title = title,
|
| 48 |
+
examples=examples)
|
| 49 |
+
|
| 50 |
+
demo.launch()
|