Spaces:
Build error
Build error
Commit
·
d76860c
1
Parent(s):
8a27b89
Create new file
Browse files
app.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from huggingface_hub import from_pretrained_fastai
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
examples = ["llama.jpg"]
|
| 7 |
+
repo_id = "osanseviero/is_it_a_llama"
|
| 8 |
+
path = Path("./")
|
| 9 |
+
|
| 10 |
+
def get_y(r):
|
| 11 |
+
return r["label"]
|
| 12 |
+
|
| 13 |
+
def get_x(r):
|
| 14 |
+
return path/r["fname"]
|
| 15 |
+
|
| 16 |
+
learner = from_pretrained_fastai(repo_id)
|
| 17 |
+
labels = learner.dls.vocab
|
| 18 |
+
|
| 19 |
+
def inference(image):
|
| 20 |
+
label_predict,_,probs = learner.predict(image)
|
| 21 |
+
labels_probs = {labels[i]: float(probs[i]) for i, _ in enumerate(labels)}
|
| 22 |
+
return labels_probs
|
| 23 |
+
|
| 24 |
+
gr.Interface(
|
| 25 |
+
fn=inference,
|
| 26 |
+
title="Llama image classification",
|
| 27 |
+
description = "Predict if this image has a llama (or a forest)",
|
| 28 |
+
inputs="image",
|
| 29 |
+
outputs="label",
|
| 30 |
+
examples=examples,
|
| 31 |
+
).launch(debug=True, enable_queue=True)
|