Spaces:
Sleeping
Sleeping
update interface in app.py
Browse files
app.py
CHANGED
|
@@ -8,25 +8,27 @@ 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 |
},
|
| 18 |
"en": {
|
| 19 |
-
"title": "Cat Detector 🐈
|
| 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 |
-
"
|
| 24 |
-
"
|
| 25 |
}
|
| 26 |
}
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Преобразование изображения
|
| 32 |
image = image.resize((224, 224))
|
|
@@ -38,54 +40,62 @@ def predict(image, lang):
|
|
| 38 |
confidence = f"{100*(1-pred if pred < 0.5 else pred):.1f}"
|
| 39 |
|
| 40 |
if pred < 0.5:
|
| 41 |
-
return translations[
|
| 42 |
else:
|
| 43 |
-
return translations[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
|
|
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
label="Язык / Language"
|
| 51 |
-
)
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
title = gr.Markdown("#
|
| 55 |
-
description = gr.Markdown(translations[
|
| 56 |
|
| 57 |
with gr.Row():
|
| 58 |
image_input = gr.Image(type="pil")
|
| 59 |
output = gr.Textbox()
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
gr.Examples(
|
| 63 |
examples=["cat.jpg", "not_cat.jpg"],
|
| 64 |
inputs=image_input,
|
| 65 |
-
label=
|
| 66 |
-
)
|
| 67 |
-
|
| 68 |
-
btn = gr.Button(translations["ru"]["button_text"])
|
| 69 |
-
btn.click(
|
| 70 |
-
fn=predict,
|
| 71 |
-
inputs=[image_input, language],
|
| 72 |
-
outputs=output
|
| 73 |
)
|
| 74 |
|
| 75 |
-
# Об
|
| 76 |
-
def update_interface(
|
| 77 |
-
lang_code = "ru" if lang == "Русский (ru)" else "en"
|
| 78 |
return {
|
| 79 |
-
title: "#
|
| 80 |
-
description: translations[
|
| 81 |
-
|
| 82 |
-
|
| 83 |
}
|
| 84 |
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
fn=update_interface,
|
| 87 |
-
|
| 88 |
-
outputs=[title, description, examples_label, btn]
|
| 89 |
)
|
| 90 |
|
| 91 |
demo.launch()
|
|
|
|
| 8 |
# Словарь переводов
|
| 9 |
translations = {
|
| 10 |
"ru": {
|
| 11 |
+
"title": "Определитель котов 🐱",
|
| 12 |
"description": "Загрузите фото, и модель определит, кот это или нет!",
|
| 13 |
"cat_result": "Кот! 😻 (Уверенность: {confidence}%)",
|
| 14 |
"not_cat_result": "Не кот! 😾 (Уверенность: {confidence}%)",
|
| 15 |
+
"button_text": "Определить",
|
| 16 |
+
"switch_lang": "EN 🇺🇸" # Текст кнопки переключения
|
| 17 |
},
|
| 18 |
"en": {
|
| 19 |
+
"title": "Cat Detector 🐈",
|
| 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 |
+
"button_text": "Detect",
|
| 24 |
+
"switch_lang": "RU 🇷🇺" # Текст кнопки переключения
|
| 25 |
}
|
| 26 |
}
|
| 27 |
|
| 28 |
+
current_lang = "ru" # Текущий язык по умолчанию
|
| 29 |
+
|
| 30 |
+
def predict(image):
|
| 31 |
+
global current_lang
|
| 32 |
|
| 33 |
# Преобразование изображения
|
| 34 |
image = image.resize((224, 224))
|
|
|
|
| 40 |
confidence = f"{100*(1-pred if pred < 0.5 else pred):.1f}"
|
| 41 |
|
| 42 |
if pred < 0.5:
|
| 43 |
+
return translations[current_lang]["cat_result"].format(confidence=confidence)
|
| 44 |
else:
|
| 45 |
+
return translations[current_lang]["not_cat_result"].format(confidence=confidence)
|
| 46 |
+
|
| 47 |
+
def switch_language():
|
| 48 |
+
global current_lang
|
| 49 |
+
current_lang = "en" if current_lang == "ru" else "ru"
|
| 50 |
+
return translations[current_lang]["switch_lang"]
|
| 51 |
|
| 52 |
+
# Создаем интерфейс
|
| 53 |
with gr.Blocks() as demo:
|
| 54 |
+
# Кнопка переключения языка в правом верхнем углу
|
| 55 |
+
with gr.Row():
|
| 56 |
+
gr.Markdown("") # Пустой элемент для выравнивания
|
| 57 |
+
lang_btn = gr.Button(translations["ru"]["switch_lang"], size="sm")
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
# Основной интерфейс
|
| 60 |
+
title = gr.Markdown(f"## {translations[current_lang]['title']}")
|
| 61 |
+
description = gr.Markdown(translations[current_lang]["description"])
|
| 62 |
|
| 63 |
with gr.Row():
|
| 64 |
image_input = gr.Image(type="pil")
|
| 65 |
output = gr.Textbox()
|
| 66 |
|
| 67 |
+
btn = gr.Button(translations[current_lang]["button_text"])
|
| 68 |
+
btn.click(fn=predict, inputs=image_input, outputs=output)
|
| 69 |
+
|
| 70 |
+
# Примеры изображений
|
| 71 |
gr.Examples(
|
| 72 |
examples=["cat.jpg", "not_cat.jpg"],
|
| 73 |
inputs=image_input,
|
| 74 |
+
label="Примеры:" if current_lang == "ru" else "Examples:"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
)
|
| 76 |
|
| 77 |
+
# Обработка переключения языка
|
| 78 |
+
def update_interface():
|
|
|
|
| 79 |
return {
|
| 80 |
+
title: f"## {translations[current_lang]['title']}",
|
| 81 |
+
description: translations[current_lang]["description"],
|
| 82 |
+
btn: translations[current_lang]["button_text"],
|
| 83 |
+
lang_btn: translations[current_lang]["switch_lang"]
|
| 84 |
}
|
| 85 |
|
| 86 |
+
lang_btn.click(
|
| 87 |
+
fn=switch_language,
|
| 88 |
+
outputs=lang_btn,
|
| 89 |
+
_js="""
|
| 90 |
+
function() {
|
| 91 |
+
// Принудительное обновление интерфейса
|
| 92 |
+
setTimeout(() => window.dispatchEvent(new Event('resize')), 100);
|
| 93 |
+
return arguments[0];
|
| 94 |
+
}
|
| 95 |
+
"""
|
| 96 |
+
).then(
|
| 97 |
fn=update_interface,
|
| 98 |
+
outputs=[title, description, btn, lang_btn]
|
|
|
|
| 99 |
)
|
| 100 |
|
| 101 |
demo.launch()
|