Spaces:
Build error
Build error
smdbs commited on
Commit ·
97c208d
1
Parent(s): aa5504b
feat: use resnet50
Browse files- app.py +25 -11
- requirements.txt +2 -0
app.py
CHANGED
|
@@ -1,16 +1,30 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
salutation = "Good morning" if is_morning else "Good evening"#sssss
|
| 6 |
-
greeting = f"{salutation} {name}. It is {temperature} degrees today"
|
| 7 |
-
celsius = (temperature - 32) * 5 / 9
|
| 8 |
-
return greeting, round(celsius, 2)
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
)
|
|
|
|
| 15 |
if __name__ == "__main__":
|
| 16 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
from transformers import AutoImageProcessor, ResNetForImageClassification
|
| 4 |
+
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
print("Loading model...")
|
| 7 |
+
processor = AutoImageProcessor.from_pretrained("microsoft/resnet-50")
|
| 8 |
+
model = ResNetForImageClassification.from_pretrained("microsoft/resnet-50")
|
| 9 |
+
print("Model loaded.")
|
| 10 |
+
|
| 11 |
+
def resnet50(image):
|
| 12 |
+
inputs = processor(image, return_tensors="pt")
|
| 13 |
+
|
| 14 |
+
with torch.no_grad():
|
| 15 |
+
logits = model(**inputs).logits
|
| 16 |
+
|
| 17 |
+
# model predicts one of the 1000 ImageNet classes
|
| 18 |
+
predicted_label = logits.argmax(-1).item()
|
| 19 |
+
return model.config.id2label[predicted_label]
|
| 20 |
+
|
| 21 |
+
ui = gr.Interface(
|
| 22 |
+
fn=resnet50,
|
| 23 |
+
inputs=gr.Image(height=224, width=224),
|
| 24 |
+
outputs=gr.Text(),
|
| 25 |
+
title="ResNet-50 ImageNet",
|
| 26 |
+
description="Identify the main object in an image. This model is a ResNet-50 neural network pre-trained on ImageNet."
|
| 27 |
)
|
| 28 |
+
|
| 29 |
if __name__ == "__main__":
|
| 30 |
+
ui.launch()
|
requirements.txt
CHANGED
|
@@ -1,2 +1,4 @@
|
|
| 1 |
gradio==4.42.0
|
| 2 |
fastapi==0.112.2
|
|
|
|
|
|
|
|
|
| 1 |
gradio==4.42.0
|
| 2 |
fastapi==0.112.2
|
| 3 |
+
torch
|
| 4 |
+
transformers
|