Spaces:
Sleeping
Sleeping
GoofyGoof
commited on
Commit
·
ae78290
1
Parent(s):
8f3ece0
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,53 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import hf_hub_download
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
import gradio as gr
|
5 |
|
6 |
+
# Загрузка файла модели из Hugging Face Hub
|
7 |
+
model_path = hf_hub_download(repo_id="neuronetties/wine", filename="wine_model.keras")
|
8 |
|
9 |
+
# Загрузка модели
|
10 |
+
model = tf.keras.models.load_model(model_path)
|
11 |
+
|
12 |
+
# Функция предсказания
|
13 |
+
def predict(data_row):
|
14 |
+
try:
|
15 |
+
# Преобразуем данные в массив float
|
16 |
+
input_data = np.array(data_row, dtype=float).reshape(1, -1)
|
17 |
+
|
18 |
+
# Предсказания модели
|
19 |
+
predictions = model.predict(input_data)
|
20 |
+
|
21 |
+
# Находим индекс класса с наибольшей вероятностью
|
22 |
+
primary_class_index = predictions[0].argmax() # Основной класс
|
23 |
+
predicted_class = f"№{primary_class_index + 1}" # Первый предсказанный класс
|
24 |
+
|
25 |
+
# Находим второй предсказанный сорт (второй по вероятности)
|
26 |
+
sorted_indices = predictions[0].argsort()
|
27 |
+
second_class_index = sorted_indices[-2]
|
28 |
+
second_class_probability = predictions[0][second_class_index]
|
29 |
+
|
30 |
+
# Если вероятность второго класса больше 0.1, добавляем его
|
31 |
+
if second_class_probability > 0.1:
|
32 |
+
predicted_class += f" (or №{second_class_index + 1})"
|
33 |
+
|
34 |
+
return predicted_class
|
35 |
+
except ValueError:
|
36 |
+
return "Пожалуйста, убедитесь, что все значения числовые."
|
37 |
+
|
38 |
+
# Gradio интерфейс
|
39 |
+
interface = gr.Interface(
|
40 |
+
fn=predict,
|
41 |
+
inputs=gr.Dataframe(
|
42 |
+
headers=["alcohol", "malic_acid", "ash", "alcalinity_of_ash", "magnesium",
|
43 |
+
"total_phenols", "flavanoids", "nonflavanoid_phenols", "proanthocyanins",
|
44 |
+
"color_intensity", "hue", "od280/od315_of_diluted_wines", "proline"],
|
45 |
+
row_count=1, col_count=13,
|
46 |
+
type="array" # Гарантирует получение данных в виде массива
|
47 |
+
),
|
48 |
+
outputs=gr.Textbox(label="Predicted Class"), # Вывод через Textbox
|
49 |
+
description="Введите данные (13 признаков) для предсказания сорта вина"
|
50 |
+
)
|
51 |
+
|
52 |
+
# Запуск приложения
|
53 |
+
interface.launch()
|