fexeak
commited on
Commit
·
ad71468
1
Parent(s):
16f95f5
feat: 添加Hugging Face token登录功能
Browse files从环境变量获取HF_TOKEN并实现登录验证,未设置token时提示用户并退出
app.py
CHANGED
|
@@ -1,4 +1,16 @@
|
|
| 1 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
safe_pipe = pipeline(
|
| 4 |
"text-generation",
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
+
from huggingface_hub import login
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# 设置 Hugging Face token
|
| 6 |
+
HF_TOKEN = os.getenv('HF_TOKEN') # 从环境变量获取 token
|
| 7 |
+
if HF_TOKEN:
|
| 8 |
+
login(token=HF_TOKEN) # 使用 token 登录
|
| 9 |
+
else:
|
| 10 |
+
print("请设置环境变量 HF_TOKEN 为你的 Hugging Face token")
|
| 11 |
+
print("Windows 设置方法: $env:HF_TOKEN = '你的token'")
|
| 12 |
+
print("或者直接在代码中设置: login(token='你的token')")
|
| 13 |
+
exit(1)
|
| 14 |
|
| 15 |
safe_pipe = pipeline(
|
| 16 |
"text-generation",
|