AnatarG commited on
Commit
8d4979a
·
verified ·
1 Parent(s): 1bb813b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -48
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
- "button_text": "Определить",
16
- "switch_lang": "EN 🇺🇸",
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
- "button_text": "Detect",
25
- "switch_lang": "RU 🇷🇺",
26
- "examples_label": "Examples:"
27
  }
28
  }
29
 
30
- current_lang = "ru" # Текущий язык по умолчанию
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[current_lang]["cat_result"].format(confidence=confidence)
46
  else:
47
- return translations[current_lang]["not_cat_result"].format(confidence=confidence)
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
- with gr.Row():
67
- gr.Markdown("") # Пустой элемент для выравнивания
68
- lang_btn = gr.Button(translations[current_lang]["switch_lang"], size="sm")
 
 
69
 
70
- # Основной интерфейс
71
- title = gr.Markdown(f"## {translations[current_lang]['title']}")
72
- description = gr.Markdown(translations[current_lang]["description"])
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
- lang_btn.click(
91
- fn=switch_language,
92
- outputs=lang_btn
93
- ).then(
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  fn=update_interface,
95
- outputs=[title, description, btn, lang_btn, examples_label]
 
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()