lgccccc commited on
Commit
d88fd87
·
verified ·
1 Parent(s): 9f54b41

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import gradio as gr
5
+ from huggingface_hub import HfApi
6
+
7
+ HF_TOKEN = os.getenv("HF_TOKEN")
8
+ SPACE_ID = os.getenv("SPACE_ID") # Hugging Face Spaces 内置环境变量,例如 username/space-name
9
+ DISCUSSION_NUM = int(os.getenv("DISCUSSION_NUM", "3")) # 改成你的 discussion 编号
10
+
11
+ api = HfApi(token=HF_TOKEN)
12
+
13
+
14
+ def send_hf_notification(message: str, profile: gr.OAuthProfile | None) -> str:
15
+ if profile is None:
16
+ return "请先点击上面的 Sign in with Hugging Face 登录。"
17
+
18
+ if not HF_TOKEN:
19
+ return "错误:没有配置 HF_TOKEN secret。"
20
+
21
+ if not SPACE_ID:
22
+ return "错误:没有找到 SPACE_ID。请确认代码运行在 Hugging Face Space 上。"
23
+
24
+ username = profile.username # HF username,例如 abidlabs
25
+
26
+ comment = f"""@{username}
27
+
28
+ 你的任务已经完成:
29
+
30
+ {message}
31
+ """
32
+
33
+ api.comment_discussion(
34
+ repo_id=SPACE_ID,
35
+ repo_type="space",
36
+ discussion_num=DISCUSSION_NUM,
37
+ comment=comment,
38
+ )
39
+
40
+ return f"已发送 Hugging Face notification 给 @{username}。"
41
+
42
+
43
+ with gr.Blocks() as demo:
44
+ gr.Markdown("## Hugging Face Notification Demo")
45
+ gr.LoginButton()
46
+
47
+ msg = gr.Textbox(
48
+ label="通知内容",
49
+ value="你的任务已经完成,请回到 Space 查看结果。",
50
+ )
51
+ btn = gr.Button("发送 HF Notification")
52
+ out = gr.Textbox(label="结果")
53
+
54
+ btn.click(
55
+ fn=send_hf_notification,
56
+ inputs=msg,
57
+ outputs=out,
58
+ )
59
+
60
+ demo.launch()