Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -146,13 +146,23 @@ 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 |
state_res = gr.State({})
|
| 153 |
state_translations_dict = gr.State({})
|
| 154 |
|
| 155 |
-
with gr.Row(visible=False) as main_interface:
|
| 156 |
with gr.Column(scale=1):
|
| 157 |
img_in = gr.Image(type="pil", label="上传图片", height=300)
|
| 158 |
btn = gr.Button("🚀 开始分析", variant="primary", elem_classes=["btn-analyze-container"])
|
|
@@ -197,14 +207,11 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=cus
|
|
| 197 |
try:
|
| 198 |
user_info = whoami(token=token)
|
| 199 |
welcome_msg = f"✅ 您好, **{user_info.get('fullname', user_info.get('name'))}**!欢迎使用本应用。"
|
| 200 |
-
return welcome_msg, gr.update(visible=True)
|
| 201 |
except Exception as e:
|
| 202 |
print(f"Token 无效或已过期: {e}")
|
| 203 |
|
| 204 |
-
|
| 205 |
-
login_url = "/login?redirect=/"
|
| 206 |
-
msg = f"**<p style='color:red; text-align:center;'>🚫 检测到您尚未登录,请先 <a href='{login_url}' target='_top'>[点击此处登录]</a> 后刷新页面。</p>**"
|
| 207 |
-
return msg, gr.update(visible=False) # 隐藏主界面
|
| 208 |
|
| 209 |
def format_tags_html(tags_dict, translations_list, show_scores):
|
| 210 |
if not tags_dict: return "<p>暂无标签</p>"
|
|
@@ -248,7 +255,6 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=cus
|
|
| 248 |
|
| 249 |
yield gr.update(interactive=False, value="🔄 处理中..."), gr.update(visible=True, value="🔄 正在分析..."), *["<p>分析中...</p>"]*3, "分析中...", {}, {}
|
| 250 |
|
| 251 |
-
# 密钥决策逻辑
|
| 252 |
use_space_keys = bool(ACCESS_PASSWORD and access_pwd == ACCESS_PASSWORD)
|
| 253 |
|
| 254 |
final_tencent_id, final_tencent_key, baidu_json_str = (
|
|
@@ -290,7 +296,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=cus
|
|
| 290 |
raise gr.Error(f"处理时发生错误: {e}")
|
| 291 |
|
| 292 |
# ----------------- 绑定事件 -----------------
|
| 293 |
-
demo.load(fn=check_user_status, inputs=None, outputs=[user_status_md, main_interface], queue=False)
|
| 294 |
|
| 295 |
btn.click(
|
| 296 |
process_image_and_generate_outputs,
|
|
|
|
| 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 |
|
| 165 |
+
with gr.Row(visible=False) as main_interface:
|
| 166 |
with gr.Column(scale=1):
|
| 167 |
img_in = gr.Image(type="pil", label="上传图片", height=300)
|
| 168 |
btn = gr.Button("🚀 开始分析", variant="primary", elem_classes=["btn-analyze-container"])
|
|
|
|
| 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>"
|
|
|
|
| 255 |
|
| 256 |
yield gr.update(interactive=False, value="🔄 处理中..."), gr.update(visible=True, value="🔄 正在分析..."), *["<p>分析中...</p>"]*3, "分析中...", {}, {}
|
| 257 |
|
|
|
|
| 258 |
use_space_keys = bool(ACCESS_PASSWORD and access_pwd == ACCESS_PASSWORD)
|
| 259 |
|
| 260 |
final_tencent_id, final_tencent_key, baidu_json_str = (
|
|
|
|
| 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,
|