Rajarshi255 commited on
Commit
6148d3f
Β·
verified Β·
1 Parent(s): 8864c5d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +283 -0
app.py ADDED
@@ -0,0 +1,283 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, json, textwrap, re
2
+ import gradio as gr
3
+ from openai import OpenAI
4
+
5
+ # ==================== Config ====================
6
+ # DeepSeek (primary) β€” OpenAI-compatible API
7
+ DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
8
+ DEEPSEEK_BASE_URL = os.getenv("DEEPSEEK_BASE_URL", "https://api.deepseek.com/v1")
9
+ DEEPSEEK_MODEL = os.getenv("DEEPSEEK_MODEL", "DeepSeek-V3.2-Exp") # non-thinking
10
+
11
+ # OpenAI (fallback)
12
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
13
+ OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-5")
14
+
15
+ # Limits (basic hygiene for web)
16
+ MAX_CHARS = 12000 # limit paste size to keep latency sane
17
+
18
+ # ==================== Prompts ====================
19
+ RUBRIC_USER_TEXT = """
20
+ You are a teacher grading a student's summary. Follow these rules strictly.
21
+
22
+ Step 1: Decide if you KNOW the story.
23
+ - If you KNOW the work (book/movie), set "knows_story": true.
24
+ - If you DO NOT know the work, set "knows_story": false.
25
+
26
+ Step 2: Scoring rules.
27
+ - If knows_story=true, grade all of these:
28
+ 1) Comprehension – 1 to 5 stars (how accurately the student captured the story or message).
29
+ 2) Expression – 1 to 5 stars (clarity, coherence, fluency; how understandable if narrated).
30
+ 3) Reflection/Moral Takeaway – 1 to 5 stars (thoughtfulness, personal connection).
31
+ 4) Grammar (lenient): Identify grammar/spelling/punctuation mistakes in the student's text. Ignore minor conversational-English slips.
32
+ Return a list of issues where each item contains:
33
+ - "error_excerpt": student's exact snippet with the mistake (max ~120 chars)
34
+ - "correction": corrected version of that snippet
35
+ - "explanation": short reason (e.g., "subject–verb agreement", "punctuation spacing")
36
+ - "start_index": 0-based char index in the ORIGINAL text where the error begins
37
+ - "end_index": exclusive end index
38
+ - If knows_story=false, omit Comprehension (set it to null) but grade the rest with the same grammar rules.
39
+
40
+ Constraints:
41
+ - Keep "error_excerpt" and "correction" minimal and tightly focused (no rewriting entire paragraphs).
42
+ - Indices MUST refer to the student's ORIGINAL text provided.
43
+ - After listing issues, also return a "corrected_summary" that lightly fixes the student's full passage (minimal edits, same meaning, natural).
44
+
45
+ IMPORTANT: Do NOT compute "Length" stars. The app computes them locally by word count bins.
46
+ """
47
+
48
+ JSON_SCHEMA_HINT = """
49
+ Return ONLY valid JSON with this EXACT structure:
50
+ {
51
+ "knows_story": true/false,
52
+ "scores": {
53
+ "Comprehension": 1-5 or null,
54
+ "Expression": 1-5,
55
+ "Reflection/Moral Takeaway": 1-5
56
+ },
57
+ "grammar_issues": [
58
+ {
59
+ "error_excerpt": "<string>",
60
+ "correction": "<string>",
61
+ "explanation": "<string>",
62
+ "start_index": 0,
63
+ "end_index": 0
64
+ }
65
+ ],
66
+ "corrected_summary": "<the student's passage corrected for grammar (minimal edits)>",
67
+ "remarks": "<1–2 sentence feedback (plain text)>"
68
+ }
69
+ - If there are no grammar issues, return "grammar_issues": [].
70
+ - No markdown. No extra keys. Numbers only for scores.
71
+ """
72
+
73
+ # ==================== LLM Bridge ====================
74
+ def llm_chat(messages, prefer_deepseek=True):
75
+ """
76
+ Try DeepSeek first (non-thinking mode via OpenAI-compatible API); on error, fallback to OpenAI.
77
+ Return (content, model_used_str).
78
+ """
79
+ last_err = None
80
+ if prefer_deepseek and DEEPSEEK_API_KEY:
81
+ try:
82
+ ds = OpenAI(api_key=DEEPSEEK_API_KEY, base_url=DEEPSEEK_BASE_URL)
83
+ resp = ds.chat.completions.create(model=DEEPSEEK_MODEL, messages=messages)
84
+ return resp.choices[0].message.content, f"DeepSeek ({DEEPSEEK_MODEL})"
85
+ except Exception as e:
86
+ last_err = e # fall through to OpenAI
87
+
88
+ if OPENAI_API_KEY:
89
+ try:
90
+ oa = OpenAI(api_key=OPENAI_API_KEY)
91
+ resp = oa.chat.completions.create(model=OPENAI_MODEL, messages=messages)
92
+ return resp.choices[0].message.content, f"OpenAI ({OPENAI_MODEL})"
93
+ except Exception as e:
94
+ last_err = e
95
+
96
+ if not DEEPSEEK_API_KEY and not OPENAI_API_KEY:
97
+ raise RuntimeError("No API key found. Set DEEPSEEK_API_KEY and/or OPENAI_API_KEY as Secrets.")
98
+ raise RuntimeError(f"Both DeepSeek and OpenAI calls failed. Last error: {last_err}")
99
+
100
+ # ==================== Helpers ====================
101
+ def salvage_json(s: str) -> dict:
102
+ s = s.strip()
103
+ try:
104
+ return json.loads(s)
105
+ except json.JSONDecodeError:
106
+ m = re.search(r"\{.*\}\s*$", s, flags=re.S)
107
+ if not m:
108
+ raise ValueError("Model did not return valid JSON.")
109
+ return json.loads(m.group(0))
110
+
111
+ def stars_bar(value: float | int | None, max_stars: int = 5) -> str:
112
+ if value is None or float(value) <= 0:
113
+ return "β€”"
114
+ n_int = max(0, min(max_stars, int(round(float(value)))))
115
+ return "⭐" * n_int
116
+
117
+ def count_words(text: str) -> int:
118
+ return len(re.findall(r"\b\w[\w'-]*\b", text))
119
+
120
+ def length_stars(word_count: int) -> int:
121
+ if word_count < 100: return 0
122
+ if word_count <= 150: return 1
123
+ if word_count <= 200: return 3
124
+ if word_count <= 250: return 4
125
+ return 5
126
+
127
+ def clamp_1_to_5(x): return max(1, min(5, int(x)))
128
+
129
+ def grammar_stars_from_count(n: int) -> float:
130
+ if n >= 50: return 0.0
131
+ return max(0.0, 5.0 - 0.1 * n)
132
+
133
+ def annotate_original_with_issues(text: str, issues: list[dict]) -> str:
134
+ clean = []
135
+ for i, it in enumerate(issues, 1):
136
+ try:
137
+ si, ei = int(it["start_index"]), int(it["end_index"])
138
+ if 0 <= si < ei <= len(text): clean.append((si, ei, i))
139
+ except Exception:
140
+ continue
141
+ clean.sort(key=lambda x: x[0])
142
+ out, cursor, last_end = [], 0, -1
143
+ for si, ei, idx in clean:
144
+ if si < last_end: # skip overlaps
145
+ continue
146
+ out.append(text[cursor:si])
147
+ out.append(f"[[#{idx}: {text[si:ei]}]]")
148
+ cursor, last_end = ei, ei
149
+ out.append(text[cursor:])
150
+ return "".join(out)
151
+
152
+ def apply_corrections_by_offsets(text: str, issues: list[dict]) -> str:
153
+ spans = []
154
+ for it in issues:
155
+ try:
156
+ si, ei = int(it["start_index"]), int(it["end_index"])
157
+ cor = str(it.get("correction", ""))
158
+ if 0 <= si < ei <= len(text): spans.append((si, ei, cor))
159
+ except Exception:
160
+ continue
161
+ spans.sort(key=lambda x: x[0])
162
+ out, cursor, last_end = [], 0, -1
163
+ for si, ei, cor in spans:
164
+ if si < last_end: # skip overlaps
165
+ continue
166
+ out.append(text[cursor:si]); out.append(cor)
167
+ cursor, last_end = ei, ei
168
+ out.append(text[cursor:])
169
+ return "".join(out)
170
+
171
+ # ==================== Core grading ====================
172
+ def grade_summary(title: str, student_summary: str):
173
+ if not title.strip():
174
+ return "⚠️ Please enter a title.", None
175
+ if not student_summary.strip():
176
+ return "⚠️ Please paste the student's summary.", None
177
+ if len(student_summary) > MAX_CHARS:
178
+ return f"⚠️ Summary too long ({len(student_summary)} chars). Limit is {MAX_CHARS}.", None
179
+
180
+ prompt = f"""
181
+ TITLE: {title}
182
+
183
+ STUDENT SUMMARY (ORIGINAL TEXT BELOW):
184
+ {student_summary}
185
+
186
+ {RUBRIC_USER_TEXT}
187
+
188
+ {JSON_SCHEMA_HINT}
189
+ """
190
+ # Call LLM
191
+ content, model_used = llm_chat([{"role": "user", "content": prompt}], prefer_deepseek=True)
192
+ data = salvage_json(content)
193
+
194
+ # Extract
195
+ knows_story = bool(data.get("knows_story", False))
196
+ scores = data.get("scores", {}) or {}
197
+ comp = scores.get("Comprehension", None)
198
+ expr = clamp_1_to_5(scores.get("Expression", 1))
199
+ refl = clamp_1_to_5(scores.get("Reflection/Moral Takeaway", 1))
200
+ issues = data.get("grammar_issues", []) or []
201
+ corrected = (data.get("corrected_summary") or "").strip()
202
+ remarks = (data.get("remarks") or "").strip()
203
+
204
+ if not knows_story:
205
+ comp = None
206
+
207
+ wc = count_words(student_summary)
208
+ len_s = length_stars(wc)
209
+ grammar_count = len(issues)
210
+ grammar_star = round(grammar_stars_from_count(grammar_count), 1)
211
+
212
+ # Average over included categories
213
+ vals = [float(expr), float(refl), float(len_s), float(grammar_star)]
214
+ if knows_story and comp is not None:
215
+ vals.append(float(comp))
216
+ avg = round(sum(vals) / len(vals), 2) if vals else 0.0
217
+
218
+ # Compose Markdown report
219
+ md = []
220
+ md.append(f"### πŸ“š Summary Grading Report")
221
+ md.append(f"**Title:** {title}")
222
+ md.append(f"**Word count:** {wc}")
223
+ md.append(f"**Model used:** `{model_used}`")
224
+ md.append("")
225
+ md.append("#### Category-wise Scores")
226
+ if knows_story and comp is not None:
227
+ md.append(f"- **Comprehension:** {stars_bar(comp)} ({comp} stars)")
228
+ else:
229
+ md.append(f"- **Comprehension:** *(omitted)*")
230
+ md.append(f"- **Expression:** {stars_bar(expr)} ({expr} stars)")
231
+ md.append(f"- **Reflection/Moral Takeaway:** {stars_bar(refl)} ({refl} stars)")
232
+ md.append(f"- **Length:** {stars_bar(len_s)} ({len_s} stars, based on {wc} words)")
233
+ md.append(f"- **Grammar:** {stars_bar(grammar_star)} ({grammar_star:.1f} stars, {grammar_count} mistakes)")
234
+ md.append("")
235
+ md.append(f"**Average Score:** {avg:.2f} {stars_bar(avg)} *(out of 5)*")
236
+ md.append("")
237
+ md.append("#### Corrected Passage (LLM, minimal edits)")
238
+ md.append(f"{corrected if corrected else '_(No changes returned by the model.)_'}")
239
+ md.append("")
240
+ md.append("#### Annotated Original (exact mistake spans)")
241
+ annotated = annotate_original_with_issues(student_summary, issues)
242
+ md.append(f"{annotated}")
243
+ md.append("")
244
+ md.append("#### Grammar Issues (with positions)")
245
+ if not issues:
246
+ md.append("- None πŸŽ‰")
247
+ else:
248
+ for i, it in enumerate(issues, 1):
249
+ err = (it.get('error_excerpt') or '').strip()
250
+ cor = (it.get('correction') or '').strip()
251
+ why = (it.get('explanation') or '').strip()
252
+ si = it.get('start_index', '?')
253
+ ei = it.get('end_index', '?')
254
+ md.append(f"{i}. `[{si}:{ei}]` **{err}** β†’ **{cor}** β€” {why}")
255
+ md.append("")
256
+ md.append("#### Program-applied corrections (by offsets)")
257
+ program_fixed = apply_corrections_by_offsets(student_summary, issues)
258
+ md.append(f"{program_fixed}")
259
+ md.append("")
260
+ md.append("#### Overall Remark")
261
+ md.append(f"{remarks if remarks else '_(None)_'}")
262
+ md_str = "\n".join(md)
263
+
264
+ # Also return plain corrected text as a second output (easy copy)
265
+ return md_str, corrected
266
+
267
+ # ==================== UI ====================
268
+ with gr.Blocks(title="Summary Grader (DeepSeek β†’ OpenAI fallback)") as demo:
269
+ gr.Markdown("## Summary Grader\nDeepSeek-V3.2-Exp first; falls back to OpenAI GPT-5 if needed.")
270
+ with gr.Row():
271
+ title_in = gr.Textbox(label="Book/Movie Title", placeholder="The Gift of the Magi")
272
+ summary_in = gr.Textbox(label="Paste the student's summary", lines=12, placeholder="Paste text here...")
273
+ btn = gr.Button("Grade Summary", variant="primary")
274
+ with gr.Row():
275
+ report_out = gr.Markdown(label="Grading Report")
276
+ corrected_out = gr.Textbox(label="Corrected Passage (plain text)", lines=10)
277
+
278
+ btn.click(fn=grade_summary, inputs=[title_in, summary_in], outputs=[report_out, corrected_out])
279
+
280
+ # Entrypoint for Spaces
281
+ if __name__ == "__main__":
282
+ demo.launch()
283
+