soldni commited on
Commit
0583214
1 Parent(s): 6d60b3c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from io import StringIO
2
+
3
+ import gradio as gr
4
+ import pandas as pd
5
+ import spacy
6
+
7
+
8
+ nlp = spacy.load('en_core_web_sm')
9
+
10
+ HTML_RED = '<span style="background-color: rgba(255, 0, 0, 0.2)">{t}</span>'
11
+ HTML_GRN = '<span style="background-color: rgba(0, 255, 0, 0.3)">{t}</span>'
12
+ HTML_BLU = '<span style="background-color: rgba(0, 0, 255, 0.2)">{t}</span>'
13
+ HTML_PLN = '<span>{t}</span>'
14
+ TABLE_CSS = '''
15
+ th, td {
16
+ padding: 4px;
17
+ }
18
+ table, th, td {
19
+ border: 1px solid black;
20
+ border-collapse: collapse;
21
+
22
+ }
23
+ '''
24
+
25
+ def colorize(file_obj):
26
+ with open(file_obj.name, 'r') as f:
27
+ raw = f.read()
28
+ raw = raw[raw.find('example_id'):]
29
+ data = pd.read_csv(StringIO(raw))
30
+
31
+ table_content = []
32
+
33
+ for row in data.iterrows():
34
+ gold, genA, genB = nlp.pipe((
35
+ row[1]['target summary'],
36
+ row[1]['model summary A'],
37
+ row[1]['model summary B']
38
+ ))
39
+ tokens_gold = {token.lemma_.lower() for token in gold}
40
+ table_content.append(
41
+ [gold.text] +
42
+ [
43
+ ''.join(
44
+ (
45
+ HTML_PLN.format(t=token.text)
46
+ if token.pos_ not in {'NOUN', 'PROPN', 'VERB'}
47
+ else (
48
+ HTML_GRN.format(t=token.text)
49
+ if token.lemma_.lower() in tokens_gold
50
+ else HTML_RED.format(t=token.text)
51
+ )
52
+ ) + token.whitespace_
53
+ for token in gen
54
+ )
55
+ for gen in (genA, genB)
56
+ ]
57
+ )
58
+
59
+ # return an HTML table using data in table_content
60
+ return '\n'.join((
61
+ '<table>',
62
+ "<tr>"
63
+ "<td><b>Gold</b></td>",
64
+ "<td><b>Model A</b></td>",
65
+ "<td><b>Model B</b></td>",
66
+ "</tr>",
67
+ '\n'.join(
68
+ '<tr>\n' +
69
+ '\n'.join('<td>{}</td>'.format(cell) for cell in row) +
70
+ '\n</tr>'
71
+ for row in table_content
72
+ ),
73
+ '</table>'
74
+ ))
75
+
76
+
77
+ def main():
78
+ with gr.Blocks(css=TABLE_CSS) as demo:
79
+ gr.Markdown(
80
+ "After uploading, click Run and switch to the Visualization tab."
81
+ )
82
+ with gr.Tabs():
83
+ with gr.TabItem("Upload"):
84
+ data = gr.File(
85
+ label='upload csv with Annotations', type='file'
86
+ )
87
+ run = gr.Button(label='Run')
88
+ with gr.TabItem("Visualization"):
89
+ viz = gr.HTML(label='Upload a csv file to start.')
90
+ run.click(colorize, data, viz)
91
+
92
+ demo.launch()
93
+
94
+
95
+ if __name__ == '__main__':
96
+ main()