Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,21 +3,30 @@ from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
|
| 3 |
import torch
|
| 4 |
import numpy as np
|
| 5 |
|
|
|
|
| 6 |
# Load model and tokenizer
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
inputs = tokenizer(
|
| 22 |
essay,
|
| 23 |
return_tensors="pt",
|
|
@@ -25,29 +34,73 @@ def score_essay(essay):
|
|
| 25 |
max_length=512
|
| 26 |
)
|
| 27 |
|
| 28 |
-
#
|
| 29 |
with torch.no_grad():
|
| 30 |
outputs = model(**inputs)
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
|
|
|
|
|
|
|
|
|
| 41 |
# Gradio UI
|
|
|
|
| 42 |
with gr.Blocks() as demo:
|
| 43 |
-
gr.Markdown("## Automated IELTS
|
| 44 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
score_output = gr.Label()
|
| 48 |
|
| 49 |
submit_btn = gr.Button("Score Essay")
|
| 50 |
-
submit_btn.click(fn=score_essay, inputs=essay_input, outputs=score_output)
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
demo.launch()
|
|
|
|
| 3 |
import torch
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
+
# -------------------------------
|
| 7 |
# Load model and tokenizer
|
| 8 |
+
# -------------------------------
|
| 9 |
+
MODEL_NAME = "JacobLinCool/IELTS_essay_scoring_safetensors"
|
| 10 |
+
|
| 11 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 13 |
+
|
| 14 |
+
model.eval()
|
| 15 |
+
|
| 16 |
+
# -------------------------------
|
| 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,
|
| 23 |
+
"Coherence & Cohesion": 0.0,
|
| 24 |
+
"Vocabulary": 0.0,
|
| 25 |
+
"Grammar": 0.0,
|
| 26 |
+
"Overall": 0.0,
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
# Tokenize input
|
| 30 |
inputs = tokenizer(
|
| 31 |
essay,
|
| 32 |
return_tensors="pt",
|
|
|
|
| 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",
|
| 66 |
+
"Coherence & Cohesion",
|
| 67 |
+
"Vocabulary",
|
| 68 |
+
"Grammar",
|
| 69 |
+
"Overall"
|
| 70 |
+
]
|
| 71 |
|
| 72 |
+
return {label: float(score) for label, score in zip(labels, bands)}
|
| 73 |
+
|
| 74 |
+
# -------------------------------
|
| 75 |
# Gradio UI
|
| 76 |
+
# -------------------------------
|
| 77 |
with gr.Blocks() as demo:
|
| 78 |
+
gr.Markdown("## 📝 Automated IELTS Writing Scorer")
|
| 79 |
+
gr.Markdown(
|
| 80 |
+
"Paste your IELTS Task 2 essay below. "
|
| 81 |
+
"The system will estimate band scores for all four criteria and the overall band."
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
essay_input = gr.Textbox(
|
| 85 |
+
lines=12,
|
| 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 |
|
| 93 |
+
submit_btn.click(
|
| 94 |
+
fn=score_essay,
|
| 95 |
+
inputs=essay_input,
|
| 96 |
+
outputs=score_output
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
gr.Markdown(
|
| 100 |
+
"⚠️ **Note:** This is an AI-based estimator, not an official IELTS examiner score."
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# -------------------------------
|
| 104 |
+
# Launch app
|
| 105 |
+
# -------------------------------
|
| 106 |
demo.launch()
|