Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# 모델과 feature extractor 로드
|
| 7 |
+
model_name = "shinyice/densenet121-dog-emotions"
|
| 8 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
| 9 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
def predict_emotion(image):
|
| 12 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 13 |
+
with torch.no_grad():
|
| 14 |
+
outputs = model(**inputs)
|
| 15 |
+
logits = outputs.logits
|
| 16 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 17 |
+
return model.config.id2label[predicted_class_idx]
|
| 18 |
+
|
| 19 |
+
# Gradio 인터페이스 생성
|
| 20 |
+
interface = gr.Interface(
|
| 21 |
+
fn=predict_emotion,
|
| 22 |
+
inputs=gr.inputs.Image(type="pil"),
|
| 23 |
+
outputs="text",
|
| 24 |
+
title="Dog Emotion Recognition",
|
| 25 |
+
description="Upload an image of your dog and get its predicted emotion."
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
interface.launch()
|