Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,29 +8,25 @@ model = tf.keras.models.load_model("is_cat.keras")
|
|
| 8 |
# Словарь переводов
|
| 9 |
translations = {
|
| 10 |
"ru": {
|
| 11 |
-
"title": "Определитель котов
|
| 12 |
"description": "Загрузите фото, и модель определит, кот это или нет!",
|
| 13 |
"cat_result": "Кот! 😻 (Уверенность: {confidence}%)",
|
| 14 |
"not_cat_result": "Не кот! 😾 (Уверенность: {confidence}%)",
|
| 15 |
-
"
|
| 16 |
-
"
|
| 17 |
-
"examples_label": "Примеры:"
|
| 18 |
},
|
| 19 |
"en": {
|
| 20 |
-
"title": "Cat Detector 🐈",
|
| 21 |
"description": "Upload a photo to check if it's a cat!",
|
| 22 |
"cat_result": "Cat! 😻 (Confidence: {confidence}%)",
|
| 23 |
"not_cat_result": "Not a cat! 😾 (Confidence: {confidence}%)",
|
| 24 |
-
"
|
| 25 |
-
"
|
| 26 |
-
"examples_label": "Examples:"
|
| 27 |
}
|
| 28 |
}
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
def predict(image):
|
| 33 |
-
global current_lang
|
| 34 |
|
| 35 |
# Преобразование изображения
|
| 36 |
image = image.resize((224, 224))
|
|
@@ -42,57 +38,53 @@ def predict(image):
|
|
| 42 |
confidence = f"{100*(1-pred if pred < 0.5 else pred):.1f}"
|
| 43 |
|
| 44 |
if pred < 0.5:
|
| 45 |
-
return translations[
|
| 46 |
else:
|
| 47 |
-
return translations[
|
| 48 |
-
|
| 49 |
-
def switch_language():
|
| 50 |
-
global current_lang
|
| 51 |
-
current_lang = "en" if current_lang == "ru" else "ru"
|
| 52 |
-
return translations[current_lang]["switch_lang"]
|
| 53 |
|
| 54 |
-
def update_interface():
|
| 55 |
-
return {
|
| 56 |
-
title: f"## {translations[current_lang]['title']}",
|
| 57 |
-
description: translations[current_lang]["description"],
|
| 58 |
-
btn: translations[current_lang]["button_text"],
|
| 59 |
-
lang_btn: translations[current_lang]["switch_lang"],
|
| 60 |
-
examples_label: translations[current_lang]["examples_label"]
|
| 61 |
-
}
|
| 62 |
-
|
| 63 |
-
# Создаем интерфейс
|
| 64 |
with gr.Blocks() as demo:
|
| 65 |
-
#
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
title = gr.Markdown(
|
| 72 |
-
description = gr.Markdown(translations[
|
| 73 |
|
| 74 |
with gr.Row():
|
| 75 |
image_input = gr.Image(type="pil")
|
| 76 |
output = gr.Textbox()
|
| 77 |
|
| 78 |
-
btn = gr.Button(translations[current_lang]["button_text"])
|
| 79 |
-
btn.click(fn=predict, inputs=image_input, outputs=output)
|
| 80 |
-
|
| 81 |
-
# Примеры изображений
|
| 82 |
-
examples_label = gr.Label(translations[current_lang]["examples_label"])
|
| 83 |
gr.Examples(
|
| 84 |
examples=["cat.jpg", "not_cat.jpg"],
|
| 85 |
inputs=image_input,
|
| 86 |
-
label=examples_label
|
| 87 |
)
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
fn=
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
fn=update_interface,
|
| 95 |
-
|
|
|
|
| 96 |
)
|
| 97 |
|
| 98 |
demo.launch()
|
|
|
|
| 8 |
# Словарь переводов
|
| 9 |
translations = {
|
| 10 |
"ru": {
|
| 11 |
+
"title": "Определитель котов 🐈 vs 🚫🐈",
|
| 12 |
"description": "Загрузите фото, и модель определит, кот это или нет!",
|
| 13 |
"cat_result": "Кот! 😻 (Уверенность: {confidence}%)",
|
| 14 |
"not_cat_result": "Не кот! 😾 (Уверенность: {confidence}%)",
|
| 15 |
+
"examples_label": "Примеры:",
|
| 16 |
+
"button_text": "Определить"
|
|
|
|
| 17 |
},
|
| 18 |
"en": {
|
| 19 |
+
"title": "Cat Detector 🐈 vs 🚫🐈",
|
| 20 |
"description": "Upload a photo to check if it's a cat!",
|
| 21 |
"cat_result": "Cat! 😻 (Confidence: {confidence}%)",
|
| 22 |
"not_cat_result": "Not a cat! 😾 (Confidence: {confidence}%)",
|
| 23 |
+
"examples_label": "Examples:",
|
| 24 |
+
"button_text": "Detect"
|
|
|
|
| 25 |
}
|
| 26 |
}
|
| 27 |
|
| 28 |
+
def predict(image, lang):
|
| 29 |
+
lang_code = "ru" if lang == "Русский (ru)" else "en"
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Преобразование изображения
|
| 32 |
image = image.resize((224, 224))
|
|
|
|
| 38 |
confidence = f"{100*(1-pred if pred < 0.5 else pred):.1f}"
|
| 39 |
|
| 40 |
if pred < 0.5:
|
| 41 |
+
return translations[lang_code]["cat_result"].format(confidence=confidence)
|
| 42 |
else:
|
| 43 |
+
return translations[lang_code]["not_cat_result"].format(confidence=confidence)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
+
# Переключатель языка
|
| 47 |
+
language = gr.Radio(
|
| 48 |
+
choices=["Русский (ru)", "English (en)"],
|
| 49 |
+
value="Русский (ru)",
|
| 50 |
+
label="Язык / Language"
|
| 51 |
+
)
|
| 52 |
|
| 53 |
+
# Динамические элементы
|
| 54 |
+
title = gr.Markdown("# " + translations["ru"]["title"])
|
| 55 |
+
description = gr.Markdown(translations["ru"]["description"])
|
| 56 |
|
| 57 |
with gr.Row():
|
| 58 |
image_input = gr.Image(type="pil")
|
| 59 |
output = gr.Textbox()
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
gr.Examples(
|
| 62 |
examples=["cat.jpg", "not_cat.jpg"],
|
| 63 |
inputs=image_input,
|
| 64 |
+
label=gr.Label(translations["ru"]["examples_label"])
|
| 65 |
)
|
| 66 |
|
| 67 |
+
btn = gr.Button(translations["ru"]["button_text"])
|
| 68 |
+
btn.click(
|
| 69 |
+
fn=predict,
|
| 70 |
+
inputs=[image_input, language],
|
| 71 |
+
outputs=output
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
# Обновление интерфейса
|
| 75 |
+
def update_interface(lang):
|
| 76 |
+
lang_code = "ru" if lang == "Русский (ru)" else "en"
|
| 77 |
+
return {
|
| 78 |
+
title: "# " + translations[lang_code]["title"],
|
| 79 |
+
description: translations[lang_code]["description"],
|
| 80 |
+
examples_label: translations[lang_code]["examples_label"],
|
| 81 |
+
btn: translations[lang_code]["button_text"]
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
language.change(
|
| 85 |
fn=update_interface,
|
| 86 |
+
inputs=language,
|
| 87 |
+
outputs=[title, description, examples_label, btn]
|
| 88 |
)
|
| 89 |
|
| 90 |
demo.launch()
|