alphaprep commited on
Commit
71bcad8
·
verified ·
1 Parent(s): 43197d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -18
app.py CHANGED
@@ -17,6 +17,7 @@ model.eval()
17
  # Scoring function
18
  # -------------------------------
19
  def score_essay(essay: str):
 
20
  if not essay or not essay.strip():
21
  return {
22
  "Task Achievement": 0.0,
@@ -26,7 +27,7 @@ def score_essay(essay: str):
26
  "Overall": 0.0,
27
  }
28
 
29
- # Tokenize input
30
  inputs = tokenizer(
31
  essay,
32
  return_tensors="pt",
@@ -34,32 +35,30 @@ def score_essay(essay: str):
34
  max_length=512
35
  )
36
 
37
- # Inference
38
  with torch.no_grad():
39
  outputs = model(**inputs)
40
 
41
- # Raw logits from model
42
- raw_scores = outputs.logits.squeeze().cpu().numpy()
43
 
44
  # -------------------------------
45
- # IELTS calibration (IMPORTANT)
46
  # -------------------------------
47
- # Convert logits IELTS band scale
48
- bands = 0.75 * raw_scores + 5.0
49
 
50
- # Length penalty (IELTS-like)
51
- word_count = len(essay.split())
52
 
 
 
53
  if word_count < 150:
54
- bands -= 1.0
55
  elif word_count < 250:
56
- bands -= 0.5
57
 
58
- # Clamp to valid IELTS range
59
- bands = np.clip(bands, 0.0, 9.0)
60
-
61
- # Round to nearest 0.5
62
- bands = np.round(bands * 2) / 2
63
 
64
  labels = [
65
  "Task Achievement",
@@ -69,7 +68,7 @@ def score_essay(essay: str):
69
  "Overall"
70
  ]
71
 
72
- return {label: float(score) for label, score in zip(labels, bands)}
73
 
74
  # -------------------------------
75
  # Gradio UI
@@ -86,7 +85,7 @@ with gr.Blocks() as demo:
86
  placeholder="Paste your IELTS essay here..."
87
  )
88
 
89
- score_output = gr.Label(label="Estimated IELTS Band Scores")
90
 
91
  submit_btn = gr.Button("Score Essay")
92
 
 
17
  # Scoring function
18
  # -------------------------------
19
  def score_essay(essay: str):
20
+ # Empty essay → zero scores
21
  if not essay or not essay.strip():
22
  return {
23
  "Task Achievement": 0.0,
 
27
  "Overall": 0.0,
28
  }
29
 
30
+ # Tokenize input, truncate to 512 tokens
31
  inputs = tokenizer(
32
  essay,
33
  return_tensors="pt",
 
35
  max_length=512
36
  )
37
 
38
+ # Model inference
39
  with torch.no_grad():
40
  outputs = model(**inputs)
41
 
42
+ # Raw logits from the model
43
+ preds = outputs.logits.squeeze().cpu().numpy()
44
 
45
  # -------------------------------
46
+ # CORRECT CALIBRATION
47
  # -------------------------------
48
+ # Clip logits to 0-9 (IELTS band)
49
+ scores = np.clip(preds, 0, 9)
50
 
51
+ # Round to nearest 0.5
52
+ rounded = np.round(scores * 2) / 2
53
 
54
+ # Optional: Penalize very short essays (like IELTS examiners)
55
+ word_count = len(essay.split())
56
  if word_count < 150:
57
+ rounded -= 1.0
58
  elif word_count < 250:
59
+ rounded -= 0.5
60
 
61
+ rounded = np.clip(rounded, 0, 9)
 
 
 
 
62
 
63
  labels = [
64
  "Task Achievement",
 
68
  "Overall"
69
  ]
70
 
71
+ return {label: float(score) for label, score in zip(labels, rounded)}
72
 
73
  # -------------------------------
74
  # Gradio UI
 
85
  placeholder="Paste your IELTS essay here..."
86
  )
87
 
88
+ score_output = gr.JSON(label="Estimated IELTS Band Scores")
89
 
90
  submit_btn = gr.Button("Score Essay")
91