SJTUCL commited on
Commit
0bbc8ff
1 Parent(s): 9c53030

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -27
app.py CHANGED
@@ -16,7 +16,7 @@ color_map = {
16
  '20%': green.c200,
17
  '30%': green.c100,
18
  '40%': green.c50,
19
- '50%': None,
20
  '60%': red.c50,
21
  '70%': red.c100,
22
  '80%': red.c200,
@@ -31,23 +31,26 @@ def predict_doc(doc):
31
  res = []
32
  for sent in sents:
33
  prob = predict_one_sent(sent)
 
34
  data['sentence'].append(sent)
35
  data['score'].append(round(prob, 4))
 
 
 
 
36
  if prob < 0.1: label = '0%'
37
  elif prob < 0.2: label = '10%'
38
- elif prob < 0.3: label = '20%',
39
- elif prob < 0.4: label = '30%',
40
- elif prob < 0.5: label = '40%',
41
- elif prob < 0.6: label = '50%',
42
- elif prob < 0.7: label = '60%',
43
- elif prob < 0.8: label = '70%',
44
- elif prob < 0.9: label = '80%',
45
  elif prob < 1: label = '90%'
46
  else: label = '100%'
47
  res.append((sent, label))
48
- if prob <= 0.5:
49
- data['label'].append('Human')
50
- else: data['label'].append('Machine')
51
  df = pd.DataFrame(data)
52
  df.to_csv('result.csv')
53
  overall_score = df.score.mean()
@@ -55,7 +58,7 @@ def predict_doc(doc):
55
  if overall_score <= 0.5: overall_label = 'Human'
56
  else: overall_label = 'Machine'
57
  sum_str = f'The essay is probably written by {overall_label}. The probability of being generated by AI is {overall_score}'
58
- # print(sum_str)
59
  return sum_str, res, df, 'result.csv'
60
 
61
 
@@ -72,24 +75,32 @@ def predict_one_sent(sent):
72
 
73
 
74
  with gr.Blocks() as demo:
75
- text_in = gr.Textbox(
76
- lines=5,
77
- label='Essay input',
78
- info='Please enter the essay in the textbox'
79
- )
80
- button = gr.Button('Predict who writes this essay!')
81
- summary = gr.Textbox(lines=1, label='Result summary')
82
- sent_res = gr.HighlightedText(
83
- label = 'Labeled Result'
84
- ).style(color_map=color_map)
 
 
 
 
 
 
 
 
 
 
 
85
  tab = gr.DataFrame(
86
  label='Table with Probability Score',
87
- max_rows=10
88
- )
89
- csv_f = gr.File(
90
- label='CSV file storing data with all sentences.'
91
  )
92
- button.click(predict_doc, inputs=[text_in], outputs=[summary, sent_res, tab, csv_f], api_name='predict_doc')
93
 
94
  demo.launch()
95
 
 
16
  '20%': green.c200,
17
  '30%': green.c100,
18
  '40%': green.c50,
19
+ '50%': '#ffffff',
20
  '60%': red.c50,
21
  '70%': red.c100,
22
  '80%': red.c200,
 
31
  res = []
32
  for sent in sents:
33
  prob = predict_one_sent(sent)
34
+
35
  data['sentence'].append(sent)
36
  data['score'].append(round(prob, 4))
37
+ if prob <= 0.5:
38
+ data['label'].append('Human')
39
+ else: data['label'].append('Machine')
40
+
41
  if prob < 0.1: label = '0%'
42
  elif prob < 0.2: label = '10%'
43
+ elif prob < 0.3: label = '20%'
44
+ elif prob < 0.4: label = '30%'
45
+ elif prob < 0.5: label = '40%'
46
+ elif prob < 0.6: label = '50%'
47
+ elif prob < 0.7: label = '60%'
48
+ elif prob < 0.8: label = '70%'
49
+ elif prob < 0.9: label = '80%'
50
  elif prob < 1: label = '90%'
51
  else: label = '100%'
52
  res.append((sent, label))
53
+
 
 
54
  df = pd.DataFrame(data)
55
  df.to_csv('result.csv')
56
  overall_score = df.score.mean()
 
58
  if overall_score <= 0.5: overall_label = 'Human'
59
  else: overall_label = 'Machine'
60
  sum_str = f'The essay is probably written by {overall_label}. The probability of being generated by AI is {overall_score}'
61
+
62
  return sum_str, res, df, 'result.csv'
63
 
64
 
 
75
 
76
 
77
  with gr.Blocks() as demo:
78
+ with gr.Row():
79
+ with gr.Column():
80
+ text_in = gr.Textbox(
81
+ lines=5,
82
+ label='Essay input',
83
+ info='Please enter the essay in the textbox'
84
+ )
85
+ btn = gr.Button('Predict who writes this essay!')
86
+
87
+ sent_res = gr.Highlight(
88
+ label='Labeled Result'
89
+ ).style(color_map=color_map)
90
+
91
+ with gr.Row():
92
+ summary = gr.Text(
93
+ label='Result summary'
94
+ )
95
+ csv_f = gr.File(
96
+ label='CSV file storing data with all sentences.'
97
+ )
98
+
99
  tab = gr.DataFrame(
100
  label='Table with Probability Score',
101
+ max_rows=100
 
 
 
102
  )
103
+ btn.click(predict_doc, inputs=[text_in], outputs=[summary, sent_res, tab, csv_f], api_name='predict_doc')
104
 
105
  demo.launch()
106