cwadayi commited on
Commit
6ceffd5
·
verified ·
1 Parent(s): 1793f82

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -27
app.py CHANGED
@@ -1,46 +1,59 @@
1
  import gradio as gr
2
- from transformers import BertTokenizer, BertForSequenceClassification, pipeline
3
- import torch
4
 
5
- # 1. 載入編碼工具與預訓練模型 (對應書中 2.3 節)
6
- model_name = "bert-base-chinese"
7
- tokenizer = BertTokenizer.from_pretrained(model_name)
8
 
9
- # 使用 pipeline 簡化測試流程 (對應書中第 5 章概念)
10
- classifier = pipeline("sentiment-analysis", model="liam168/chat-gpt-detector-roberta-chinese")
 
 
11
 
12
- def process_text(text):
13
- # 2. 實作編碼工作流程 (對應書中表 2-1)
14
- # 取得 input_ids, attention_mask 等資訊
 
 
 
 
 
 
 
 
 
 
15
  encodings = tokenizer.encode_plus(
16
  text,
17
- add_special_tokens=True,
18
- max_length=25,
19
- padding='max_length',
 
20
  return_tensors="pt"
21
  )
22
-
23
- # 取得編碼後的數字 (input_ids)
24
  input_ids = encodings['input_ids'].tolist()[0]
25
 
26
- # 3. 執行推理 (對應測試階段)
27
  result = classifier(text)[0]
28
- label = result['label']
29
- score = result['score']
30
 
 
 
 
31
  return {
32
- "編碼結果 (Input IDs)": str(input_ids),
33
- "情感分析結果": f"{label} (信心程度: {score:.2f})",
34
- "還原文字 (Decode)": tokenizer.decode(input_ids) # 對應書中 decode 測試
 
35
  }
36
 
37
- # 4. 建立 Gradio 介面
38
  demo = gr.Interface(
39
- fn=process_text,
40
- inputs=gr.Textbox(placeholder="請輸入中文句子,例如:你站在橋上看風景"),
41
- outputs="json",
42
- title="Hugging Face 標準流程實作 - 中文編碼與分析",
43
- description="本程式根據李福林老師著作,實作編碼器 (Tokenizer) 流程與模型測試。"
44
  )
45
 
46
  if __name__ == "__main__":
 
1
  import gradio as gr
2
+ import os
3
+ from transformers import BertTokenizer, pipeline
4
 
5
+ # Space Secrets 中讀取 Token
6
+ hf_token = os.getenv("HF_TOKEN")
 
7
 
8
+ # 1. 載入編碼器與模型 (對應書中 2.3.1 )
9
+ # 若官方模型暫時無法讀取,建議使用此穩定的中文情感分析模型
10
+ model_name = "shibing624/bert-chinese-sentiment"
11
+ tokenizer = BertTokenizer.from_pretrained(model_name, use_auth_token=hf_token)
12
 
13
+ # 2. 實作書中提到的字典操作 (對應 2.3.6 節)
14
+ # 增加新詞,使其不被拆散,例如「明月」
15
+ new_tokens = ['明月', '裝飾', '窗子']
16
+ tokenizer.add_tokens(new_tokens)
17
+
18
+ # 載入推理管線
19
+ classifier = pipeline("sentiment-analysis", model=model_name, use_auth_token=hf_token)
20
+
21
+ def nlp_workflow(text):
22
+ if not text.strip():
23
+ return "請輸入文字"
24
+
25
+ # 3. 進階編碼函數實作 (對應書中 2.3.4 節與表 2-2)
26
  encodings = tokenizer.encode_plus(
27
  text,
28
+ add_special_tokens=True, # 增加 [CLS], [SEP]
29
+ max_length=25, # 參考書中範例長度
30
+ padding='max_length', # 補齊長度至 25
31
+ truncation=True, # 超長截斷
32
  return_tensors="pt"
33
  )
34
+
 
35
  input_ids = encodings['input_ids'].tolist()[0]
36
 
37
+ # 4. 測試推理結果 (對應標準流程中的「測試」階段)
38
  result = classifier(text)[0]
 
 
39
 
40
+ # 5. 還原文字驗證 (對應書中 decode 測試)
41
+ decoded_text = tokenizer.decode(input_ids)
42
+
43
  return {
44
+ "情感分析結果": f"{result['label']} (信心度: {result['score']:.4f})",
45
+ "編碼 ID (input_ids)": input_ids,
46
+ "解碼還原文字": decoded_text,
47
+ "注意": "若輸入包含「明月」,您會發現它被視為單一 Token 而非拆散。"
48
  }
49
 
50
+ # 建立 Gradio 介面
51
  demo = gr.Interface(
52
+ fn=nlp_workflow,
53
+ inputs=gr.Textbox(label="輸入中文句子", placeholder="例如:明月裝飾了你的窗子"),
54
+ outputs=gr.JSON(label="Hugging Face 標準流程輸出"),
55
+ title="李福林《Hugging Face 自然語言處理實戰》流程實作",
56
+ description="本程式實作了書中第一章的標準流程與第二章的編碼工具細節。"
57
  )
58
 
59
  if __name__ == "__main__":