DeepLearning101 commited on
Commit
4e7c010
·
verified ·
1 Parent(s): 6730acb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import T5ForConditionalGeneration, T5Tokenizer
3
+ import torch
4
+
5
+ # 指定模型路徑或 Hugging Face Model Hub 上的模型 ID
6
+ model_name_or_path = "DeepLearning101/Corrector101zhTWT5"
7
+ auth_token = os.getenv("HF_HOME")
8
+
9
+ # 嘗試加載模型和分詞器
10
+ try:
11
+ tokenizer = T5Tokenizer.from_pretrained(model_name_or_path, use_auth_token=auth_token)
12
+ model = T5ForConditionalGeneration.from_pretrained(model_name_or_path, use_auth_token=auth_token)
13
+ model.eval()
14
+ except Exception as e:
15
+ print(f"加載模型或分詞器失敗,錯誤信息:{e}")
16
+ exit(1)
17
+
18
+ if torch.cuda.is_available():
19
+ model.cuda() # 如果可用,將模型移至 GPU
20
+
21
+ def correct_text(text):
22
+ """將輸入的文本通過 T5 模型進行修正"""
23
+ inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True, padding=True)
24
+ if torch.cuda.is_available():
25
+ inputs = {k: v.cuda() for k, v in inputs.items()} # 將輸入移至 GPU
26
+ with torch.no_grad():
27
+ outputs = model.generate(**inputs)
28
+ corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
29
+ return corrected_text
30
+
31
+ def main():
32
+ interface = gr.Interface(
33
+ fn=correct_text,
34
+ inputs=gr.Textbox(lines=5, placeholder="請輸入需要修正的中文文本..."),
35
+ outputs=gr.Textbox(label="修正後的文本"),
36
+ title="客服ASR文本AI糾錯系統",
37
+ description="""<a href='https://www.twman.org' target='_blank'>TonTon Huang Ph.D. @ 2024/04 </a><br>
38
+ 輸入ASR文本,糾正同音字/詞錯誤<br>
39
+ <a href='https://blog.twman.org/2021/04/ASR.html' target='_blank'>那些語音處理 (Speech Processing) 踩的坑</a><br>
40
+ <a href='https://blog.twman.org/2024/02/asr-tts.html' target='_blank'>那些ASR和TTS可能會踩的坑</a><br>
41
+ <a href='https://blog.twman.org/2021/04/NLP.html' target='_blank'>那些自然語言處理 (Natural Language Processing, NLP) 踩的坑</a><br>
42
+ 基於transformers的T5ForConditionalGeneration""",
43
+ theme="default",
44
+ examples=[
45
+ ["你究輸入利的手機門號跟生分證就可以了。"],
46
+ ["這裡是客服中新,很高性為您服物,請問金天有什麼須要幫忙您得"],
47
+ ["因為我們這邊是按天術比例計蒜給您的,其實不會有態大的穎響。也就是您用前面的資非的廢率來做計算"],
48
+ ["我來看以下,他的時價是多少?起實您就可以直皆就不用到門事"],
49
+ ["因為你現在月富是六九九嘛,我幫擬減衣百塊,兒且也不會江速"]
50
+ ]
51
+ )
52
+ interface.launch(share=True)
53
+
54
+ if __name__ == "__main__":
55
+ main()