andreslu commited on
Commit
b397ba0
·
1 Parent(s): 2832422

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -17
app.py CHANGED
@@ -1,22 +1,26 @@
1
  import gradio as gr
 
2
 
3
- # 定义第一个接口
4
- def greet(name):
5
- return "Hello " + name + "!!"
 
6
 
7
- iface_greet = gr.Interface(fn=greet, inputs="text", outputs="text", title="Greeting")
 
 
 
 
 
 
 
 
 
8
 
9
- # 定义第二个接口
10
- def count_numbers(n):
11
- return len(str(n))
 
 
12
 
13
- iface_count = gr.Interface(fn=count_numbers, inputs="number", outputs="number", title="Count Numbers")
14
-
15
- # 将两个接口添加到接口组中
16
- iface_group = gr.InterfaceGroup([iface_greet, iface_count], title="My Interfaces")
17
-
18
- # 为第一个接口添加一个按钮,点击该按钮将切换到第二个接口
19
- iface_greet.add_button("Count Numbers", interface=iface_count)
20
-
21
- # 启动接口组
22
- iface_group.launch()
 
1
  import gradio as gr
2
+ from transformers import pipeline, AutoTokenizer
3
 
4
+ # 加载模型和分词器
5
+ model_name = "bert-base-uncased"
6
+ model = pipeline("text-classification", model=model_name, tokenizer=model_name)
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
 
9
+ # 定义 Gradio 接口
10
+ def classify_text(text):
11
+ # 对输入文本进行分词
12
+ inputs = tokenizer(text, return_tensors="pt")
13
+ # 使用模型进行预测
14
+ outputs = model(inputs)
15
+ # 获取预测结果
16
+ label = outputs[0]["label"]
17
+ score = outputs[0]["score"]
18
+ return f"Label: {label}, Score: {score:.2f}"
19
 
20
+ iface = gr.Interface(
21
+ fn=classify_text,
22
+ inputs=gr.inputs.Textbox(),
23
+ outputs=gr.outputs.HTML(),
24
+ )
25
 
26
+ iface.launch()