Cpuritan commited on
Commit
fe938ab
1 Parent(s): b216f96

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -1
app.py CHANGED
@@ -7,10 +7,29 @@ import gradio as gr
7
  import numpy as np
8
  import pandas as pd
9
  import matplotlib.pyplot as plt
10
- from transformers import BertConfig, BertTokenizer, XLMRobertaForSequenceClassification
11
  from keras.models import load_model
12
 
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def text_clf(text):
15
  vocab_file = "vocab.txt" # 词汇表
16
  tokenizer = BertTokenizer(vocab_file)
@@ -125,7 +144,9 @@ with gr.Blocks() as demo:
125
  with gr.Tab("Flip Text"):
126
  text = gr.Textbox(label="文本哟")
127
  text_output = gr.outputs.Label(label="情感呢")
 
128
  text_button = gr.Button("确认")
 
129
 
130
  with gr.Tab("Flip Audio"):
131
  audio = gr.Audio(label="音频捏")
@@ -139,6 +160,7 @@ with gr.Blocks() as demo:
139
  cir_button = gr.Button("确认")
140
 
141
  text_button.click(fn=text_clf, inputs=text, outputs=text_output)
 
142
  audio_button.click(fn=audio_clf, inputs=audio, outputs=audio_output)
143
  cir_button.click(fn=cir_clf, inputs=[cir_l, cir_r], outputs=cir_output)
144
 
 
7
  import numpy as np
8
  import pandas as pd
9
  import matplotlib.pyplot as plt
10
+ from transformers import BertConfig, BertTokenizer, XLMRobertaForSequenceClassification, BertForTokenClassification
11
  from keras.models import load_model
12
 
13
 
14
+ def text_clf_ori(text):
15
+ vocab_file = "vocab.txt" # 词汇表
16
+ tokenizer = BertTokenizer(vocab_file)
17
+ # 加载模型
18
+ config = BertConfig.from_pretrained("nanaaaa/emotion_chinese_english")
19
+ model = BertForTokenClassification.from_pretrained("nanaaaa/emotion_chinese_english", config=config)
20
+
21
+ inputs = tokenizer(text, return_tensors="pt")
22
+
23
+ # 模型推断
24
+ outputs = model(**inputs)
25
+ probs = torch.nn.functional.softmax(outputs.logits, dim=1)
26
+ # 创建标签和概率列表
27
+ labels = ["害怕", "高兴喵", "惊喜", "伤心", "生气"]
28
+ probabilities = probs.detach().cpu().numpy()[0].tolist()
29
+ # 返回标签和概率列表
30
+ return {labels[i]: float(probabilities[0][i]) for i in range(len(labels))}
31
+
32
+
33
  def text_clf(text):
34
  vocab_file = "vocab.txt" # 词汇表
35
  tokenizer = BertTokenizer(vocab_file)
 
144
  with gr.Tab("Flip Text"):
145
  text = gr.Textbox(label="文本哟")
146
  text_output = gr.outputs.Label(label="情感呢")
147
+ text_output1 = gr.outputs.Label(label="情感呢")
148
  text_button = gr.Button("确认")
149
+ text_button1 = gr.Button("确认对比")
150
 
151
  with gr.Tab("Flip Audio"):
152
  audio = gr.Audio(label="音频捏")
 
160
  cir_button = gr.Button("确认")
161
 
162
  text_button.click(fn=text_clf, inputs=text, outputs=text_output)
163
+ text_button1.click(fn=text_clf_ori, inputs=text, outputs=text_output1)
164
  audio_button.click(fn=audio_clf, inputs=audio, outputs=audio_output)
165
  cir_button.click(fn=cir_clf, inputs=[cir_l, cir_r], outputs=cir_output)
166