samueldomdey commited on
Commit
ab4f033
1 Parent(s): e3f32c5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # imports
2
+ from transformers import pipeline
3
+ import gradio as gr
4
+ import pandas as pd
5
+
6
+ # define nlp mask
7
+ model = "siebert/sentiment-roberta-large-english"
8
+ nlp = pipeline(model=model, device=0) # set device=0 to use GPU (CPU default, -1)
9
+
10
+ # perform inference on given file
11
+ def inference(df, filename):
12
+ # texts & ids
13
+ texts = df[df.columns[1]].to_list()
14
+ ids = df[df.columns[0]].to_list()
15
+
16
+ # create new df based on csv inputs
17
+ new_df = pd.DataFrame(columns=[df.columns[0], df.columns[1], "Label", "Score"])
18
+
19
+ # iterate over texts, perform inference
20
+ for index in range(len(texts)):
21
+ preds = nlp(texts[index])
22
+ pred_sentiment = preds[0]["label"]
23
+ pred_score = preds[0]["score"]
24
+ print(texts[index])
25
+ print(preds)
26
+
27
+ # write data into df
28
+ # predicted sentiment
29
+ new_df.at[index, "Label"] = pred_sentiment
30
+ # predicted score
31
+ new_df.at[index, "Score"] = pred_score
32
+ # write text
33
+ new_df.at[index, df.columns[1]] = texts[index]
34
+ # write ID
35
+ new_df.at[index, df.columns[0]] = ids[index]
36
+
37
+ # export new file
38
+ n_filename = filename.name.split(".")[0] + "_csiebert_sentiment.csv"
39
+ new_df.to_csv(n_filename, index=False)
40
+
41
+ # return new file
42
+ return n_filename
43
+
44
+ # handle file reading for both csv and excel files
45
+ def read_file(filename):
46
+ # check type of input file
47
+ if filename.name.split(".")[1] == "csv":
48
+ print("entered")
49
+ # read file, drop index if exists
50
+ df = pd.read_csv(filename.name, index_col=False)
51
+ # perform inference on given .csv file
52
+ result = inference(df=df, filename=filename)
53
+ print("computed")
54
+ return result
55
+ elif filename.name.split(".")[1] == "xlsx":
56
+ df = pd.read_excel(filename.name, index_col=False)
57
+ # handle Unnamed
58
+ if df.columns[0] == "Unnamed: 0":
59
+ df = df.drop("Unnamed: 0", axis=1)
60
+ # perform inference on given .xlsx file
61
+ result = inference(df=df, filename=filename)
62
+ return result
63
+ # if neither csv nor xlsx provided -> exit
64
+ else:
65
+ return
66
+
67
+ gr.Interface(read_file,
68
+ inputs=[gr.inputs.File(label="Input file")],
69
+ outputs=[gr.outputs.File(label="Output file")],
70
+ description="Sentiment analysis: Input a csv/xlsx of form ID, Text. App performs sentiment analysis on Texts and exports results as new csv to download.",
71
+ allow_flagging=False,
72
+ layout="horizontal",
73
+ ).launch()