eliseobao commited on
Commit
483096c
1 Parent(s): 37b647e

Improvements

Browse files
requirements.txt CHANGED
File without changes
src/manage.py CHANGED
File without changes
src/templates/index.html CHANGED
@@ -12,7 +12,6 @@
12
  var checkboxRouge1 = document.getElementById('checkbox-rouge1').checked;
13
  var checkboxRouge2 = document.getElementById('checkbox-rouge2').checked;
14
  var checkboxRougeL = document.getElementById('checkbox-rougeL').checked;
15
- var checkboxTf1 = document.getElementById('checkbox-tf1').checked;
16
 
17
  $.ajax({
18
  type: 'POST',
@@ -25,7 +24,6 @@
25
  checkbox_rouge1: checkboxRouge1,
26
  checkbox_rouge2: checkboxRouge2,
27
  checkbox_rougeL: checkboxRougeL,
28
- checkbox_tf1: checkboxTf1,
29
  csrfmiddlewaretoken: '{{ csrf_token }}'
30
  },
31
  success: function (data) {
@@ -47,14 +45,11 @@
47
  if (checkboxRougeL && 'rougeL' in data) {
48
  resultHTML += '<p>ROUGE-L: ' + data.rougeL + '</p>';
49
  }
50
- if (checkboxTf1 && 'tf1' in data) {
51
- resultHTML += '<p>TF1: ' + data.tf1 + '</p>';
52
- }
53
 
54
  resultsContainer.innerHTML = resultHTML || '<p>No results available</p>';
55
  },
56
  error: function () {
57
- alert('Error computing results');
58
  }
59
  });
60
  }
@@ -93,17 +88,38 @@
93
  function handleDragOver(event) {
94
  event.preventDefault();
95
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  </script>
97
 
98
  <head>
99
  <meta charset="UTF-8">
100
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
101
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
102
- <title>BLEU | ROUGE | TF1</title>
103
  <style>
104
  body {
105
- font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
106
- background-color: #f5f5f5;
107
  margin: 0;
108
  display: flex;
109
  align-items: center;
@@ -112,54 +128,64 @@
112
  }
113
 
114
  #container {
115
- background-color: #fff;
116
- box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
117
- border-radius: 8px;
118
  width: 80%;
119
  max-width: 800px;
120
- padding: 20px;
121
  box-sizing: border-box;
122
  }
123
 
124
  header {
125
  text-align: center;
126
- margin-bottom: 20px;
127
  }
128
 
129
- .label-container {
130
- width: 100%;
131
- text-align: center;
132
- margin-bottom: 20px;
 
 
 
 
 
133
  }
134
 
135
  textarea {
136
  width: 100%;
137
- padding: 10px;
138
  box-sizing: border-box;
139
- border: 1px solid #ccc;
140
- border-radius: 4px;
141
  resize: vertical;
142
  }
143
 
 
 
 
 
 
144
  .checkbox-container {
145
  text-align: center;
146
- margin-bottom: 20px;
147
  }
148
 
149
- label {
150
- margin-right: 10px;
 
151
  }
152
 
153
  button {
154
  background-color: #4caf50;
155
  color: #fff;
156
  border: none;
157
- padding: 10px 20px;
158
  text-align: center;
159
  text-decoration: none;
160
  display: inline-block;
161
- font-size: 16px;
162
- border-radius: 4px;
163
  cursor: pointer;
164
  transition: background-color 0.3s;
165
  }
@@ -167,6 +193,16 @@
167
  button:hover {
168
  background-color: #45a049;
169
  }
 
 
 
 
 
 
 
 
 
 
170
  </style>
171
  </head>
172
 
@@ -174,56 +210,61 @@
174
  <div id="container">
175
  <header>
176
  <h1>How faithful are your predictions?</h1>
177
- <p>You can write, drag-and-drop or upload your references and predictions. <br> End <span style="text-decoration:underline">all</span> of them with with a comma and a
178
- newline.</p>
179
  </header>
