Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,28 +4,50 @@ import torch
|
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
# Load model and tokenizer
|
| 7 |
-
model_name = "
|
| 8 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
|
| 11 |
# Prediction function
|
| 12 |
def score_essay(essay):
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
with torch.no_grad():
|
| 15 |
outputs = model(**inputs)
|
|
|
|
|
|
|
| 16 |
preds = outputs.logits.squeeze().numpy()
|
| 17 |
normalized = (preds / preds.max()) * 9
|
| 18 |
rounded = np.round(normalized * 2) / 2
|
|
|
|
|
|
|
| 19 |
labels = ["Task Achievement", "Coherence & Cohesion", "Vocabulary", "Grammar", "Overall"]
|
| 20 |
return {label: float(score) for label, score in zip(labels, rounded)}
|
| 21 |
|
| 22 |
# Gradio UI
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
# Load model and tokenizer
|
| 7 |
+
model_name = "JacobLinCool/IELTS_essay_scoring_safetensors"
|
| 8 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
|
| 11 |
# Prediction function
|
| 12 |
def score_essay(essay):
|
| 13 |
+
if not essay.strip():
|
| 14 |
+
return {"Task Achievement": 0,
|
| 15 |
+
"Coherence & Cohesion": 0,
|
| 16 |
+
"Vocabulary": 0,
|
| 17 |
+
"Grammar": 0,
|
| 18 |
+
"Overall": 0}
|
| 19 |
+
|
| 20 |
+
# Tokenize and truncate to max 512 tokens
|
| 21 |
+
inputs = tokenizer(
|
| 22 |
+
essay,
|
| 23 |
+
return_tensors="pt",
|
| 24 |
+
truncation=True,
|
| 25 |
+
max_length=512
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Run inference
|
| 29 |
with torch.no_grad():
|
| 30 |
outputs = model(**inputs)
|
| 31 |
+
|
| 32 |
+
# Extract logits and normalize to 9
|
| 33 |
preds = outputs.logits.squeeze().numpy()
|
| 34 |
normalized = (preds / preds.max()) * 9
|
| 35 |
rounded = np.round(normalized * 2) / 2
|
| 36 |
+
|
| 37 |
+
# Map labels
|
| 38 |
labels = ["Task Achievement", "Coherence & Cohesion", "Vocabulary", "Grammar", "Overall"]
|
| 39 |
return {label: float(score) for label, score in zip(labels, rounded)}
|
| 40 |
|
| 41 |
# Gradio UI
|
| 42 |
+
with gr.Blocks() as demo:
|
| 43 |
+
gr.Markdown("## Automated IELTS Essay Scoring")
|
| 44 |
+
gr.Markdown("Paste your essay below to get scores for all dimensions (Task, Coherence, Vocabulary, Grammar, Overall).")
|
| 45 |
+
|
| 46 |
+
essay_input = gr.Textbox(lines=10, placeholder="Paste your IELTS essay here...")
|
| 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 |
+
# Launch app (for HF Spaces, leave default)
|
| 53 |
+
demo.launch()
|