fixedcatagory app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
#
|
| 5 |
generator = pipeline("text2text-generation", model="google/flan-t5-base", device=-1)
|
| 6 |
|
|
|
|
| 7 |
def build_prompt(essay_text):
|
| 8 |
-
return f"""
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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."
|
|
@@ -35,11 +53,19 @@ def grade_essay(essay, file):
|
|
| 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)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# ✅ Small, CPU-friendly model
|
| 5 |
generator = pipeline("text2text-generation", model="google/flan-t5-base", device=-1)
|
| 6 |
|
| 7 |
+
# 🧠 Stronger, structured prompt
|
| 8 |
def build_prompt(essay_text):
|
| 9 |
+
return f"""
|
| 10 |
+
You are an experienced English teacher.
|
| 11 |
+
|
| 12 |
+
Evaluate the following student essay in **6 categories**, giving a score from 0 to 100 for each:
|
| 13 |
+
|
| 14 |
+
1. Grammar & Mechanics
|
| 15 |
+
2. Coherence & Flow
|
| 16 |
+
3. Clarity & Style
|
| 17 |
+
4. Argument Strength & Evidence
|
| 18 |
+
5. Structure & Organization
|
| 19 |
+
6. Teacher-Specific Style
|
| 20 |
+
|
| 21 |
+
Then calculate the average of those 6 scores and return it as **"Total Grade"**.
|
| 22 |
+
|
| 23 |
+
⚠️ Format your response **only** as JSON like this:
|
| 24 |
+
{{
|
| 25 |
+
"Grammar & Mechanics": 87,
|
| 26 |
+
"Coherence & Flow": 92,
|
| 27 |
+
"Clarity & Style": 85,
|
| 28 |
+
"Argument Strength & Evidence": 90,
|
| 29 |
+
"Structure & Organization": 88,
|
| 30 |
+
"Teacher-Specific Style": 91,
|
| 31 |
+
"Total Grade": 88.83
|
| 32 |
+
}}
|
| 33 |
+
|
| 34 |
+
Here is the essay:
|
| 35 |
+
\"\"\"{essay_text}\"\"\"
|
| 36 |
"""
|
| 37 |
|
| 38 |
+
# 📄 Optional file input
|
| 39 |
def extract_text(file):
|
| 40 |
if file.name.endswith(".txt"):
|
| 41 |
return file.read().decode("utf-8")
|
| 42 |
return "Unsupported file type. Please upload a .txt file."
|
| 43 |
|
| 44 |
+
# 💬 Main grading logic
|
| 45 |
def grade_essay(essay, file):
|
| 46 |
if not essay and not file:
|
| 47 |
return "Please provide either text or a file."
|
|
|
|
| 53 |
|
| 54 |
prompt = build_prompt(essay)
|
| 55 |
result = generator(prompt, max_new_tokens=256)[0]["generated_text"]
|
|
|
|
| 56 |
|
| 57 |
+
# Extract just the JSON if the model follows the prompt
|
| 58 |
+
start = result.find("{")
|
| 59 |
+
end = result.rfind("}") + 1
|
| 60 |
+
if start != -1 and end != -1:
|
| 61 |
+
return result[start:end]
|
| 62 |
+
|
| 63 |
+
return "Model response could not be parsed as JSON."
|
| 64 |
+
|
| 65 |
+
# 🎨 Gradio UI
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
gr.Markdown("# ✏️ MimicMark – AI Essay Evaluator")
|
| 68 |
+
gr.Markdown("Paste your essay or upload a `.txt` file. The AI will score it in 6 categories and return a final numeric grade.")
|
| 69 |
|
| 70 |
with gr.Row():
|
| 71 |
essay_input = gr.Textbox(label="Paste Your Essay", lines=12)
|