180
 
181
  <div class="label-container" ondrop="handleFileDrop(event, 'references')" ondragover="handleDragOver(event)">
182
  <label for="references">References</label>
183
- <textarea id="references" rows="5" placeholder="a,&#10;b,&#10;c&#10;" draggable="true"
184
- ondrop="handleFileDrop(event, 'references')" ondragover="handleDragOver(event)"></textarea>
 
 
185
  <input type="file" id="file-references" accept=".txt"
186
  onchange="handleFileUpload('file-references', 'references')">
187
  </div>
188
 
189
  <div class="label-container" ondrop="handleFileDrop(event, 'predictions')" ondragover="handleDragOver(event)">
190
  <label for="predictions">Predictions</label>
191
- <textarea id="predictions" rows="5" placeholder="a,&#10;d,&#10;c&#10;" draggable="true"
192
- ondrop="handleFileDrop(event, 'predictions')" ondragover="handleDragOver(event)"></textarea>
 
 
193
  <input type="file" id="file-predictions" accept=".txt"
194
  onchange="handleFileUpload('file-predictions', 'predictions')">
195
  </div>
196
 
197
  <div class="checkbox-container">
198
  <label for="checkbox-sentence-bleu">
199
- <input type="checkbox" id="checkbox-sentence-bleu" name="checkbox-sentence-bleu"> <a href="https://aclanthology.org/P02-1040/">Sentence BLEU</a>
 
200
  </label>
201
 
202
  <label for="checkbox-corpus-bleu">
203
- <input type="checkbox" id="checkbox-corpus-bleu" name="checkbox-corpus-bleu"> <a href="https://aclanthology.org/P02-1040/">Corpus BLEU</a>
 
204
  </label>
205
 
206
  <label for="checkbox-rouge1">
207
- <input type="checkbox" id="checkbox-rouge1" name="checkbox-rouge1"> <a href="https://aclanthology.org/W04-1013/">ROUGE-1</a>
 
208
  </label>
209
 
210
  <label for="checkbox-rouge2">
211
- <input type="checkbox" id="checkbox-rouge2" name="checkbox-rouge2"> <a href="https://aclanthology.org/W04-1013/">ROUGE-2</a>
 
212
  </label>
213
 
214
  <label for="checkbox-rougeL">
215
- <input type="checkbox" id="checkbox-rougeL" name="checkbox-rougeL"> <a href="https://aclanthology.org/W04-1013/">ROUGE-L</a>
216
- </label>
217
-
218
- <label for="checkbox-tf1">
219
- <input type="checkbox" id="checkbox-tf1" name="checkbox-tf1"> <a href="https://aclanthology.org/2020.acl-main.408/">TF1</a>
220
  </label>
221
  </div>
222
 
223
  <div style="text-align: center;">
224
- <button onclick="computeFunction()">Compute</button>
225
  </div>
226
- <div id="results-container" style="text-align: center; margin-top: 20px;"></div>
227
  </div>
228
  </body>
229
 
 
12
  var checkboxRouge1 = document.getElementById('checkbox-rouge1').checked;
13
  var checkboxRouge2 = document.getElementById('checkbox-rouge2').checked;
14
  var checkboxRougeL = document.getElementById('checkbox-rougeL').checked;
 
15
 
16
  $.ajax({
17
  type: 'POST',
 
24
  checkbox_rouge1: checkboxRouge1,
25
  checkbox_rouge2: checkboxRouge2,
26
  checkbox_rougeL: checkboxRougeL,
 
27
  csrfmiddlewaretoken: '{{ csrf_token }}'
28
  },
29
  success: function (data) {
 
45
  if (checkboxRougeL && 'rougeL' in data) {
46
  resultHTML += '<p>ROUGE-L: ' + data.rougeL + '</p>';
47
  }
 
 
 
48
 
49
  resultsContainer.innerHTML = resultHTML || '<p>No results available</p>';
50
  },
51
  error: function () {
52
+ alert('Parsing failed. Please check your inputs format.');
53
  }
54
  });
