Spaces:
Running
on
Zero
Running
on
Zero
JacobLinCool
commited on
Commit
•
2e6bf26
1
Parent(s):
5a8a1bf
feat: grader info
Browse files- app.py +13 -9
- model/Engessay_grading_ML.py +27 -22
- model/IELTS_essay_scoring.py +28 -23
- model/common.py +13 -0
- model/mistral_7b_ielts_evaluator.py +15 -11
app.py
CHANGED
@@ -1,13 +1,14 @@
|
|
1 |
from typing import *
|
2 |
import gradio as gr
|
3 |
-
from model.
|
4 |
-
from model.
|
5 |
-
from model.
|
|
|
6 |
|
7 |
-
models = {
|
8 |
-
"IELTS_essay_scoring":
|
9 |
-
"Engessay_grading_ML":
|
10 |
-
"mistral_7b_ielts_evaluator":
|
11 |
}
|
12 |
|
13 |
|
@@ -20,7 +21,7 @@ def grade(question: str, answer: str, model: str) -> Tuple[float, str]:
|
|
20 |
raise gr.Error(f"Model {model} not found")
|
21 |
|
22 |
grader = models[model]
|
23 |
-
return grader(question, answer)
|
24 |
|
25 |
|
26 |
with gr.Blocks() as app:
|
@@ -46,7 +47,7 @@ with gr.Blocks() as app:
|
|
46 |
btn = gr.Button("Grade Essay", variant="primary")
|
47 |
with gr.Column():
|
48 |
overall = gr.Number(label="Overall Score")
|
49 |
-
comment = gr.
|
50 |
|
51 |
btn.click(
|
52 |
fn=grade,
|
@@ -79,4 +80,7 @@ with gr.Blocks() as app:
|
|
79 |
inputs=[question, essay],
|
80 |
)
|
81 |
|
|
|
|
|
|
|
82 |
app.launch()
|
|
|
1 |
from typing import *
|
2 |
import gradio as gr
|
3 |
+
from model.common import Grader
|
4 |
+
from model.IELTS_essay_scoring import IELTS_essay_scoring
|
5 |
+
from model.Engessay_grading_ML import Engessay_grading_ML
|
6 |
+
from model.mistral_7b_ielts_evaluator import Mistral_7b_IELTS_Evaluator
|
7 |
|
8 |
+
models: Dict[str, Grader] = {
|
9 |
+
"IELTS_essay_scoring": IELTS_essay_scoring(),
|
10 |
+
"Engessay_grading_ML": Engessay_grading_ML(),
|
11 |
+
"mistral_7b_ielts_evaluator": Mistral_7b_IELTS_Evaluator(),
|
12 |
}
|
13 |
|
14 |
|
|
|
21 |
raise gr.Error(f"Model {model} not found")
|
22 |
|
23 |
grader = models[model]
|
24 |
+
return grader.grade(question, answer)
|
25 |
|
26 |
|
27 |
with gr.Blocks() as app:
|
|
|
47 |
btn = gr.Button("Grade Essay", variant="primary")
|
48 |
with gr.Column():
|
49 |
overall = gr.Number(label="Overall Score")
|
50 |
+
comment = gr.Markdown(label="Comment")
|
51 |
|
52 |
btn.click(
|
53 |
fn=grade,
|
|
|
80 |
inputs=[question, essay],
|
81 |
)
|
82 |
|
83 |
+
info = "\n\n".join([f"### {k}\n{grader.info()}" for k, grader in models.items()])
|
84 |
+
gr.Markdown(info)
|
85 |
+
|
86 |
app.launch()
|
model/Engessay_grading_ML.py
CHANGED
@@ -1,37 +1,42 @@
|
|
1 |
from typing import *
|
2 |
import torch
|
3 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
|
|
4 |
|
5 |
model_name = "KevSun/Engessay_grading_ML"
|
6 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
|
9 |
|
10 |
-
|
11 |
-
def
|
12 |
-
|
13 |
|
14 |
-
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
predictions = outputs.logits.squeeze()
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
rounded_scores = [round(score * 2) / 2 for score in scaled_scores]
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
"vocabulary",
|
27 |
-
"phraseology",
|
28 |
-
"grammar",
|
29 |
-
"conventions",
|
30 |
-
]
|
31 |
-
overall_score = round(sum(rounded_scores) / len(rounded_scores) * 2) / 2
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
|
|
|
|
|
|
|
|
|
1 |
from typing import *
|
2 |
import torch
|
3 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
4 |
+
from .common import Grader
|
5 |
|
6 |
model_name = "KevSun/Engessay_grading_ML"
|
7 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
|
10 |
|
11 |
+
class Engessay_grading_ML(Grader):
|
12 |
+
def info(self) -> str:
|
13 |
+
return "[KevSun/Engessay_grading_ML](https://huggingface.co/KevSun/Engessay_grading_ML)"
|
14 |
|
15 |
+
@torch.no_grad()
|
16 |
+
def grade(self, question: str, answer: str) -> Tuple[float, str]:
|
17 |
+
text = f"{question} {answer}"
|
18 |
|
19 |
+
inputs = tokenizer(text, return_tensors="pt")
|
|
|
20 |
|
21 |
+
outputs = model(**inputs)
|
22 |
+
predictions = outputs.logits.squeeze()
|
|
|
23 |
|
24 |
+
predicted_scores = predictions.numpy()
|
25 |
+
scaled_scores = 2.25 * predicted_scores - 1.25
|
26 |
+
rounded_scores = [round(score * 2) / 2 for score in scaled_scores]
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
labels = [
|
29 |
+
"cohesion",
|
30 |
+
"syntax",
|
31 |
+
"vocabulary",
|
32 |
+
"phraseology",
|
33 |
+
"grammar",
|
34 |
+
"conventions",
|
35 |
+
]
|
36 |
+
overall_score = round(sum(rounded_scores) / len(rounded_scores) * 2) / 2
|
37 |
|
38 |
+
comment = ""
|
39 |
+
for label, score in zip(labels, rounded_scores):
|
40 |
+
comment += f"{label}: {score}\n"
|
41 |
+
|
42 |
+
return overall_score, comment
|
model/IELTS_essay_scoring.py
CHANGED
@@ -2,38 +2,43 @@ from typing import *
|
|
2 |
import torch
|
3 |
import numpy as np
|
4 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
|
|
5 |
|
6 |
model_name = "JacobLinCool/IELTS_essay_scoring_safetensors"
|
7 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
|
10 |
|
11 |
-
|
12 |
-
def
|
13 |
-
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
|
19 |
-
|
20 |
-
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
rounded_scores = np.round(normalized_scores * 2) / 2
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
"Vocabulary",
|
30 |
-
"Grammar",
|
31 |
-
"Overall",
|
32 |
-
]
|
33 |
-
overall_score = float(rounded_scores[-1])
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
|
|
|
|
|
|
|
|
|
2 |
import torch
|
3 |
import numpy as np
|
4 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
5 |
+
from .common import Grader
|
6 |
|
7 |
model_name = "JacobLinCool/IELTS_essay_scoring_safetensors"
|
8 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
|
11 |
|
12 |
+
class IELTS_essay_scoring(Grader):
|
13 |
+
def info(self) -> str:
|
14 |
+
return "Safetensors version of [KevSun/IELTS_essay_scoring](https://huggingface.co/KevSun/IELTS_essay_scoring)"
|
15 |
|
16 |
+
@torch.no_grad()
|
17 |
+
def grade(self, question: str, answer: str) -> Tuple[float, str]:
|
18 |
+
text = f"{question} {answer}"
|
19 |
|
20 |
+
inputs = tokenizer(
|
21 |
+
text, return_tensors="pt", padding=True, truncation=True, max_length=512
|
22 |
+
)
|
23 |
|
24 |
+
outputs = model(**inputs)
|
25 |
+
predictions = outputs.logits.squeeze()
|
|
|
26 |
|
27 |
+
predicted_scores = predictions.numpy()
|
28 |
+
normalized_scores = (predicted_scores / predicted_scores.max()) * 9
|
29 |
+
rounded_scores = np.round(normalized_scores * 2) / 2
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
labels = [
|
32 |
+
"Task Achievement",
|
33 |
+
"Coherence and Cohesion",
|
34 |
+
"Vocabulary",
|
35 |
+
"Grammar",
|
36 |
+
"Overall",
|
37 |
+
]
|
38 |
+
overall_score = float(rounded_scores[-1])
|
39 |
|
40 |
+
comment = ""
|
41 |
+
for label, score in zip(labels, rounded_scores):
|
42 |
+
comment += f"{label}: {score}\n"
|
43 |
+
|
44 |
+
return overall_score, comment
|
model/common.py
CHANGED
@@ -1,4 +1,17 @@
|
|
|
|
|
|
1 |
from accelerate import Accelerator
|
2 |
|
3 |
accelerator = Accelerator()
|
4 |
device = accelerator.device
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Tuple
|
2 |
+
import spaces
|
3 |
from accelerate import Accelerator
|
4 |
|
5 |
accelerator = Accelerator()
|
6 |
device = accelerator.device
|
7 |
+
|
8 |
+
|
9 |
+
class Grader:
|
10 |
+
def info(self) -> str:
|
11 |
+
raise NotImplementedError("Method info not implemented")
|
12 |
+
|
13 |
+
def grade(self, question: str, answer: str) -> Tuple[float, str]:
|
14 |
+
raise NotImplementedError("Method grade not implemented")
|
15 |
+
|
16 |
+
def __call__(self, question: str, answer: str) -> Tuple[float, str]:
|
17 |
+
return self.grade(question, answer)
|
model/mistral_7b_ielts_evaluator.py
CHANGED
@@ -2,7 +2,7 @@ from typing import *
|
|
2 |
import torch
|
3 |
from transformers import pipeline
|
4 |
import spaces
|
5 |
-
from .common import device
|
6 |
|
7 |
model_name = "JacobLinCool/mistral-7b-ielts-evaluator-safetensors"
|
8 |
pipe = pipeline("text-generation", model_name, device=device)
|
@@ -44,10 +44,14 @@ system = """In this task, you are required to evaluate an IELTS Writing Task 2 e
|
|
44 |
- Suggest strategies for enhancement in weaker areas."""
|
45 |
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
51 |
|
52 |
## Prompt:
|
53 |
{question}
|
@@ -58,10 +62,10 @@ def grade_mistral_7b_ielts_evaluator(question: str, answer: str) -> Tuple[float,
|
|
58 |
## Evaluation:
|
59 |
"""
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
|
67 |
-
|
|
|
2 |
import torch
|
3 |
from transformers import pipeline
|
4 |
import spaces
|
5 |
+
from .common import Grader, device
|
6 |
|
7 |
model_name = "JacobLinCool/mistral-7b-ielts-evaluator-safetensors"
|
8 |
pipe = pipeline("text-generation", model_name, device=device)
|
|
|
44 |
- Suggest strategies for enhancement in weaker areas."""
|
45 |
|
46 |
|
47 |
+
class Mistral_7b_IELTS_Evaluator(Grader):
|
48 |
+
def info(self) -> str:
|
49 |
+
return "Safetensors version of [chillies/mistral-7b-ielts-evaluator-q4](https://huggingface.co/chillies/mistral-7b-ielts-evaluator-q4)"
|
50 |
+
|
51 |
+
@spaces.GPU(duration=120)
|
52 |
+
@torch.no_grad()
|
53 |
+
def grade(self, question: str, answer: str) -> Tuple[float, str]:
|
54 |
+
text = f"""{system}
|
55 |
|
56 |
## Prompt:
|
57 |
{question}
|
|
|
62 |
## Evaluation:
|
63 |
"""
|
64 |
|
65 |
+
outputs = pipe(text, max_length=2048)
|
66 |
+
comment = outputs[0]["generated_text"].split("## Evaluation:")[1].strip()
|
67 |
+
overall_score = float(
|
68 |
+
comment.split("Suggested Overall Band Score: ")[1].split("\n")[0] or 0.0
|
69 |
+
)
|
70 |
|
71 |
+
return overall_score, comment
|