changcheng967 commited on
Commit
bd4258a
·
verified ·
1 Parent(s): 0ed9173

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -3
app.py CHANGED
@@ -76,7 +76,8 @@ class ProfessionalEssayGrader:
76
  self.grammar_model = pipeline(
77
  "text2text-generation",
78
  model="pszemraj/flan-t5-large-grammar-synthesis",
79
- device=-1
 
80
  )
81
  self.coherence_model = pipeline(
82
  "sentiment-analysis",
@@ -96,8 +97,29 @@ class ProfessionalEssayGrader:
96
  self.paraphrase_model = pipeline(
97
  "text2text-generation",
98
  model="Vamsi/T5_Paraphrase_Paws",
 
 
 
 
 
 
 
99
  device=-1
100
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  # Grammar checker fallback: if Java is missing, use basic regex-based grammar checks
102
  try:
103
  self.grammar_checker = language_tool_python.LanguageTool('en-US')
@@ -126,6 +148,16 @@ class ProfessionalEssayGrader:
126
  # Paraphrase (for feedback)
127
  paraphrase = self.paraphrase_model(f"paraphrase: {essay_text[:256]}")[0]['generated_text'] if len(essay_text) > 50 else "Essay too short for paraphrase."
128
 
 
 
 
 
 
 
 
 
 
 
129
  # Weighted overall score
130
  overall_score = self._calculate_enhanced_score(stats, readability, coherence, grammar, structure, content, style)
131
  overall_score = max(0, min(100, overall_score))
@@ -135,7 +167,7 @@ class ProfessionalEssayGrader:
135
 
136
  # Generate feedback
137
  feedback = self._generate_ontario_feedback(
138
- overall_score, rubric, stats, readability, coherence, grammar, structure, content, style, topic_results, summary, paraphrase
139
  )
140
 
141
  return {
@@ -145,6 +177,9 @@ class ProfessionalEssayGrader:
145
  "topic_detection": topic_results,
146
  "summary": summary,
147
  "paraphrase": paraphrase,
 
 
 
148
  "detailed_breakdown": {
149
  "enhanced_statistics": stats,
150
  "readability_analysis": readability,
@@ -167,7 +202,7 @@ class ProfessionalEssayGrader:
167
  }
168
  return ONTARIO_RUBRIC["R"]
169
 
170
- def _generate_ontario_feedback(self, score, rubric, stats, readability, coherence, grammar, structure, content, style, topic_results, summary, paraphrase) -> Dict:
171
  """Generate extremely detailed and specific feedback aligned with Ontario curriculum"""
172
 
173
  feedback = []
@@ -179,6 +214,9 @@ class ProfessionalEssayGrader:
179
  feedback.append(f"Summary: {summary}")
180
  feedback.append(f"Paraphrase: {paraphrase}")
181
  feedback.append(f"Topic Detection: {topic_results}")
 
 
 
182
  feedback.append(f"Statistics: {stats}")
183
  feedback.append(f"Readability: {readability}")
184
  feedback.append(f"Coherence: {coherence}")
@@ -191,6 +229,7 @@ class ProfessionalEssayGrader:
191
  feedback.append("- Use topic analysis to strengthen essay focus.")
192
  feedback.append("- Improve grammar and coherence as suggested.")
193
  feedback.append("- Expand vocabulary and sentence variety.")
 
194
 
195
  return feedback
196
 
 
76
  self.grammar_model = pipeline(
77
  "text2text-generation",
78
  model="pszemraj/flan-t5-large-grammar-synthesis",
79
+ device=-1,
80
+ use_fast=False
81
  )
82
  self.coherence_model = pipeline(
83
  "sentiment-analysis",
 
97
  self.paraphrase_model = pipeline(
98
  "text2text-generation",
99
  model="Vamsi/T5_Paraphrase_Paws",
100
+ device=-1,
101
+ tokenizer_kwargs={"use_fast": False}
102
+ )
103
+ # Add advanced readability model
104
+ self.readability_model = pipeline(
105
+ "text-classification",
106
+ model="nlptown/bert-base-multilingual-uncased-sentiment",
107
  device=-1
108
  )
109
+ # Add advanced grammar correction model
110
+ self.grammar_correction_model = pipeline(
111
+ "text2text-generation",
112
+ model="prithivida/grammar_error_correcter_v1",
113
+ device=-1,
114
+ use_fast=False
115
+ )
116
+ # Add advanced feedback generator
117
+ self.feedback_model = pipeline(
118
+ "text2text-generation",
119
+ model="google/flan-t5-large",
120
+ device=-1,
121
+ use_fast=False
122
+ )
123
  # Grammar checker fallback: if Java is missing, use basic regex-based grammar checks
124
  try:
125
  self.grammar_checker = language_tool_python.LanguageTool('en-US')
 
148
  # Paraphrase (for feedback)
149
  paraphrase = self.paraphrase_model(f"paraphrase: {essay_text[:256]}")[0]['generated_text'] if len(essay_text) > 50 else "Essay too short for paraphrase."
150
 
151
+ # Readability analysis
152
+ readability_score = self.readability_model(essay_text)[0]['label']
153
+
154
+ # Grammar correction
155
+ grammar_correction = self.grammar_correction_model(essay_text)[0]['generated_text']
156
+
157
+ # Advanced feedback generation
158
+ feedback_prompt = f"Give extremely detailed Ontario curriculum feedback for this essay: {essay_text}"
159
+ advanced_feedback = self.feedback_model(feedback_prompt)[0]['generated_text']
160
+
161
  # Weighted overall score
162
  overall_score = self._calculate_enhanced_score(stats, readability, coherence, grammar, structure, content, style)
163
  overall_score = max(0, min(100, overall_score))
 
167
 
168
  # Generate feedback
169
  feedback = self._generate_ontario_feedback(
170
+ overall_score, rubric, stats, readability, coherence, grammar, structure, content, style, topic_results, summary, paraphrase, readability_score, grammar_correction, advanced_feedback
171
  )
172
 
173
  return {
 
177
  "topic_detection": topic_results,
178
  "summary": summary,
179
  "paraphrase": paraphrase,
180
+ "readability_score": readability_score,
181
+ "grammar_correction": grammar_correction,
182
+ "advanced_feedback": advanced_feedback,
183
  "detailed_breakdown": {
184
  "enhanced_statistics": stats,
185
  "readability_analysis": readability,
 
202
  }
203
  return ONTARIO_RUBRIC["R"]
204
 
205
+ def _generate_ontario_feedback(self, score, rubric, stats, readability, coherence, grammar, structure, content, style, topic_results, summary, paraphrase, readability_score, grammar_correction, advanced_feedback) -> Dict:
206
  """Generate extremely detailed and specific feedback aligned with Ontario curriculum"""
207
 
208
  feedback = []
 
214
  feedback.append(f"Summary: {summary}")
215
  feedback.append(f"Paraphrase: {paraphrase}")
216
  feedback.append(f"Topic Detection: {topic_results}")
217
+ feedback.append(f"Readability Score: {readability_score}")
218
+ feedback.append(f"Grammar Correction: {grammar_correction}")
219
+ feedback.append(f"Advanced Feedback: {advanced_feedback}")
220
  feedback.append(f"Statistics: {stats}")
221
  feedback.append(f"Readability: {readability}")
222
  feedback.append(f"Coherence: {coherence}")
 
229
  feedback.append("- Use topic analysis to strengthen essay focus.")
230
  feedback.append("- Improve grammar and coherence as suggested.")
231
  feedback.append("- Expand vocabulary and sentence variety.")
232
+ feedback.append("- Apply advanced feedback for Ontario curriculum excellence.")
233
 
234
  return feedback
235