IdlecloudX commited on
Commit
7f38460
·
verified ·
1 Parent(s): 20d3044

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -8
app.py CHANGED
@@ -6,7 +6,8 @@ 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
 
@@ -183,22 +184,33 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=cus
183
  with gr.TabItem("⭐ 评分标签"): out_rating = gr.HTML(label="Rating Tags")
184
  gr.Markdown("### 标签汇总结果")
185
  out_summary = gr.Textbox(label="标签汇总", lines=5, show_copy_button=True)
186
-
187
  def get_token_from_request(request: gr.Request) -> str | None:
188
- """Helper function to extract token from request headers."""
189
  auth_header = request.headers.get("authorization")
190
  if auth_header and auth_header.startswith("Bearer "):
191
  return auth_header.split(" ")[1]
192
  return None
193
 
 
 
 
 
 
 
 
 
 
 
 
194
  def check_user_status(request: gr.Request):
195
  token = get_token_from_request(request)
196
  if token:
197
  try:
198
  user_info = whoami(token=token)
199
  space_runtime = get_space_runtime()
200
- if space_runtime and user_info and user_info["name"] == space_runtime.owner:
201
- return f"✅ 以所有者 **{user_info['fullname']}** 身份登录,将使用空间配置的密钥。", gr.update(visible=False)
 
202
  else:
203
  return f"👋 你好, **{user_info.get('fullname', '用户')}**!请在下方提供你自己的翻译 API 密钥。", gr.update(visible=True, open=True)
204
  except Exception as e:
@@ -251,7 +263,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=cus
251
  try:
252
  user_info = whoami(token=token)
253
  space_runtime = get_space_runtime()
254
- if space_runtime and user_info and user_info["name"] == space_runtime.owner:
255
  is_owner = True
256
  except Exception: pass
257
 
@@ -293,7 +305,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=cus
293
  traceback.print_exc()
294
  raise gr.Error(f"处理时发生错误: {e}")
295
 
296
- demo.load(fn=check_user_status, outputs=[user_status_md, api_key_accordion], queue=False)
297
 
298
  btn.click(
299
  process_image_and_generate_outputs,
@@ -321,4 +333,4 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=cus
321
  if __name__ == "__main__":
322
  if tagger_instance is None:
323
  print("CRITICAL: Tagger 未能初始化,应用功能将受限。")
324
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
6
  import onnxruntime as rt
7
  import pandas as pd
8
  from PIL import Image
9
+ from huggingface_hub import whoami, get_space_runtime, HfApi
10
+ from huggingface_hub.hf_api import SpaceRuntime
11
 
12
  from translator import translate_texts
13
 
 
184
  with gr.TabItem("⭐ 评分标签"): out_rating = gr.HTML(label="Rating Tags")
185
  gr.Markdown("### 标签汇总结果")
186
  out_summary = gr.Textbox(label="标签汇总", lines=5, show_copy_button=True)
187
+
188
  def get_token_from_request(request: gr.Request) -> str | None:
 
189
  auth_header = request.headers.get("authorization")
190
  if auth_header and auth_header.startswith("Bearer "):
191
  return auth_header.split(" ")[1]
192
  return None
193
 
194
+ def check_user_is_owner(user_info: dict | None, space_runtime: SpaceRuntime | None) -> bool:
195
+ if not user_info or not space_runtime:
196
+ return False
197
+ if user_info.get("name") == space_runtime.owner:
198
+ return True
199
+ user_orgs = user_info.get("orgs", [])
200
+ if any(org.get("name") == space_runtime.owner for org in user_orgs):
201
+ return True
202
+
203
+ return False
204
+
205
  def check_user_status(request: gr.Request):
206
  token = get_token_from_request(request)
207
  if token:
208
  try:
209
  user_info = whoami(token=token)
210
  space_runtime = get_space_runtime()
211
+
212
+ if check_user_is_owner(user_info, space_runtime):
213
+ return f"✅ 以所有者 **{user_info.get('fullname', user_info.get('name'))}** 身份登录,将使用空间配置的密钥。", gr.update(visible=False)
214
  else:
215
  return f"👋 你好, **{user_info.get('fullname', '用户')}**!请在下方提供你自己的翻译 API 密钥。", gr.update(visible=True, open=True)
216
  except Exception as e:
 
263
  try:
264
  user_info = whoami(token=token)
265
  space_runtime = get_space_runtime()
266
+ if check_user_is_owner(user_info, space_runtime):
267
  is_owner = True
268
  except Exception: pass
269
 
 
305
  traceback.print_exc()
306
  raise gr.Error(f"处理时发生错误: {e}")
307
 
308
+ demo.load(fn=check_user_status, inputs=None, outputs=[user_status_md, api_key_accordion], queue=False)
309
 
310
  btn.click(
311
  process_image_and_generate_outputs,
 
333
  if __name__ == "__main__":
334
  if tagger_instance is None:
335
  print("CRITICAL: Tagger 未能初始化,应用功能将受限。")
336
+ demo.launch(server_name="0.0.0.0", server_port=7860)