Spaces:
Sleeping
Sleeping
Download app.py from AnatarG/CatDetector: direct link, hf CLI and curl.
- Browser
- Download file 3.78 kB
-
https://huggingface.co/spaces/AnatarG/CatDetector/resolve/main/app.py
- Command line
-
hf download hf://spaces/AnatarG/CatDetector/app.py
-
curl -L -o app.py https://huggingface.co/spaces/AnatarG/CatDetector/resolve/main/app.py
3.78 kB
| import gradio as gr | |
| import tensorflow as tf | |
| import numpy as np | |
| # Загрузка модели | |
| model = tf.keras.models.load_model("is_cat.keras") | |
| # Словарь переводов | |
| translations = { | |
| "ru": { | |
| "title": "Определитель котов 🐈 vs 🚫🐈", | |
| "description": "Модель определяет только явных котов (порог 70%)", | |
| "cat_result": "Точно кот! 😻 (Уверенность: {confidence}%)", | |
| "not_sure_result": "Не уверен 🤔 (Уверенность: {confidence}%)", | |
| "not_cat_result": "Точно не кот! 😾 (Уверенность: {confidence}%)", | |
| "examples_label": "Примеры:", | |
| "button_text": "Определить" | |
| }, | |
| "en": { | |
| "title": "Cat Detector 🐈 vs 🚫🐈", | |
| "description": "Model detects only obvious cats (70% threshold)", | |
| "cat_result": "Definitely cat! 😻 (Confidence: {confidence}%)", | |
| "not_sure_result": "Not sure 🤔 (Confidence: {confidence}%)", | |
| "not_cat_result": "Definitely not cat! 😾 (Confidence: {confidence}%)", | |
| "examples_label": "Examples:", | |
| "button_text": "Detect" | |
| } | |
| } | |
| def predict(image, lang): | |
| lang_code = "ru" if lang == "Русский (ru)" else "en" | |
| # Преобразование изображения | |
| image = image.resize((224, 224)) | |
| image = np.array(image) / 255.0 | |
| image = np.expand_dims(image, axis=0) | |
| # Предсказание с новым порогом | |
| pred = model.predict(image)[0][0] | |
| confidence = f"{100*(1-pred if pred < 0.7 else pred):.1f}" | |
| if pred < 0.3: # Точно кот (70% уверенности что это кот) | |
| return translations[lang_code]["cat_result"].format(confidence=confidence) | |
| elif pred < 0.7: # Не уверен (30-70%) | |
| return translations[lang_code]["not_sure_result"].format(confidence=confidence) | |
| else: # Точно не кот (>70% уверенности что не кот) | |
| return translations[lang_code]["not_cat_result"].format(confidence=confidence) | |
| with gr.Blocks() as demo: | |
| # Переключатель языка | |
| language = gr.Radio( | |
| choices=["Русский (ru)", "English (en)"], | |
| value="Русский (ru)", | |
| label="Язык / Language" | |
| ) | |
| # Динамические элементы | |
| title = gr.Markdown("# " + translations["ru"]["title"]) | |
| description = gr.Markdown(translations["ru"]["description"]) | |
| examples_label = gr.Markdown("### " + translations["ru"]["examples_label"]) | |
| with gr.Row(): | |
| image_input = gr.Image(type="pil") | |
| output = gr.Textbox() | |
| # Примеры (без привязки к изменению языка) | |
| gr.Examples( | |
| examples=["cat.jpg", "not_cat.jpg"], | |
| inputs=image_input, | |
| label=None # Убрали label, так как используем отдельный Markdown | |
| ) | |
| btn = gr.Button(translations["ru"]["button_text"]) | |
| btn.click( | |
| fn=predict, | |
| inputs=[image_input, language], | |
| outputs=output | |
| ) | |
| # Обновление интерфейса | |
| def update_interface(lang): | |
| lang_code = "ru" if lang == "Русский (ru)" else "en" | |
| return { | |
| title: "# " + translations[lang_code]["title"], | |
| description: translations[lang_code]["description"], | |
| examples_label: "### " + translations[lang_code]["examples_label"], | |
| btn: translations[lang_code]["button_text"] | |
| } | |
| language.change( | |
| fn=update_interface, | |
| inputs=language, | |
| outputs=[title, description, examples_label, btn] | |
| ) | |
| demo.launch() |