guohp123456 commited on
Commit
4230b1a
·
verified ·
1 Parent(s): e03ea27

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import fasttext
2
+ import gradio as gr
3
+
4
+ # 训练数据(直接在代码中写入)
5
+ train_data = [
6
+ "__label__查余额 查询余额",
7
+ "__label__查余额 账户里有多少钱",
8
+ "__label__信用卡申请 申请信用卡",
9
+ "__label__信用卡申请 我想办一张信用卡",
10
+ "__label__贷款利率 贷款利率是多少",
11
+ "__label__贷款利率 现在房贷利息多少"
12
+ ]
13
+
14
+ # 将训练数据保存到一个临时文件,以便 FastText 使用
15
+ train_file = "train_data.txt"
16
+ with open(train_file, "w") as f:
17
+ for line in train_data:
18
+ f.write(line + "\n")
19
+
20
+ # 训练 FastText 分类模型
21
+ model = fasttext.train_supervised(input=train_file, epoch=25, lr=0.5, wordNgrams=2, dim=100)
22
+
23
+ # 保存训练好的模型
24
+ model.save_model("intent_classifier.bin")
25
+
26
+ # 定义一个用于预测的函数
27
+ def classify_intent(text):
28
+ label, confidence = model.predict(text)
29
+ return label[0].replace("__label__", ""), confidence[0]
30
+
31
+ # 创建 Gradio 界面
32
+ iface = gr.Interface(fn=classify_intent,
33
+ inputs=gr.Textbox(label="请输入查询内容"),
34
+ outputs=[gr.Textbox(label="预测意图"), gr.Textbox(label="置信度")])
35
+
36
+ # 启动 Gradio 应用
37
+ iface.launch()