Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,15 +8,7 @@ import pandas as pd
|
|
| 8 |
from PIL import Image
|
| 9 |
from huggingface_hub import login
|
| 10 |
|
| 11 |
-
|
| 12 |
-
# from translator import translate_texts
|
| 13 |
-
# Mock translator for a standalone example if translator.py is not available
|
| 14 |
-
def translate_texts(texts, src_lang="auto", tgt_lang="zh"):
|
| 15 |
-
print(f"Mock translating: {texts} from {src_lang} to {tgt_lang}")
|
| 16 |
-
if not texts:
|
| 17 |
-
return []
|
| 18 |
-
# 返回一个简单的模拟翻译结果,实际使用时请确保 translator.py 可用且功能正确
|
| 19 |
-
return [f"{text}_译" for text in texts]
|
| 20 |
|
| 21 |
# ------------------------------------------------------------------
|
| 22 |
# 模型配置
|
|
@@ -64,7 +56,6 @@ class Tagger:
|
|
| 64 |
print("✅ 模型和标签加载成功")
|
| 65 |
except Exception as e:
|
| 66 |
print(f"❌ 模型或标签加载失败: {e}")
|
| 67 |
-
# 可以选择抛出异常或设置一个标志,让应用知道模型未就绪
|
| 68 |
raise RuntimeError(f"模型初始化失败: {e}")
|
| 69 |
|
| 70 |
|
|
@@ -263,9 +254,6 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=cus
|
|
| 263 |
return "<p>暂无标签</p>"
|
| 264 |
|
| 265 |
html = '<div class="label-container">'
|
| 266 |
-
# Ensure translations_list is a list and matches length, or provide empty strings if not.
|
| 267 |
-
# This assumes translations_list corresponds to the order in tags_dict.keys()
|
| 268 |
-
# For dictionaries, keys() order is insertion order from Python 3.7+
|
| 269 |
|
| 270 |
if not isinstance(translations_list, list): # defensive check
|
| 271 |
translations_list = []
|
|
@@ -369,11 +357,6 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=cus
|
|
| 369 |
# The predict method now returns res and tag_categories_for_translation
|
| 370 |
res, tag_categories_original_order = tagger_instance.predict(img, g_th, c_th)
|
| 371 |
|
| 372 |
-
# 2. Translate all tags that will be displayed in lists
|
| 373 |
-
# The `show_zh_in_list_checkbox` now controls if we translate for lists.
|
| 374 |
-
# For summary, translation is controlled by `s_zh_in_sum`.
|
| 375 |
-
# We should always translate all potential tags to have them ready.
|
| 376 |
-
|
| 377 |
all_tags_to_translate = []
|
| 378 |
for cat_key in ["general", "characters", "ratings"]:
|
| 379 |
all_tags_to_translate.extend(tag_categories_original_order.get(cat_key, []))
|
|
@@ -392,17 +375,11 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=cus
|
|
| 392 |
offset += num_tags_in_cat
|
| 393 |
else:
|
| 394 |
current_translations_dict[cat_key] = []
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
# 3. Format HTML outputs (always show English, translations if available and `show_zh_in_list` is true)
|
| 398 |
-
# Let's assume `show_zh_in_list` is a new checkbox or fixed to true for list display.
|
| 399 |
-
# For simplicity, let's assume list translations are always prepared if `current_translations_dict` has them.
|
| 400 |
|
| 401 |
general_html = format_tags_html(res.get("general", {}), current_translations_dict.get("general", []), "general", s_scores, True)
|
| 402 |
char_html = format_tags_html(res.get("characters", {}), current_translations_dict.get("characters", []), "characters", s_scores, True)
|
| 403 |
rating_html = format_tags_html(res.get("ratings", {}), current_translations_dict.get("ratings", []), "ratings", s_scores, True)
|
| 404 |
|
| 405 |
-
# 4. Generate initial summary text (based on current summary settings from UI)
|
| 406 |
summary_text = generate_summary_text_content(
|
| 407 |
res, current_translations_dict,
|
| 408 |
s_gen, s_char, s_rat, s_sep, s_zh_in_sum # Use summary specific checkbox for zh
|
|
@@ -472,15 +449,6 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=cus
|
|
| 472 |
# show_progress=False # Typically fast, no need for progress indicator
|
| 473 |
)
|
| 474 |
|
| 475 |
-
# If tag score display in lists is changed, re-render HTMLs
|
| 476 |
-
# This requires storing the raw data or re-processing parts of it.
|
| 477 |
-
# For simplicity, we can make the list HTML generation also dependent on state if needed,
|
| 478 |
-
# or re-trigger a lighter version of 'process' that only updates HTML.
|
| 479 |
-
# Current implementation: score display is set at 'analyze' time.
|
| 480 |
-
# To make 'show_tag_scores' dynamic for lists *after* analysis without re-analyzing:
|
| 481 |
-
# We would need a new callback that re-runs `format_tags_html` for each category
|
| 482 |
-
# using data from `state_res` and `state_translations_dict`.
|
| 483 |
-
|
| 484 |
# ------------------------------------------------------------------
|
| 485 |
# 启动
|
| 486 |
# ------------------------------------------------------------------
|
|
|
|
| 8 |
from PIL import Image
|
| 9 |
from huggingface_hub import login
|
| 10 |
|
| 11 |
+
from translator import translate_texts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# ------------------------------------------------------------------
|
| 14 |
# 模型配置
|
|
|
|
| 56 |
print("✅ 模型和标签加载成功")
|
| 57 |
except Exception as e:
|
| 58 |
print(f"❌ 模型或标签加载失败: {e}")
|
|
|
|
| 59 |
raise RuntimeError(f"模型初始化失败: {e}")
|
| 60 |
|
| 61 |
|
|
|
|
| 254 |
return "<p>暂无标签</p>"
|
| 255 |
|
| 256 |
html = '<div class="label-container">'
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
if not isinstance(translations_list, list): # defensive check
|
| 259 |
translations_list = []
|
|
|
|
| 357 |
# The predict method now returns res and tag_categories_for_translation
|
| 358 |
res, tag_categories_original_order = tagger_instance.predict(img, g_th, c_th)
|
| 359 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
all_tags_to_translate = []
|
| 361 |
for cat_key in ["general", "characters", "ratings"]:
|
| 362 |
all_tags_to_translate.extend(tag_categories_original_order.get(cat_key, []))
|
|
|
|
| 375 |
offset += num_tags_in_cat
|
| 376 |
else:
|
| 377 |
current_translations_dict[cat_key] = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 378 |
|
| 379 |
general_html = format_tags_html(res.get("general", {}), current_translations_dict.get("general", []), "general", s_scores, True)
|
| 380 |
char_html = format_tags_html(res.get("characters", {}), current_translations_dict.get("characters", []), "characters", s_scores, True)
|
| 381 |
rating_html = format_tags_html(res.get("ratings", {}), current_translations_dict.get("ratings", []), "ratings", s_scores, True)
|
| 382 |
|
|
|
|
| 383 |
summary_text = generate_summary_text_content(
|
| 384 |
res, current_translations_dict,
|
| 385 |
s_gen, s_char, s_rat, s_sep, s_zh_in_sum # Use summary specific checkbox for zh
|
|
|
|
| 449 |
# show_progress=False # Typically fast, no need for progress indicator
|
| 450 |
)
|
| 451 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 452 |
# ------------------------------------------------------------------
|
| 453 |
# 启动
|
| 454 |
# ------------------------------------------------------------------
|