LiuYunhui commited on
Commit
bb49726
·
1 Parent(s): 02249fb

Add model inference for file

Browse files
Files changed (1) hide show
  1. app.py +28 -0
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import gradio as gr
2
  import pandas as pd
3
  from sentiment_analyser import RandomAnalyser, RoBERTaAnalyser, ChatGPTAnalyser
@@ -49,6 +50,21 @@ def analysis(Text):
49
  return pd.DataFrame([values], columns=keys)
50
 
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  MODEL_MAPPING = {
53
  'Random': RandomAnalyser(),
54
  'RoBERTa': RoBERTaAnalyser(),
@@ -91,6 +107,18 @@ with gr.Blocks(title=TITLE) as demo:
91
  headers=list(MODEL_MAPPING.keys()), interactive=False)
92
  text_input.submit(analysis, inputs=text_input, outputs=senti_output, show_progress=True)
93
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  gr.HTML("<H2>Model Evaluation</H2>")
95
  gr.Markdown((
96
  "这里是在 StackOverflow4423 数据集上评估我们的模型。"
 
1
+ import csv
2
  import gradio as gr
3
  import pandas as pd
4
  from sentiment_analyser import RandomAnalyser, RoBERTaAnalyser, ChatGPTAnalyser
 
50
  return pd.DataFrame([values], columns=keys)
51
 
52
 
53
+ def analyse_file(file):
54
+ output_name = 'output.csv'
55
+ with open(output_name, mode='w', newline='') as output:
56
+ writer = csv.writer(output)
57
+ header = ['Text', 'Label']
58
+ writer.writerow(header)
59
+ model = MODEL_MAPPING[OUR_MODEL]
60
+ with open(file.name) as f:
61
+ for line in f:
62
+ text = line[:-1]
63
+ sentiment = model.predict([text])
64
+ writer.writerow([text, sentiment[0]])
65
+ return output_name
66
+
67
+
68
  MODEL_MAPPING = {
69
  'Random': RandomAnalyser(),
70
  'RoBERTa': RoBERTaAnalyser(),
 
107
  headers=list(MODEL_MAPPING.keys()), interactive=False)
108
  text_input.submit(analysis, inputs=text_input, outputs=senti_output, show_progress=True)
109
 
110
+ gr.Markdown((
111
+ "在左侧文件框中上传 txt/csv 文件,模型会对输入文本的每一行当作一个文本进行情感分析。"
112
+ "可以在右侧下载输出文件,输出文件为两列 csv 格式,第一列为原始文本,第二列为分类结果。"
113
+ ))
114
+ with gr.Row():
115
+ with gr.Column():
116
+ file_input = gr.File(label='File',
117
+ file_types=['.txt', '.csv'])
118
+ with gr.Column():
119
+ file_output = gr.File(label='Output')
120
+ file_input.upload(analyse_file, inputs=file_input, outputs=file_output)
121
+
122
  gr.HTML("<H2>Model Evaluation</H2>")
123
  gr.Markdown((
124
  "这里是在 StackOverflow4423 数据集上评估我们的模型。"