55
  }
 
88
  function handleDragOver(event) {
89
  event.preventDefault();
90
  }
91
+
92
+ function checkIfCanCompute() {
93
+ var anyCheckbox = !$("input:checkbox:checked").length > 0;
94
+ var references = document.getElementById("references").value;
95
+ var predictions = document.getElementById("predictions").value;
96
+
97
+ document.getElementById("compute-button").disabled = anyCheckbox;
98
+ }
99
+
100
+ $(document).ready(function () {
101
+ $('input[type=checkbox]').change(
102
+ function () {
103
+ checkIfCanCompute();
104
+ });
105
+
106
+ $('textarea').change(
107
+ function () {
108
+ checkIfCanCompute();
109
+ });
110
+ });
111
+
112
  </script>
113
 
114
  <head>
115
  <meta charset="UTF-8">
116
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
117
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
118
+ <title>BLEU and ROUGE</title>
119
  <style>
120
  body {
121
+ font-family: 'Arial', sans-serif;
122
+ background-color: #f8f8f8;
123
  margin: 0;
124
  display: flex;
125
  align-items: center;
 
128
  }
129
 
130
  #container {
131
+ background-color: #ffffff;
132
+ box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.1);
133
+ border-radius: 10px;
134
  width: 80%;
135
  max-width: 800px;
136
+ padding: 30px;
137
  box-sizing: border-box;
138
  }
139
 
140
  header {
141
  text-align: center;
142
+ margin-bottom: 30px;
143
  }
144
 
145
+ h1,
146
+ h2,
147
+ p {
148
+ color: #333333;
149
+ }
150
+
151
+ label,
152
+ a {
153
+ color: #4caf50;
154
  }
155
 
156
  textarea {
157
  width: 100%;
158
+ padding: 15px;
159
  box-sizing: border-box;
160
+ border: 1px solid #ddd;
161
+ border-radius: 6px;
162
  resize: vertical;
163
  }
164
 
165
+ .label-container,
166
+ .checkbox-container {
167
+ margin-bottom: 30px;
168
+ }
169
+
170
  .checkbox-container {
171
  text-align: center;
 
172
  }
173
 
174
+ label:hover,
175
+ a:hover {
176
+ text-decoration: underline;
177
  }
178
 
179
  button {
180
  background-color: #4caf50;
181
  color: #fff;
182
  border: none;
183
+ padding: 15px 30px;
184
  text-align: center;
185
  text-decoration: none;
186
  display: inline-block;
187
+ font-size: 18px;
188
+ border-radius: 5px;
189
  cursor: pointer;
190
  transition: background-color 0.3s;
191
  }
 
193
  button:hover {
194
  background-color: #45a049;
195
  }
196
+
197
+ button:disabled {
198
+ background-color: #81a683;
199
+ cursor: not-allowed;
200
+ }
201
+
202
+ #results-container {
203
+ text-align: center;
204
+ margin-top: 30px;
205
+ }
206
  </style>
207
  </head>
208
 
 
210
  <div id="container">
211
  <header>
212
  <h1>How faithful are your predictions?</h1>
213
+ <p>You can write, drag-and-drop or upload your references and predictions. <br> Always quote your inputs and
214
+ separate them with a comma. <br> When multiple references for the same prediction, use a list.</p>
215
  </header>
216
 
217
  <div class="label-container" ondrop="handleFileDrop(event, 'references')" ondragover="handleDragOver(event)">
218
  <label for="references">References</label>
219
+ <textarea id="references" rows="5"
220
+ placeholder="&quot;The quick brown fox jumps over the lazy dog&quot;, [&quot;The cat in the hat is wearing a red bow&quot;, &quot;A feline in headwear sports a crimson ribbon&quot;], &quot;The sun sets behind the mountains, casting a warm glow&quot;"
221
+ draggable="true" ondrop="handleFileDrop(event, 'references')"
222
+ ondragover="handleDragOver(event)"></textarea>
223
  <input type="file" id="file-references" accept=".txt"
