Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import numpy as np
|
|
| 6 |
import onnxruntime as rt
|
| 7 |
import pandas as pd
|
| 8 |
from PIL import Image
|
| 9 |
-
from huggingface_hub import whoami
|
| 10 |
|
| 11 |
from translator import translate_texts
|
| 12 |
|
|
@@ -18,7 +18,6 @@ MODEL_FILENAME = "model.onnx"
|
|
| 18 |
LABEL_FILENAME = "selected_tags.csv"
|
| 19 |
|
| 20 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 21 |
-
# 新增:从环境变量读取访问密码
|
| 22 |
ACCESS_PASSWORD = os.environ.get("ACCESS_PASSWORD")
|
| 23 |
|
| 24 |
# ------------------------------------------------------------------
|
|
@@ -146,19 +145,13 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=cus
|
|
| 146 |
gr.Markdown("# 🖼️ AI 图像标签分析器")
|
| 147 |
gr.Markdown("上传图片自动识别标签,支持中英文显示和一键复制。[NovelAI在线绘画](https://nai.idlecloud.cc/)")
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
</div>
|
| 156 |
-
"""
|
| 157 |
-
)
|
| 158 |
-
gr.LoginButton(value="🤗 通过 Hugging Face 登录")
|
| 159 |
|
| 160 |
-
user_status_md = gr.Markdown(visible=False)
|
| 161 |
-
|
| 162 |
state_res = gr.State({})
|
| 163 |
state_translations_dict = gr.State({})
|
| 164 |
|
|
@@ -206,12 +199,33 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=cus
|
|
| 206 |
if token:
|
| 207 |
try:
|
| 208 |
user_info = whoami(token=token)
|
| 209 |
-
welcome_msg = f"
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
except Exception as e:
|
| 212 |
print(f"Token 无效或已过期: {e}")
|
| 213 |
-
|
| 214 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
def format_tags_html(tags_dict, translations_list, show_scores):
|
| 217 |
if not tags_dict: return "<p>暂无标签</p>"
|
|
@@ -296,7 +310,12 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=cus
|
|
| 296 |
raise gr.Error(f"处理时发生错误: {e}")
|
| 297 |
|
| 298 |
# ----------------- 绑定事件 -----------------
|
| 299 |
-
demo.load(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
|
| 301 |
btn.click(
|
| 302 |
process_image_and_generate_outputs,
|
|
|
|
| 6 |
import onnxruntime as rt
|
| 7 |
import pandas as pd
|
| 8 |
from PIL import Image
|
| 9 |
+
from huggingface_hub import whoami
|
| 10 |
|
| 11 |
from translator import translate_texts
|
| 12 |
|
|
|
|
| 18 |
LABEL_FILENAME = "selected_tags.csv"
|
| 19 |
|
| 20 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
|
|
| 21 |
ACCESS_PASSWORD = os.environ.get("ACCESS_PASSWORD")
|
| 22 |
|
| 23 |
# ------------------------------------------------------------------
|
|
|
|
| 145 |
gr.Markdown("# 🖼️ AI 图像标签分析器")
|
| 146 |
gr.Markdown("上传图片自动识别标签,支持中英文显示和一键复制。[NovelAI在线绘画](https://nai.idlecloud.cc/)")
|
| 147 |
|
| 148 |
+
# 统一的状态和登录/登出控制区域
|
| 149 |
+
with gr.Row():
|
| 150 |
+
user_status_html = gr.HTML("<p>ℹ️ 正在检查登录状态...</p>")
|
| 151 |
+
with gr.Row():
|
| 152 |
+
login_button = gr.LoginButton(value="🤗 通过 Hugging Face 登录", visible=True)
|
| 153 |
+
logout_button = gr.LogoutButton(value="退出登录", visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
|
|
|
|
|
|
| 155 |
state_res = gr.State({})
|
| 156 |
state_translations_dict = gr.State({})
|
| 157 |
|
|
|
|
| 199 |
if token:
|
| 200 |
try:
|
| 201 |
user_info = whoami(token=token)
|
| 202 |
+
welcome_msg = f"<p style='color:green;font-weight:bold;'>✅ 您好, {user_info.get('fullname', user_info.get('name'))}!欢迎使用。</p>"
|
| 203 |
+
# 已登录:显示欢迎信息,隐藏登录按钮,显示登出按钮,显示主界面
|
| 204 |
+
return (
|
| 205 |
+
gr.update(value=welcome_msg),
|
| 206 |
+
gr.update(visible=False),
|
| 207 |
+
gr.update(visible=True),
|
| 208 |
+
gr.update(visible=True)
|
| 209 |
+
)
|
| 210 |
except Exception as e:
|
| 211 |
print(f"Token 无效或已过期: {e}")
|
| 212 |
+
error_msg = "<p style='color:red;'>🚫 登录令牌无效或已过期,请重新登录。</p>"
|
| 213 |
+
# 令牌无效:显示错误,显示登录按钮,隐藏登出按钮,隐藏主界面
|
| 214 |
+
return (
|
| 215 |
+
gr.update(value=error_msg),
|
| 216 |
+
gr.update(visible=True),
|
| 217 |
+
gr.update(visible=False),
|
| 218 |
+
gr.update(visible=False)
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
# 未登录
|
| 222 |
+
info_msg = "<p style='color:#d46b08;'>🚫 您需要登录才能使用此应用。</p>"
|
| 223 |
+
return (
|
| 224 |
+
gr.update(value=info_msg),
|
| 225 |
+
gr.update(visible=True),
|
| 226 |
+
gr.update(visible=False),
|
| 227 |
+
gr.update(visible=False)
|
| 228 |
+
)
|
| 229 |
|
| 230 |
def format_tags_html(tags_dict, translations_list, show_scores):
|
| 231 |
if not tags_dict: return "<p>暂无标签</p>"
|
|
|
|
| 310 |
raise gr.Error(f"处理时发生错误: {e}")
|
| 311 |
|
| 312 |
# ----------------- 绑定事件 -----------------
|
| 313 |
+
demo.load(
|
| 314 |
+
fn=check_user_status,
|
| 315 |
+
inputs=None,
|
| 316 |
+
outputs=[user_status_html, login_button, logout_button, main_interface],
|
| 317 |
+
queue=False
|
| 318 |
+
)
|
| 319 |
|
| 320 |
btn.click(
|
| 321 |
process_image_and_generate_outputs,
|