Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
from PIL import Image
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
def dog_classifier(dog_image):
|
7 |
+
image = Image.fromarray(dog_image)
|
8 |
+
dog_classifier = pipeline("image-classification", model='raffaelsiregar/dog-breeds-classification')
|
9 |
+
output = dog_classifier(image)
|
10 |
+
|
11 |
+
# creating pandas dataframe
|
12 |
+
df = pd.DataFrame(output)
|
13 |
+
|
14 |
+
# adjust score column
|
15 |
+
df['score'] = df['score'] * 100
|
16 |
+
df['score'] = df['score'].apply(lambda x: round(x, 4))
|
17 |
+
|
18 |
+
# rename the columns to make it more user-friendly
|
19 |
+
df.columns = ['Breed', 'Confidence (%)']
|
20 |
+
|
21 |
+
return df
|
22 |
+
|
23 |
+
title = "Dog Breed Classification"
|
24 |
+
description = "Upload an image (jpg is recommended) of a dog to predict its breed. The model will provide the top predictions with the confidence levels."
|
25 |
+
article = """
|
26 |
+
### How It Works
|
27 |
+
- The model classifies the breed of the dog in the image.
|
28 |
+
- It returns a table of the top predictions along with their confidence levels.
|
29 |
+
- This tool is built using a pre-trained image classification model from Hugging Face.
|
30 |
+
"""
|
31 |
+
themes = gr.themes.Citrus()
|
32 |
+
|
33 |
+
input_image = gr.Image(type="numpy", label="Upload a dog image")
|
34 |
+
output_table = gr.DataFrame(headers=["Breed", "Confidence (%)"], type="pandas")
|
35 |
+
|
36 |
+
# gradio interface
|
37 |
+
interface = gr.Interface(fn=dog_classifier,
|
38 |
+
inputs=input_image,
|
39 |
+
outputs=output_table,
|
40 |
+
title=title,
|
41 |
+
description=description,
|
42 |
+
article=article,
|
43 |
+
theme=themes)
|
44 |
+
interface.launch()
|