trial3cata app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,16 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
|
|
3 |
|
4 |
-
# β
|
5 |
generator = pipeline("text2text-generation", model="declare-lab/flan-alpaca-base", device=-1)
|
6 |
|
7 |
-
# π§
|
8 |
def build_prompt(essay_text):
|
9 |
return f"""
|
10 |
You are an English teacher.
|
11 |
|
12 |
-
|
13 |
-
Each should receive a score from 0 to 100:
|
14 |
|
15 |
1. Grammar & Mechanics
|
16 |
2. Coherence & Flow
|
@@ -19,30 +19,28 @@ Each should receive a score from 0 to 100:
|
|
19 |
5. Structure & Organization
|
20 |
6. Teacher-Specific Style
|
21 |
|
22 |
-
Then, calculate the average of
|
23 |
|
24 |
-
Respond ONLY
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
"Total Grade": 88.83
|
33 |
-
}}
|
34 |
|
35 |
Essay:
|
36 |
\"\"\"{essay_text}\"\"\"
|
37 |
"""
|
38 |
|
39 |
-
# π
|
40 |
def extract_text(file):
|
41 |
if file.name.endswith(".txt"):
|
42 |
return file.read().decode("utf-8")
|
43 |
return "Unsupported file type. Please upload a .txt file."
|
44 |
|
45 |
-
# π§ͺ
|
46 |
def grade_essay(essay, file):
|
47 |
if not essay and not file:
|
48 |
return "Please provide either text or a file."
|
@@ -55,13 +53,26 @@ def grade_essay(essay, file):
|
|
55 |
prompt = build_prompt(essay)
|
56 |
result = generator(prompt, max_new_tokens=128)[0]["generated_text"]
|
57 |
|
58 |
-
# Extract
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
# π¨ Gradio UI
|
67 |
with gr.Blocks() as demo:
|
@@ -72,7 +83,7 @@ with gr.Blocks() as demo:
|
|
72 |
essay_input = gr.Textbox(label="Paste Your Essay", lines=12)
|
73 |
file_input = gr.File(label="Or Upload a .txt File", file_types=[".txt"])
|
74 |
|
75 |
-
output = gr.
|
76 |
|
77 |
submit = gr.Button("Evaluate Essay")
|
78 |
submit.click(fn=grade_essay, inputs=[essay_input, file_input], outputs=output)
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
+
import re
|
4 |
|
5 |
+
# β
Use a small, instruction-following model that works on CPU
|
6 |
generator = pipeline("text2text-generation", model="declare-lab/flan-alpaca-base", device=-1)
|
7 |
|
8 |
+
# π§ Structured prompt
|
9 |
def build_prompt(essay_text):
|
10 |
return f"""
|
11 |
You are an English teacher.
|
12 |
|
13 |
+
Evaluate the essay below in 6 categories. For each category, give a score from 0 to 100:
|
|
|
14 |
|
15 |
1. Grammar & Mechanics
|
16 |
2. Coherence & Flow
|
|
|
19 |
5. Structure & Organization
|
20 |
6. Teacher-Specific Style
|
21 |
|
22 |
+
Then, calculate the average of these 6 scores and return it as "Total Grade".
|
23 |
|
24 |
+
Respond ONLY with this exact format:
|
25 |
+
Grammar & Mechanics: [score]
|
26 |
+
Coherence & Flow: [score]
|
27 |
+
Clarity & Style: [score]
|
28 |
+
Argument Strength & Evidence: [score]
|
29 |
+
Structure & Organization: [score]
|
30 |
+
Teacher-Specific Style: [score]
|
31 |
+
Total Grade: [average]
|
|
|
|
|
32 |
|
33 |
Essay:
|
34 |
\"\"\"{essay_text}\"\"\"
|
35 |
"""
|
36 |
|
37 |
+
# π Read .txt file input
|
38 |
def extract_text(file):
|
39 |
if file.name.endswith(".txt"):
|
40 |
return file.read().decode("utf-8")
|
41 |
return "Unsupported file type. Please upload a .txt file."
|
42 |
|
43 |
+
# π§ͺ Main logic
|
44 |
def grade_essay(essay, file):
|
45 |
if not essay and not file:
|
46 |
return "Please provide either text or a file."
|
|
|
53 |
prompt = build_prompt(essay)
|
54 |
result = generator(prompt, max_new_tokens=128)[0]["generated_text"]
|
55 |
|
56 |
+
# π Extract scores using regex even if not formatted as JSON
|
57 |
+
categories = [
|
58 |
+
"Grammar & Mechanics",
|
59 |
+
"Coherence & Flow",
|
60 |
+
"Clarity & Style",
|
61 |
+
"Argument Strength & Evidence",
|
62 |
+
"Structure & Organization",
|
63 |
+
"Teacher-Specific Style",
|
64 |
+
"Total Grade"
|
65 |
+
]
|
66 |
+
output = {}
|
67 |
+
for cat in categories:
|
68 |
+
pattern = f"{cat}\\s*[:=]\\s*(\\d+(\\.\\d+)?)"
|
69 |
+
match = re.search(pattern, result, re.IGNORECASE)
|
70 |
+
if match:
|
71 |
+
output[cat] = float(match.group(1))
|
72 |
+
else:
|
73 |
+
output[cat] = "Not found"
|
74 |
+
|
75 |
+
return output
|
76 |
|
77 |
# π¨ Gradio UI
|
78 |
with gr.Blocks() as demo:
|
|
|
83 |
essay_input = gr.Textbox(label="Paste Your Essay", lines=12)
|
84 |
file_input = gr.File(label="Or Upload a .txt File", file_types=[".txt"])
|
85 |
|
86 |
+
output = gr.JSON(label="Evaluation Results")
|
87 |
|
88 |
submit = gr.Button("Evaluate Essay")
|
89 |
submit.click(fn=grade_essay, inputs=[essay_input, file_input], outputs=output)
|