Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
generator = pipeline("text-generation", model=model_name, device=-1) # CPU
|
| 7 |
|
| 8 |
-
# 🧠 Prompt template
|
| 9 |
def build_prompt(essay_text):
|
| 10 |
-
return f"""
|
| 11 |
-
|
| 12 |
-
Your task is to evaluate the following student essay in 6 categories, each scored from 0 to 100.
|
| 13 |
-
|
| 14 |
-
Categories:
|
| 15 |
1. Grammar & Mechanics
|
| 16 |
2. Coherence & Flow
|
| 17 |
3. Clarity & Style
|
|
@@ -19,31 +13,17 @@ Categories:
|
|
| 19 |
5. Structure & Organization
|
| 20 |
6. Teacher-Specific Style
|
| 21 |
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
Return only a JSON object like this:
|
| 26 |
-
{{
|
| 27 |
-
"Grammar & Mechanics": score,
|
| 28 |
-
"Coherence & Flow": score,
|
| 29 |
-
"Clarity & Style": score,
|
| 30 |
-
"Argument Strength & Evidence": score,
|
| 31 |
-
"Structure & Organization": score,
|
| 32 |
-
"Teacher-Specific Style": score,
|
| 33 |
-
"Total Grade": average
|
| 34 |
-
}}
|
| 35 |
-
|
| 36 |
-
Here is the essay:
|
| 37 |
{essay_text}
|
| 38 |
"""
|
| 39 |
|
| 40 |
-
# 📄 Optional file support (txt only)
|
| 41 |
def extract_text(file):
|
| 42 |
if file.name.endswith(".txt"):
|
| 43 |
return file.read().decode("utf-8")
|
| 44 |
return "Unsupported file type. Please upload a .txt file."
|
| 45 |
|
| 46 |
-
# 🧪 Main evaluation function
|
| 47 |
def grade_essay(essay, file):
|
| 48 |
if not essay and not file:
|
| 49 |
return "Please provide either text or a file."
|
|
@@ -54,22 +34,15 @@ def grade_essay(essay, file):
|
|
| 54 |
return essay
|
| 55 |
|
| 56 |
prompt = build_prompt(essay)
|
| 57 |
-
result = generator(prompt, max_new_tokens=
|
| 58 |
-
|
| 59 |
-
# Extract only the JSON part
|
| 60 |
-
start = result.find("{")
|
| 61 |
-
end = result.rfind("}") + 1
|
| 62 |
-
if start != -1 and end != -1:
|
| 63 |
-
return result[start:end]
|
| 64 |
-
return "Model response couldn't be parsed."
|
| 65 |
|
| 66 |
-
# 🎨 Gradio Interface
|
| 67 |
with gr.Blocks() as demo:
|
| 68 |
gr.Markdown("# ✏️ MimicMark – AI Essay Evaluator")
|
| 69 |
-
gr.Markdown("Paste your essay or upload a `.txt` file. The AI will score it in 6 categories
|
| 70 |
|
| 71 |
with gr.Row():
|
| 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.Textbox(label="AI Evaluation Output", lines=15)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Use a small CPU-friendly model
|
| 5 |
+
generator = pipeline("text2text-generation", model="google/flan-t5-base", device=-1)
|
|
|
|
| 6 |
|
|
|
|
| 7 |
def build_prompt(essay_text):
|
| 8 |
+
return f"""Evaluate this essay based on the following categories (0–100):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
1. Grammar & Mechanics
|
| 10 |
2. Coherence & Flow
|
| 11 |
3. Clarity & Style
|
|
|
|
| 13 |
5. Structure & Organization
|
| 14 |
6. Teacher-Specific Style
|
| 15 |
|
| 16 |
+
Return scores as a JSON object, and include the average as 'Total Grade'.
|
| 17 |
|
| 18 |
+
Essay:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
{essay_text}
|
| 20 |
"""
|
| 21 |
|
|
|
|
| 22 |
def extract_text(file):
|
| 23 |
if file.name.endswith(".txt"):
|
| 24 |
return file.read().decode("utf-8")
|
| 25 |
return "Unsupported file type. Please upload a .txt file."
|
| 26 |
|
|
|
|
| 27 |
def grade_essay(essay, file):
|
| 28 |
if not essay and not file:
|
| 29 |
return "Please provide either text or a file."
|
|
|
|
| 34 |
return essay
|
| 35 |
|
| 36 |
prompt = build_prompt(essay)
|
| 37 |
+
result = generator(prompt, max_new_tokens=256)[0]["generated_text"]
|
| 38 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
|
|
|
| 40 |
with gr.Blocks() as demo:
|
| 41 |
gr.Markdown("# ✏️ MimicMark – AI Essay Evaluator")
|
| 42 |
+
gr.Markdown("Paste your essay or upload a `.txt` file. The AI will score it in 6 categories.")
|
| 43 |
|
| 44 |
with gr.Row():
|
| 45 |
+
essay_input = gr.Textbox(label="Paste Your Essay", lines=12)
|
| 46 |
file_input = gr.File(label="Or Upload a .txt File", file_types=[".txt"])
|
| 47 |
|
| 48 |
output = gr.Textbox(label="AI Evaluation Output", lines=15)
|