YUVALON commited on
Commit
4ae8173
Β·
verified Β·
1 Parent(s): 81f7da9

trial3cata app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -25
app.py CHANGED
@@ -1,16 +1,16 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
- # βœ… Updated to better CPU-compatible instruction-following model
5
  generator = pipeline("text2text-generation", model="declare-lab/flan-alpaca-base", device=-1)
6
 
7
- # 🧠 Strong prompt with JSON enforcement
8
  def build_prompt(essay_text):
9
  return f"""
10
  You are an English teacher.
11
 
12
- Please evaluate the following student essay across these 6 categories.
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 all 6 and return it as "Total Grade".
23
 
24
- Respond ONLY in this exact JSON format:
25
- {{
26
- "Grammar & Mechanics": 87,
27
- "Coherence & Flow": 92,
28
- "Clarity & Style": 85,
29
- "Argument Strength & Evidence": 90,
30
- "Structure & Organization": 88,
31
- "Teacher-Specific Style": 91,
32
- "Total Grade": 88.83
33
- }}
34
 
35
  Essay:
36
  \"\"\"{essay_text}\"\"\"
37
  """
38
 
39
- # πŸ“„ Optional file input (.txt only)
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
- # πŸ§ͺ Essay grading logic
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 only JSON part if present
59
- start = result.find("{")
60
- end = result.rfind("}") + 1
61
- if start != -1 and end != -1:
62
- return result[start:end]
63
-
64
- return "Model response could not be parsed. Try rephrasing your essay or shortening the input."
 
 
 
 
 
 
 
 
 
 
 
 
 
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.Textbox(label="AI Evaluation Output", lines=15)
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)