IdlecloudX commited on
Commit
356be23
·
verified ·
1 Parent(s): 2c08d36

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -19
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, get_space_runtime
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
- with gr.Group(visible=True) as login_view:
150
- gr.Markdown(
151
- """
152
- <div style="padding: 20px; text-align: center; background-color: #fffbe6; border: 1px solid #ffe58f; border-radius: 8px;">
153
- <p style="font-size: 1.1em; color: #d46b08;">🚫 <strong>您需要登录才能使用此应用</strong></p>
154
- <p style="color: #666;">请点击下方的按钮通过 Hugging Face 登录。登录后,请刷新此页面。</p>
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" 您好, **{user_info.get('fullname', user_info.get('name'))}**!欢迎使用本应用。"
210
- return gr.update(visible=False), gr.update(value=welcome_msg, visible=True), gr.update(visible=True)
 
 
 
 
 
 
211
  except Exception as e:
212
  print(f"Token 无效或已过期: {e}")
213
-
214
- return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(fn=check_user_status, inputs=None, outputs=[login_view, user_status_md, main_interface], queue=False)
 
 
 
 
 
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,