Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from fastai.vision.all import *
|
| 3 |
+
from fastai.vision.all import PILImage
|
| 4 |
+
|
| 5 |
+
# Load the trained model
|
| 6 |
+
learn = load_learner('levit.pkl')
|
| 7 |
+
|
| 8 |
+
# Get the labels from the data loaders
|
| 9 |
+
labels = learn.dls.vocab
|
| 10 |
+
|
| 11 |
+
# Define the prediction function
|
| 12 |
+
def predict(img):
|
| 13 |
+
img = PILImage.create(img)
|
| 14 |
+
img = img.resize((512, 512))
|
| 15 |
+
pred, pred_idx, probs = learn.predict(img)
|
| 16 |
+
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
| 17 |
+
|
| 18 |
+
# Example images for demonstration
|
| 19 |
+
examples = ['image.jpg']
|
| 20 |
+
|
| 21 |
+
# Create the Gradio interface
|
| 22 |
+
interface = gr.Interface(
|
| 23 |
+
fn=predict,
|
| 24 |
+
inputs=gr.Image(),
|
| 25 |
+
outputs=gr.Label(num_top_classes=3)
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Enable the queue to handle POST requests
|
| 29 |
+
interface.queue(api_open=True)
|
| 30 |
+
|
| 31 |
+
# Launch the interface
|
| 32 |
+
interface.launch()
|