Try1 app.py
Browse files- Mimicapp.py +82 -0
Mimicapp.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Load model (replace with any instruct-style model hosted on Hugging Face Hub)
|
6 |
+
model_name = "mistralai/Mixtral-8x7B-Instruct-v0.1" # You can change this
|
7 |
+
generator = pipeline("text-generation", model=model_name, device=-1) # CPU only
|
8 |
+
|
9 |
+
# Prompt template
|
10 |
+
def build_prompt(essay_text):
|
11 |
+
return f"""You are an experienced English teacher.
|
12 |
+
|
13 |
+
Your task is to evaluate the following student essay in 6 categories, each scored from 0 to 100.
|
14 |
+
|
15 |
+
Categories:
|
16 |
+
1. Grammar & Mechanics
|
17 |
+
2. Coherence & Flow
|
18 |
+
3. Clarity & Style
|
19 |
+
4. Argument Strength & Evidence
|
20 |
+
5. Structure & Organization
|
21 |
+
6. Teacher-Specific Style
|
22 |
+
|
23 |
+
Then, calculate the average of the 6 scores as the Total Grade.
|
24 |
+
|
25 |
+
Output format:
|
26 |
+
Return only a JSON object like this:
|
27 |
+
{{
|
28 |
+
"Grammar & Mechanics": score,
|
29 |
+
"Coherence & Flow": score,
|
30 |
+
"Clarity & Style": score,
|
31 |
+
"Argument Strength & Evidence": score,
|
32 |
+
"Structure & Organization": score,
|
33 |
+
"Teacher-Specific Style": score,
|
34 |
+
"Total Grade": average
|
35 |
+
}}
|
36 |
+
|
37 |
+
Here is the essay:
|
38 |
+
{essay_text}
|
39 |
+
"""
|
40 |
+
|
41 |
+
# Text extractor for uploaded files
|
42 |
+
def extract_text(file):
|
43 |
+
if file.name.endswith(".txt"):
|
44 |
+
return file.read().decode("utf-8")
|
45 |
+
return "Unsupported file type. Please upload a .txt file."
|
46 |
+
|
47 |
+
# Main function
|
48 |
+
def grade_essay(essay, file):
|
49 |
+
if not essay and not file:
|
50 |
+
return "Please provide either text or a file."
|
51 |
+
|
52 |
+
if file:
|
53 |
+
essay = extract_text(file)
|
54 |
+
if "Unsupported" in essay:
|
55 |
+
return essay
|
56 |
+
|
57 |
+
prompt = build_prompt(essay)
|
58 |
+
result = generator(prompt, max_new_tokens=400, temperature=0.5)[0]["generated_text"]
|
59 |
+
|
60 |
+
# Extract JSON from result
|
61 |
+
start = result.find("{")
|
62 |
+
end = result.rfind("}") + 1
|
63 |
+
try:
|
64 |
+
return result[start:end]
|
65 |
+
except:
|
66 |
+
return "Model response couldn't be parsed."
|
67 |
+
|
68 |
+
# UI
|
69 |
+
with gr.Blocks() as demo:
|
70 |
+
gr.Markdown("# ✏️ MimicMark - Essay Evaluator")
|
71 |
+
gr.Markdown("Upload or paste your essay. The AI will rate it in 6 categories and return a final score.")
|
72 |
+
|
73 |
+
with gr.Row():
|
74 |
+
essay_input = gr.Textbox(label="Paste Your Essay Here", lines=12)
|
75 |
+
file_input = gr.File(label="Or Upload a .txt File", file_types=[".txt"])
|
76 |
+
|
77 |
+
output = gr.Textbox(label="Evaluation Output", lines=15)
|
78 |
+
|
79 |
+
btn = gr.Button("Evaluate Essay")
|
80 |
+
btn.click(fn=grade_essay, inputs=[essay_input, file_input], outputs=output)
|
81 |
+
|
82 |
+
demo.launch()
|