Update app.py
Browse files
app.py
CHANGED
|
@@ -4,30 +4,32 @@ from torchvision import transforms
|
|
| 4 |
from PIL import Image
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
# Загрузка модели и меток
|
| 8 |
def load_model():
|
| 9 |
model_path = "skin_disease_model_jit.pt"
|
| 10 |
labels_path = "labels.txt"
|
| 11 |
-
|
| 12 |
if not os.path.exists(model_path):
|
| 13 |
raise FileNotFoundError(f"Модель не найдена: {model_path}")
|
| 14 |
if not os.path.exists(labels_path):
|
| 15 |
raise FileNotFoundError("Файл labels.txt не найден.")
|
| 16 |
-
|
| 17 |
model = torch.jit.load(model_path, map_location=torch.device('cpu'))
|
| 18 |
model.eval()
|
| 19 |
-
|
| 20 |
with open(labels_path, "r") as f:
|
| 21 |
labels = [line.strip() for line in f.readlines()]
|
| 22 |
-
|
| 23 |
return model, labels
|
| 24 |
|
| 25 |
model, labels = load_model()
|
| 26 |
|
|
|
|
| 27 |
preprocess = transforms.Compose([
|
| 28 |
transforms.Resize((224, 224)),
|
| 29 |
transforms.ToTensor(),
|
| 30 |
-
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
|
|
|
| 31 |
])
|
| 32 |
|
| 33 |
# Функция предсказания
|
|
@@ -38,32 +40,23 @@ def predict(image):
|
|
| 38 |
with torch.no_grad():
|
| 39 |
output = model(image_tensor)
|
| 40 |
scores = torch.nn.functional.softmax(output[0], dim=0)
|
| 41 |
-
|
| 42 |
return {label: float(score) for label, score in zip(labels, scores)}
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
title = "
|
| 46 |
-
description = "
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
.
|
| 52 |
-
""
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
gr.
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
with gr.Column():
|
| 60 |
-
image_input = gr.Image(type="pil", label="🖼️ Изображение", interactive=True)
|
| 61 |
-
predict_button = gr.Button("🔍 Анализировать", variant="primary")
|
| 62 |
-
with gr.Column():
|
| 63 |
-
result_label = gr.Label(num_top_classes=3, label="📊 Предсказания")
|
| 64 |
-
|
| 65 |
-
predict_button.click(fn=predict, inputs=image_input, outputs=result_label)
|
| 66 |
-
|
| 67 |
-
# Запуск
|
| 68 |
if __name__ == "__main__":
|
| 69 |
-
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# Загрузка модели и меток
|
| 8 |
def load_model():
|
| 9 |
model_path = "skin_disease_model_jit.pt"
|
| 10 |
labels_path = "labels.txt"
|
| 11 |
+
|
| 12 |
if not os.path.exists(model_path):
|
| 13 |
raise FileNotFoundError(f"Модель не найдена: {model_path}")
|
| 14 |
if not os.path.exists(labels_path):
|
| 15 |
raise FileNotFoundError("Файл labels.txt не найден.")
|
| 16 |
+
|
| 17 |
model = torch.jit.load(model_path, map_location=torch.device('cpu'))
|
| 18 |
model.eval()
|
| 19 |
+
|
| 20 |
with open(labels_path, "r") as f:
|
| 21 |
labels = [line.strip() for line in f.readlines()]
|
| 22 |
+
|
| 23 |
return model, labels
|
| 24 |
|
| 25 |
model, labels = load_model()
|
| 26 |
|
| 27 |
+
# Предобработка входного изображения
|
| 28 |
preprocess = transforms.Compose([
|
| 29 |
transforms.Resize((224, 224)),
|
| 30 |
transforms.ToTensor(),
|
| 31 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
| 32 |
+
std=[0.229, 0.224, 0.225]),
|
| 33 |
])
|
| 34 |
|
| 35 |
# Функция предсказания
|
|
|
|
| 40 |
with torch.no_grad():
|
| 41 |
output = model(image_tensor)
|
| 42 |
scores = torch.nn.functional.softmax(output[0], dim=0)
|
| 43 |
+
|
| 44 |
return {label: float(score) for label, score in zip(labels, scores)}
|
| 45 |
|
| 46 |
+
# Параметры интерфейса
|
| 47 |
+
title = "Skin-AI"
|
| 48 |
+
description = "Sube una imagen"
|
| 49 |
+
|
| 50 |
+
# Создаём интерфейс в стиле, максимально похожем на Spaces
|
| 51 |
+
interface = gr.Interface(
|
| 52 |
+
fn=predict,
|
| 53 |
+
inputs=gr.Image(type="pil", label="Imagen de entrada"),
|
| 54 |
+
outputs=gr.Label(num_top_classes=3, label="Predicciones"),
|
| 55 |
+
title=title,
|
| 56 |
+
description=description,
|
| 57 |
+
theme=gr.themes.Soft(primary_hue="purple") # «Фиолетовая» тема, как на скриншоте
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# Запуск приложения
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
if __name__ == "__main__":
|
| 62 |
+
interface.launch(server_name="0.0.0.0", server_port=7860)
|