224
  onchange="handleFileUpload('file-references', 'references')">
225
  </div>
226
 
227
  <div class="label-container" ondrop="handleFileDrop(event, 'predictions')" ondragover="handleDragOver(event)">
228
  <label for="predictions">Predictions</label>
229
+ <textarea id="predictions" rows="5"
230
+ placeholder="&quot;A speedy brown fox jumps over the lazy dog&quot;, &quot;A cat with a hat wears a red bow&quot;, &quot;The sun sets, casting a warm glow behind the mountains&quot;"
231
+ draggable="true" ondrop="handleFileDrop(event, 'predictions')"
232
+ ondragover="handleDragOver(event)"></textarea>
233
  <input type="file" id="file-predictions" accept=".txt"
234
  onchange="handleFileUpload('file-predictions', 'predictions')">
235
  </div>
236
 
237
  <div class="checkbox-container">
238
  <label for="checkbox-sentence-bleu">
239
+ <input type="checkbox" id="checkbox-sentence-bleu" name="checkbox-sentence-bleu"> <a
240
+ href="https://aclanthology.org/P02-1040/">Sentence BLEU</a>
241
  </label>
242
 
243
  <label for="checkbox-corpus-bleu">
244
+ <input type="checkbox" id="checkbox-corpus-bleu" name="checkbox-corpus-bleu"> <a
245
+ href="https://aclanthology.org/P02-1040/">Corpus BLEU</a>
246
  </label>
247
 
248
  <label for="checkbox-rouge1">
249
+ <input type="checkbox" id="checkbox-rouge1" name="checkbox-rouge1"> <a
250
+ href="https://aclanthology.org/W04-1013/">ROUGE-1</a>
251
  </label>
252
 
253
  <label for="checkbox-rouge2">
254
+ <input type="checkbox" id="checkbox-rouge2" name="checkbox-rouge2"> <a
255
+ href="https://aclanthology.org/W04-1013/">ROUGE-2</a>
256
  </label>
257
 
258
  <label for="checkbox-rougeL">
259
+ <input type="checkbox" id="checkbox-rougeL" name="checkbox-rougeL"> <a
260
+ href="https://aclanthology.org/W04-1013/">ROUGE-L</a>
 
 
 
261
  </label>
262
  </div>
263
 
264
  <div style="text-align: center;">
265
+ <button id="compute-button" onclick="computeFunction()" disabled>Compute</button>
266
  </div>
267
+ <div id="results-container" style="text-align: center; margin-top: 30px;"></div>
268
  </div>
269
  </body>
270
 
src/web/__init__.py CHANGED
File without changes
src/web/settings.py CHANGED
@@ -25,7 +25,7 @@ SECRET_KEY = "django-insecure-*r6s+e#n%b*26gf($$x9g35=^)a2=9g+0i*$uukx1_%x)p)h@=
25
  # SECURITY WARNING: don't run with debug turned on in production!
26
  DEBUG = True
27
 
28
- ALLOWED_HOSTS = ['eliseobao-bleu-rouge-tf1.hf.space']
29
 
30
  X_FRAME_OPTIONS = 'ALLOW-FROM https://huggingface.co/'
31
 
 
25
  # SECURITY WARNING: don't run with debug turned on in production!
26
  DEBUG = True
27
 
28
+ ALLOWED_HOSTS = ['eliseobao-bleu-rouge-tf1.hf.space', 'localhost']
29
 
30
  X_FRAME_OPTIONS = 'ALLOW-FROM https://huggingface.co/'
31
 
src/web/tasks.py CHANGED
@@ -1,16 +1,17 @@
1
- import sacrebleu
2
  import statistics
3
 
 
4
  from rouge_score import rouge_scorer
5
 
6
 
7
  def sentence_bleu(references, predictions):
