howard9963 commited on
Commit
d363f25
·
verified ·
1 Parent(s): 82596a4

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -32
app.py CHANGED
@@ -1,32 +1,35 @@
1
- import os
2
- from fastapi import FastAPI, Header, HTTPException, Body
3
- from transformers import AutoTokenizer
4
-
5
- # 從環境變數讀取 secret
6
- EXPECTED_API_KEY = os.environ.get("apikey")
7
-
8
- # 初始化 tokenizer
9
- tokenizer = AutoTokenizer.from_pretrained("ckiplab/bert-base-chinese-ws")
10
-
11
- app = FastAPI(title="CKIP Word Segmentation API")
12
-
13
- # ✅ GET 方法:健康檢查
14
- @app.get("/check")
15
- def health_check():
16
- return {"status": "ok", "message": "API is running"}
17
-
18
- # ✅ POST 方法:斷詞
19
- @app.post("/tokenize")
20
- async def tokenize(
21
- text: str = Body(..., embed=True),
22
- x_api_key: str = Header(None)
23
- ):
24
- # 驗證 API Key 設定是否存在
25
- if not EXPECTED_API_KEY:
26
- raise HTTPException(status_code=500, detail="Server missing API_KEY config")
27
- # 驗證 API Key 值是否正確
28
- if x_api_key != EXPECTED_API_KEY:
29
- raise HTTPException(status_code=401, detail="Invalid API Key")
30
-
31
- tokens = tokenizer.tokenize(text)
32
- return {"tokens": tokens}
 
 
 
 
1
+ import os
2
+ from fastapi import FastAPI, Header, HTTPException, Body
3
+ from transformers import AutoTokenizer
4
+
5
+ # 從環境變數讀取 secret
6
+ EXPECTED_API_KEY = os.environ.get("apikey")
7
+
8
+ # 初始化 tokenizer
9
+ tokenizer = AutoTokenizer.from_pretrained("ckiplab/bert-base-chinese-ws")
10
+
11
+ app = FastAPI(title="CKIP Word Segmentation API")
12
+
13
+ # ✅ GET 方法:健康檢查
14
+ @app.get("/check")
15
+ def health_check() as demo:
16
+ return {"status": "ok", "message": "API is running"}
17
+
18
+ # ✅ POST 方法:斷詞
19
+ @app.post("/tokenize")
20
+ async def tokenize(
21
+ text: str = Body(..., embed=True),
22
+ x_api_key: str = Header(None)
23
+ ):
24
+ # 驗證 API Key 設定是否存在
25
+ if not EXPECTED_API_KEY:
26
+ raise HTTPException(status_code=500, detail="Server missing API_KEY config")
27
+ # 驗證 API Key 值是否正確
28
+ if x_api_key != EXPECTED_API_KEY:
29
+ raise HTTPException(status_code=401, detail="Invalid API Key")
30
+
31
+ tokens = tokenizer.tokenize(text)
32
+ return {"tokens": tokens}
33
+
34
+ if __name__ == "__main__":
35
+ demo.launch()