Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# ----------------------------
|
| 6 |
+
# Model setup (lightweight & free)
|
| 7 |
+
# ----------------------------
|
| 8 |
+
MODEL_ID = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 9 |
+
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 11 |
+
|
| 12 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
+
MODEL_ID,
|
| 14 |
+
dtype=torch.float32,
|
| 15 |
+
device_map="auto"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
pipe = pipeline(
|
| 19 |
+
"text-generation",
|
| 20 |
+
model=model,
|
| 21 |
+
tokenizer=tokenizer
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# ----------------------------
|
| 25 |
+
# IELTS Scoring Function
|
| 26 |
+
# ----------------------------
|
| 27 |
+
def score_ielts_writing(text):
|
| 28 |
+
prompt = f"""
|
| 29 |
+
You are a certified IELTS Writing examiner.
|
| 30 |
+
|
| 31 |
+
Ignore the essay content in your output.
|
| 32 |
+
Score the essay strictly using IELTS band descriptors.
|
| 33 |
+
|
| 34 |
+
Output ONLY the final scores in JSON format.
|
| 35 |
+
Do NOT repeat the essay.
|
| 36 |
+
|
| 37 |
+
Score these criteria:
|
| 38 |
+
- Task Response (TR)
|
| 39 |
+
- Coherence and Cohesion (CC)
|
| 40 |
+
- Lexical Resource (LR)
|
| 41 |
+
- Grammatical Range and Accuracy (GRA)
|
| 42 |
+
|
| 43 |
+
MANDATORY JSON FORMAT:
|
| 44 |
+
{{
|
| 45 |
+
"task_response": 0.0,
|
| 46 |
+
"coherence_cohesion": 0.0,
|
| 47 |
+
"lexical_resource": 0.0,
|
| 48 |
+
"grammatical_range_accuracy": 0.0,
|
| 49 |
+
"overall_band": 0.0,
|
| 50 |
+
"feedback": "2–3 sentence examiner feedback"
|
| 51 |
+
}}
|
| 52 |
+
|
| 53 |
+
ESSAY (QUESTION + ESSAY):
|
| 54 |
+
{text}
|
| 55 |
+
|
| 56 |
+
Now, output ONLY the JSON scores as above.
|
| 57 |
+
"""
|
| 58 |
+
|
| 59 |
+
output = pipe(
|
| 60 |
+
prompt,
|
| 61 |
+
max_new_tokens=450, # enough for 300-word essay + JSON
|
| 62 |
+
temperature=0.3, # deterministic scoring
|
| 63 |
+
do_sample=False,
|
| 64 |
+
repetition_penalty=1.05
|
| 65 |
+
)[0]["generated_text"]
|
| 66 |
+
|
| 67 |
+
# remove the prompt part from the output
|
| 68 |
+
return output[len(prompt):].strip()
|
| 69 |
+
|
| 70 |
+
# ----------------------------
|
| 71 |
+
# Gradio UI
|
| 72 |
+
# ----------------------------
|
| 73 |
+
iface = gr.Interface(
|
| 74 |
+
fn=score_ielts_writing,
|
| 75 |
+
inputs=gr.Textbox(
|
| 76 |
+
lines=18,
|
| 77 |
+
label="Paste IELTS QUESTION + ESSAY",
|
| 78 |
+
placeholder="QUESTION:\n...\n\nESSAY:\n..."
|
| 79 |
+
),
|
| 80 |
+
outputs=gr.Textbox(label="IELTS Band Scores (JSON)"),
|
| 81 |
+
title="IELTS Writing AI Scorer (Free • CPU • Fast)",
|
| 82 |
+
description="Paste the IELTS Writing Task 2 question followed by the essay. Returns band scores + examiner feedback."
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
iface.launch()
|
| 86 |
+
|