8
  scores = []
9
- for ref, pred in zip(references, predictions):
10
  scores.append(
11
  sacrebleu.sentence_bleu(
12
- pred,
13
- [ref],
14
  smooth_method="exp",
15
  smooth_value=0.0,
16
  lowercase=True,
@@ -24,7 +25,7 @@ def sentence_bleu(references, predictions):
24
  def corpus_bleu(references, predictions):
25
  return sacrebleu.corpus_bleu(
26
  predictions,
27
- [references],
28
  smooth_method="exp",
29
  smooth_value=0.0,
30
  lowercase=True,
@@ -35,14 +36,26 @@ def corpus_bleu(references, predictions):
35
  def rouge(scope, references, predictions):
36
  rouge = f"rouge{scope}"
37
  scorer = rouge_scorer.RougeScorer([rouge], use_stemmer=True)
 
38
 
39
  scores_dict = {"precision": 0, "recall": 0, "f1": 0}
40
 
41
- for reference, prediction in zip(references, predictions):
42
- scores = scorer.score(reference, prediction)
43
-
44
- scores_dict["precision"] += scores[rouge].precision
45
- scores_dict["recall"] += scores[rouge].recall
46
- scores_dict["f1"] += scores[rouge].fmeasure
 
 
 
47
 
48
  return {k: v / len(references) for k, v in scores_dict.items()}
 
 
 
 
 
 
 
 
 
1
+ import ast
2
  import statistics
3
 
4
+ import sacrebleu
5
  from rouge_score import rouge_scorer
6
 
7
 
8
  def sentence_bleu(references, predictions):
9
  scores = []
10
+ for reference, prediction in zip(references, predictions):
11
  scores.append(
12
  sacrebleu.sentence_bleu(
13
+ prediction,
14
+ reference if isinstance(reference, list) else [reference],
15
  smooth_method="exp",
16
  smooth_value=0.0,
17
  lowercase=True,
 
25
  def corpus_bleu(references, predictions):
26
  return sacrebleu.corpus_bleu(
27
  predictions,
28
+ references,
29
  smooth_method="exp",
30
  smooth_value=0.0,
31
  lowercase=True,
 
36
  def rouge(scope, references, predictions):
37
  rouge = f"rouge{scope}"
38
  scorer = rouge_scorer.RougeScorer([rouge], use_stemmer=True)
39
+ references = [r if isinstance(r,list) else [r] for r in references]
40
 
41
  scores_dict = {"precision": 0, "recall": 0, "f1": 0}
42
 
43
+ for refs, prediction in zip(references, predictions):
44
+ best_f1 = 0
45
+ for ref in refs:
46
+ scores = scorer.score(ref, prediction)
47
+ if scores[rouge].fmeasure > best_f1:
48
+ best_f1 = scores[rouge].fmeasure
49
+ scores_dict["precision"] += scores[rouge].precision
50
+ scores_dict["recall"] += scores[rouge].recall
51
+ scores_dict["f1"] += scores[rouge].fmeasure
52
 
53
  return {k: v / len(references) for k, v in scores_dict.items()}
54
+
55
+
56
+ def parse_string(string):
57
+ try:
58
+ return list(ast.literal_eval(string))
59
+ except (ValueError, SyntaxError) as e:
60
+ print(f"Error parsing string: {e}")
61
+ return None
src/web/urls.py CHANGED
File without changes
src/web/views.py CHANGED
@@ -1,10 +1,10 @@
1
- from . import tasks
2
-
3
  from django.http import JsonResponse
4
  from django.shortcuts import render
5
  from django.views.decorators.cache import cache_control
6
  from django.views.decorators.csrf import csrf_exempt
7
 
 
 
8
 
9
  @cache_control(no_cache=True, must_revalidate=True, no_store=True, max_age=0)
10
  def index(request):
@@ -13,32 +13,39 @@ def index(request):
13
 
14
  @csrf_exempt
15
  def compute(request):
16
-
17
  def _format_rouge_result(rouge):
18
- return "P: {:.1f} | R: {:.1f} | F1: {:.1f}".format(rouge['precision'], rouge['recall'], rouge['f1'])
 
 
19
 
20
  if request.method == "POST":
21
- references = list(filter(None, request.POST.get("references", "").split(",\n")))
22
- predictions = list(filter(None, request.POST.get("predictions", "").split(",\n")))
23
-
24
- print(references)
25
- print(predictions)
26
 
27
  result = {}
28
  if request.POST.get("checkbox_sentence_bleu", False) == "true":
29
- result["sentence_bleu"] = "{:.1f}".format(tasks.sentence_bleu(references, predictions))
 
 
30
  if request.POST.get("checkbox_corpus_bleu", False) == "true":
31
- result["corpus_bleu"] = "{:.1f}".format(tasks.corpus_bleu(references, predictions))
 
 
32
  if request.POST.get("checkbox_rouge1", False) == "true":
33
- result["rouge1"] = _format_rouge_result(tasks.rouge('1', references, predictions))
 
 
34
  if request.POST.get("checkbox_rouge2", False) == "true":
35
- result["rouge2"] = _format_rouge_result(tasks.rouge('2', references, predictions))
 
 
36
  if request.POST.get("checkbox_rougeL", False) == "true":
37
- result["rougeL"] = _format_rouge_result(tasks.rouge('L', references, predictions))
 
 
38
  if request.POST.get("checkbox_tf1", False) == "true":
39
- result["tf1"] = 0.90
40
 
41
- print(result)
42
  return JsonResponse(result)
43
 
44
  return JsonResponse({"error": "Invalid request method"})
 
 
 
1
  from django.http import JsonResponse
2
  from django.shortcuts import render
3
  from django.views.decorators.cache import cache_control
4
  from django.views.decorators.csrf import csrf_exempt
5
 
6
+ from . import tasks
7
+
8
 
9
  @cache_control(no_cache=True, must_revalidate=True, no_store=True, max_age=0)
10
  def index(request):
 
13
 
14
  @csrf_exempt
15
  def compute(request):
 
16
  def _format_rouge_result(rouge):
17
+ return "P: {:.1f} | R: {:.1f} | F1: {:.1f}".format(
18
+ rouge["precision"], rouge["recall"], rouge["f1"]
19
+ )
20
 
21
  if request.method == "POST":
22
+ references = tasks.parse_string(request.POST.get("references", ""))
23
+ predictions = tasks.parse_string(request.POST.get("predictions", ""))
 
 
 
24
 
25
  result = {}
26
  if request.POST.get("checkbox_sentence_bleu", False) == "true":
27
+ result["sentence_bleu"] = "{:.1f}".format(
28
+ tasks.sentence_bleu(references, predictions)
29
+ )
30
  if request.POST.get("checkbox_corpus_bleu", False) == "true":
31
+ result["corpus_bleu"] = "{:.1f}".format(
32
+ tasks.corpus_bleu(references, predictions)
33
+ )
34
  if request.POST.get("checkbox_rouge1", False) == "true":
35
+ result["rouge1"] = _format_rouge_result(
36
+ tasks.rouge("1", references, predictions)
37
+ )
38
  if request.POST.get("checkbox_rouge2", False) == "true":
39
+ result["rouge2"] = _format_rouge_result(
40
+ tasks.rouge("2", references, predictions)
41
+ )
42
  if request.POST.get("checkbox_rougeL", False) == "true":
43
+ result["rougeL"] = _format_rouge_result(
44
+ tasks.rouge("L", references, predictions)
45
+ )
46
  if request.POST.get("checkbox_tf1", False) == "true":
47
+ result["tf1"] = "{:.1f}".format(tasks.tf1(references, predictions))
48
 
 
49
  return JsonResponse(result)
50
 
51
  return JsonResponse({"error": "Invalid request method"})