Rajarshi255 commited on
Commit
25feff5
Β·
verified Β·
1 Parent(s): ec370fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +401 -195
app.py CHANGED
@@ -1,228 +1,434 @@
1
  # app.py
2
- # -----------------------------------------------------------------------------
3
- # Gradio app that scores text with DeepSeek (fallback to OpenAI), then writes
4
- # ONLY the StarsAwarded value to Google Sheets while leaving:
5
- # Timestamp | Name | Class | CorrectAnswers | TotalQuestions | Percentage
6
- # blank (skipped). Header row in the sheet remains unchanged.
7
- # -----------------------------------------------------------------------------
8
-
9
- import os
10
- import json
11
- import time
12
- import traceback
13
- from typing import List, Dict, Tuple, Optional
14
-
15
  import gradio as gr
16
  from openai import OpenAI
17
 
18
- # ==================== ENV / CONFIG ====================
19
- DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY", "")
20
  DEEPSEEK_BASE_URL = os.getenv("DEEPSEEK_BASE_URL", "https://api.deepseek.com/v1")
21
- DEEPSEEK_MODEL = os.getenv("DEEPSEEK_MODEL", "deepseek-chat")
22
-
23
- OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "")
24
- OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-4o-mini")
25
-
26
- SHEET_ID = os.getenv("SHEETS_SPREADSHEET_ID", "")
27
- SHEET_TAB = os.getenv("SHEETS_WORKSHEET_NAME", "Sheet1")
28
- SVC_JSON = os.getenv("GOOGLE_SERVICE_ACCOUNT_JSON", "") # entire JSON blob as a string
29
-
30
- # Retry knobs
31
- MAX_RETRIES = 3
32
- INITIAL_BACKOFF_SEC = 1.5
33
-
34
- # ==================== GOOGLE SHEETS ====================
35
- # Requires: pip install gspread google-auth
36
- import gspread
37
- from google.oauth2.service_account import Credentials
38
-
39
- def _get_gspread_client():
40
- if not SVC_JSON:
41
- raise RuntimeError("Missing GOOGLE_SERVICE_ACCOUNT_JSON env var (full JSON as string).")
42
- svc_info = json.loads(SVC_JSON)
43
- scopes = [
44
- "https://www.googleapis.com/auth/spreadsheets",
45
- "https://www.googleapis.com/auth/drive"
46
- ]
47
- creds = Credentials.from_service_account_info(svc_info, scopes=scopes)
48
- gc = gspread.authorize(creds)
49
- return gc
50
-
51
- def _open_worksheet():
52
- gc = _get_gspread_client()
53
- sh = gc.open_by_key(SHEET_ID)
54
- ws = sh.worksheet(SHEET_TAB)
55
- return ws
56
-
57
- def write_stars_to_sheet(stars: int) -> str:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  """
59
- Appends a row that leaves Timestamp, Name, Class, CorrectAnswers, TotalQuestions, Percentage blank
60
- and writes only StarsAwarded. This assumes your header row already exists as:
61
-
62
- Timestamp | Name | Class | CorrectAnswers | TotalQuestions | Percentage | StarsAwarded
63
-
64
- We write: ["", "", "", "", "", "", stars]
65
  """
66
- try:
67
- ws = _open_worksheet()
68
- row = ["", "", "", "", "", "", stars]
69
- # Use value_input_option='RAW' for exact numeric write; change to 'USER_ENTERED' if needed.
70
- ws.append_row(row, value_input_option='RAW')
71
- return "βœ… Wrote to sheet (StarsAwarded only)."
72
- except Exception as e:
73
- return f"❌ Failed to write to sheet: {e}"
74
-
75
- # ==================== LLM CALLS (DeepSeek β†’ fallback OpenAI) ====================
76
-
77
- def _chat_with_client(client: OpenAI, model: str, messages: List[Dict]) -> str:
78
- """
79
- Generic chat call with retries. Returns text content or raises after exhausting retries.
80
- """
81
- backoff = INITIAL_BACKOFF_SEC
82
  last_err = None
83
- for attempt in range(1, MAX_RETRIES + 1):
 
 
84
  try:
85
- resp = client.chat.completions.create(
86
- model=model,
87
- messages=messages,
88
- temperature=0.2,
89
- timeout=60 # seconds
90
- )
91
  content = resp.choices[0].message.content
92
- if not content:
93
- raise RuntimeError("Empty response content.")
94
- return content
95
  except Exception as e:
96
  last_err = e
97
- if attempt == MAX_RETRIES:
98
- break
99
- time.sleep(backoff)
100
- backoff *= 2
101
- raise RuntimeError(f"LLM call failed after retries: {last_err}")
102
 
103
- def llm_evaluate_text(student_text: str) -> Tuple[str, int]:
104
  """
105
- Evaluates the student's text; returns (feedback, stars).
106
- Stars are a simple 0–5 rubric derived from the model feedback to keep deterministic behavior.
107
- If DeepSeek fails, automatically falls back to OpenAI.
108
  """
109
- system_prompt = (
110
- "You are a concise English evaluator for basic grammar and clarity. "
111
- "Assess the text, list key corrections (bullet points), and end with a single line:\n"
112
- "SCORE: X/5\n"
113
- "Where X is an integer (0–5) reflecting overall correctness and clarity."
114
- )
115
- user_prompt = f"Text:\n{student_text}\n\nReturn short feedback + 'SCORE: X/5' at the end."
116
-
117
- messages = [
118
- {"role": "system", "content": system_prompt},
119
- {"role": "user", "content": user_prompt},
120
- ]
121
-
122
- # Prefer DeepSeek first
123
- ds_client = None
124
  if DEEPSEEK_API_KEY:
125
  try:
126
- ds_client = OpenAI(api_key=DEEPSEEK_API_KEY, base_url=DEEPSEEK_BASE_URL)
127
- feedback = _chat_with_client(ds_client, DEEPSEEK_MODEL, messages)
128
- except Exception:
129
- feedback = None
 
 
 
 
 
 
130
  else:
131
- feedback = None
132
 
133
- # Fallback to OpenAI if needed
134
- if feedback is None:
135
- if not OPENAI_API_KEY:
136
- raise RuntimeError("Both DeepSeek failed and OPENAI_API_KEY is missing.")
137
- oa_client = OpenAI(api_key=OPENAI_API_KEY)
138
- feedback = _chat_with_client(oa_client, OPENAI_MODEL, messages)
139
-
140
- # Extract SCORE: X/5
141
- stars = 0
142
- try:
143
- # Look for a line like "SCORE: 4/5"
144
- for line in feedback.splitlines()[::-1]:
145
- line = line.strip()
146
- if line.upper().startswith("SCORE:"):
147
- # Parse X from "SCORE: X/5"
148
- chunk = line.split(":", 1)[1].strip()
149
- x = chunk.split("/")[0].strip()
150
- stars = int(x)
151
- if stars < 0: stars = 0
152
- if stars > 5: stars = 5
153
- break
154
- except Exception:
155
- # If parsing fails, keep 0
156
- pass
157
-
158
- return feedback, stars
159
-
160
- # ==================== GRADIO HANDLERS ====================
161
-
162
- def evaluate_and_save(student_text: str, manual_stars: Optional[int], use_manual: bool):
163
- if not student_text and not (use_manual and manual_stars is not None):
164
- return "Provide some text or choose manual stars.", ""
165
-
166
- # Run evaluation (unless we’re using manual override only)
167
- feedback = ""
168
- stars = 0
169
- error_blob = ""
170
-
171
- if use_manual and manual_stars is not None:
172
- stars = int(manual_stars)
173
- if stars < 0: stars = 0
174
- if stars > 5: stars = 5
175
- feedback = "(Manual stars override selected; model not called.)"
176
- else:
177
  try:
178
- feedback, stars = llm_evaluate_text(student_text)
 
 
 
 
 
 
179
  except Exception as e:
180
- # If DeepSeek+OpenAI both fail, surface error and don’t crash the app
181
- error_blob = f"⚠️ Evaluation failed, but app is fine:\n{e}\n\n{traceback.format_exc()}"
182
-
183
- # Write only StarsAwarded, leave all other columns blank
184
- sheet_msg = write_stars_to_sheet(stars)
185
-
186
- # Final display
187
- combined = []
188
- if feedback:
189
- combined.append("### Feedback\n" + feedback)
190
- if error_blob:
191
- combined.append(error_blob)
192
- combined.append(f"### Stars Awarded\n{stars} ⭐")
193
- combined.append(f"### Sheet Update\n{sheet_msg}")
194
- return "\n\n".join(combined), stars
195
 
196
- # ==================== UI ====================
197
 
198
- with gr.Blocks(title="Grammar Scorer with Sheet Writer (Stars only)") as demo:
199
- gr.Markdown("## Grammar Scorer β†’ Writes only **StarsAwarded** to Sheet\n"
200
- "- Sheet headers remain: `Timestamp | Name | Class | CorrectAnswers | TotalQuestions | Percentage | StarsAwarded`\n"
201
- "- The app appends a row like: `['', '', '', '', '', '', stars]`")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
 
203
- with gr.Row():
204
- student_text = gr.Textbox(
205
- label="Student Text",
206
- placeholder="Paste the student's text here…",
207
- lines=7
 
 
 
208
  )
 
 
 
 
 
209
 
210
  with gr.Row():
211
- use_manual = gr.Checkbox(label="Manual Stars Override (skip model)", value=False)
212
- manual_stars = gr.Slider(0, 5, value=3, step=1, label="Manual Stars (0–5)")
 
 
 
 
 
213
 
214
  with gr.Row():
215
- run_btn = gr.Button("Evaluate & Save", variant="primary")
216
 
217
- output_md = gr.Markdown(label="Result")
218
- output_stars = gr.Number(label="Stars Written", interactive=False, precision=0)
219
 
220
- run_btn.click(
221
- fn=evaluate_and_save,
222
- inputs=[student_text, manual_stars, use_manual],
223
- outputs=[output_md, output_stars]
224
- )
 
 
 
 
225
 
226
  if __name__ == "__main__":
227
- # You can change server_name/port as needed
228
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
  # app.py
2
+ import os, json, re, time, math, random
 
 
 
 
 
 
 
 
 
 
 
 
3
  import gradio as gr
4
  from openai import OpenAI
5
 
6
+ # ==================== LLM CONFIG ====================
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-chat") # public DeepSeek chat model
10
+
11
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
12
+ OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-4o-mini") # faster/cheaper fallback than gpt-5
13
+
14
+ # ==================== GOOGLE SHEETS CONFIG ====================
15
+ SVC_JSON = os.getenv("GOOGLE_SERVICE_ACCOUNT_JSON") # entire JSON string
16
+ SHEET_ID = os.getenv("SHEETS_SPREADSHEET_ID")
17
+ WS_ID = os.getenv("SHEETS_WORKSHEET_ID") # gid
18
+
19
+ # ==================== LIMITS ====================
20
+ MAX_CHARS = 12000
21
+
22
+ # ==================== PROMPTS ====================
23
+ RUBRIC_USER_TEXT = """
24
+ You are a teacher grading a student's summary.
25
+
26
+ Step 1: Decide if you KNOW the story.
27
+ - If you know it, set "knows_story": true; else false.
28
+
29
+ Step 2: Scoring.
30
+ - If knows_story=true, grade:
31
+ 1) Comprehension (1–5)
32
+ 2) Expression (1–5)
33
+ 3) Reflection/Moral Takeaway (1–5)
34
+ 4) Grammar (lenient): identify grammar/spelling/punctuation issues.
35
+ - If knows_story=false, set Comprehension to null and still do the rest.
36
+
37
+ Grammar issue format: for each issue provide
38
+ - "error_excerpt" (short snippet with the mistake, ≀120 chars),
39
+ - "correction" (just the corrected snippet),
40
+ - "explanation" (one short reason),
41
+ - "start_index" (0-based in ORIGINAL text),
42
+ - "end_index" (exclusive).
43
+
44
+ Also provide "corrected_summary" (light edits only), and "remarks" (1–2 sentences).
45
+ Do NOT compute Length stars; app does that locally.
46
+ """
47
+
48
+ JSON_SCHEMA_HINT = """
49
+ Return ONLY raw JSON (no markdown fences, no labels), exactly:
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": "<string>",
67
+ "remarks": "<string>"
68
+ }
69
+ - If no grammar issues, use an empty list [].
70
+ - Numbers only for scores.
71
+ """
72
+
73
+ # ==================== LLM BRIDGE WITH RETRIES ====================
74
+ def _retry_delays(n):
75
+ # jittered exponential backoff: 0.6s, 1.2s, 2.4s ...
76
+ base = 0.6
77
+ for i in range(n):
78
+ yield base * (2 ** i) * (0.8 + 0.4 * random.random())
79
+
80
+ def call_openai_compatible(api_key, base_url, model, messages, tries=3):
81
  """
82
+ Generic caller for OpenAI-compatible chat completions with retries.
83
+ Returns (content, model_used_str).
84
+ Raises RuntimeError after exhausting retries.
 
 
 
85
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  last_err = None
87
+ client = OpenAI(api_key=api_key, base_url=base_url) if base_url else OpenAI(api_key=api_key)
88
+
89
+ for delay in list(_retry_delays(tries)) + [0]: # run tries times; last 0 means "final attempt now"
90
  try:
91
+ resp = client.chat.completions.create(model=model, messages=messages)
 
 
 
 
 
92
  content = resp.choices[0].message.content
93
+ return content, f"{'DeepSeek' if 'deepseek' in (base_url or '') else 'OpenAI'} ({model})"
 
 
94
  except Exception as e:
95
  last_err = e
96
+ if delay > 0:
97
+ time.sleep(delay)
98
+
99
+ raise RuntimeError(f"LLM call failed after {tries} tries [{model} @ {base_url or 'api.openai.com'}]: {last_err}")
 
100
 
101
+ def llm_chat(messages):
102
  """
103
+ Try DeepSeek (3 retries). If that fails, fallback to OpenAI (2 retries).
104
+ Always returns (content, model_used_str) OR raises RuntimeError with reason.
 
105
  """
106
+ # DeepSeek first
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  if DEEPSEEK_API_KEY:
108
  try:
109
+ return call_openai_compatible(
110
+ api_key=DEEPSEEK_API_KEY,
111
+ base_url=DEEPSEEK_BASE_URL,
112
+ model=DEEPSEEK_MODEL,
113
+ messages=messages,
114
+ tries=3
115
+ )
116
+ except Exception as e:
117
+ # Continue to fallback, but include why DS failed in final error if OA also fails
118
+ ds_err = f"DeepSeek error: {e}"
119
  else:
120
+ ds_err = "DeepSeek key not set."
121
 
122
+ # OpenAI fallback
123
+ if OPENAI_API_KEY:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  try:
125
+ return call_openai_compatible(
126
+ api_key=OPENAI_API_KEY,
127
+ base_url=None, # OpenAI official
128
+ model=OPENAI_MODEL,
129
+ messages=messages,
130
+ tries=2
131
+ )
132
  except Exception as e:
133
+ raise RuntimeError(f"{ds_err}\nOpenAI error: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
 
135
+ raise RuntimeError(f"{ds_err}\nNo OpenAI fallback configured (OPENAI_API_KEY missing).")
136
 
137
+ # ==================== JSON SALVAGE (ROBUST) ====================
138
+ def salvage_json(s: str) -> dict:
139
+ """
140
+ Make the model output JSON-parseable:
141
+ - remove ```json fences
142
+ - normalize quotes
143
+ - keep only the last {...} block if extra text exists
144
+ - remove trailing commas
145
+ - fill missing start/end_index with 0
146
+ - coerce types safely
147
+ """
148
+ s = s.strip()
149
+ # strip code fences
150
+ s = re.sub(r"^\s*```(?:json)?\s*", "", s)
151
+ s = re.sub(r"\s*```\s*$", "", s)
152
+
153
+ # normalize curly quotes
154
+ s = s.replace("β€œ", '"').replace("”", '"').replace("’", "'")
155
+
156
+ # capture last JSON object if there's extra prose
157
+ m = re.search(r"\{.*\}\s*$", s, flags=re.S)
158
+ if m:
159
+ s = m.group(0)
160
+
161
+ # remove trailing commas before } or ]
162
+ s = re.sub(r",\s*([}\]])", r"\1", s)
163
+
164
+ # handle missing numeric values
165
+ s = re.sub(r'"end_index"\s*:\s*(?=(,|\}))', r'"end_index": 0', s)
166
+ s = re.sub(r'"start_index"\s*:\s*(?=(,|\}))', r'"start_index": 0', s)
167
+
168
+ data = json.loads(s)
169
+
170
+ # sanitize grammar_issues
171
+ issues = data.get("grammar_issues", [])
172
+ safe_issues = []
173
+ if isinstance(issues, list):
174
+ for it in issues:
175
+ if not isinstance(it, dict):
176
+ continue
177
+ si = it.get("start_index", 0)
178
+ ei = it.get("end_index", 0)
179
+ try: si = int(si)
180
+ except Exception: si = 0
181
+ try: ei = int(ei)
182
+ except Exception: ei = 0
183
+ safe_issues.append({
184
+ "error_excerpt": str(it.get("error_excerpt", ""))[:200],
185
+ "correction": str(it.get("correction", ""))[:200],
186
+ "explanation": str(it.get("explanation", ""))[:200],
187
+ "start_index": si,
188
+ "end_index": ei,
189
+ })
190
+ data["grammar_issues"] = safe_issues
191
+ return data
192
+
193
+ # ==================== HELPERS ====================
194
+ def stars_bar(value: float | int | None, max_stars: int = 5) -> str:
195
+ if value is None or float(value) <= 0:
196
+ return "β€”"
197
+ n_int = max(0, min(max_stars, int(round(float(value)))))
198
+ return "⭐" * n_int
199
+
200
+ def count_words(text: str) -> int:
201
+ return len(re.findall(r"\b\w[\w'-]*\b", text))
202
+
203
+ def length_stars(word_count: int) -> int:
204
+ if word_count < 100: return 0
205
+ if word_count <= 150: return 1
206
+ if word_count <= 200: return 3
207
+ if word_count <= 250: return 4
208
+ return 5
209
+
210
+ def clamp_1_to_5(x): return max(1, min(5, int(x)))
211
+
212
+ def grammar_stars_from_count(n: int) -> float:
213
+ if n >= 50: return 0.0
214
+ return max(0.0, 5.0 - 0.1 * n)
215
+
216
+ def annotate_original_with_issues(text: str, issues: list[dict]) -> str:
217
+ clean = []
218
+ for i, it in enumerate(issues, 1):
219
+ try:
220
+ si, ei = int(it["start_index"]), int(it["end_index"])
221
+ if 0 <= si < ei <= len(text): clean.append((si, ei, i))
222
+ except Exception:
223
+ continue
224
+ clean.sort(key=lambda x: x[0])
225
+ out, cursor, last_end = [], 0, -1
226
+ for si, ei, idx in clean:
227
+ if si < last_end: # skip overlaps
228
+ continue
229
+ out.append(text[cursor:si])
230
+ out.append(f"[[#{idx}: {text[si:ei]}]]")
231
+ cursor, last_end = ei, ei
232
+ out.append(text[cursor:])
233
+ return "".join(out)
234
+
235
+ def apply_corrections_by_offsets(text: str, issues: list[dict]) -> str:
236
+ spans = []
237
+ for it in issues:
238
+ try:
239
+ si, ei = int(it["start_index"]), int(it["end_index"])
240
+ cor = str(it.get("correction", ""))
241
+ if 0 <= si < ei <= len(text): spans.append((si, ei, cor))
242
+ except Exception:
243
+ continue
244
+ spans.sort(key=lambda x: x[0])
245
+ out, cursor, last_end = [], 0, -1
246
+ for si, ei, cor in spans:
247
+ if si < last_end: continue
248
+ out.append(text[cursor:si]); out.append(cor)
249
+ cursor, last_end = ei, ei
250
+ out.append(text[cursor:])
251
+ return "".join(out)
252
+
253
+ # ==================== GOOGLE SHEETS WRITER ====================
254
+ def append_to_sheet(row: list) -> str:
255
+ if not (SVC_JSON and SHEET_ID and WS_ID):
256
+ return "Sheets not configured (missing GOOGLE_SERVICE_ACCOUNT_JSON / SHEETS_SPREADSHEET_ID / SHEETS_WORKSHEET_ID)."
257
+ try:
258
+ import gspread
259
+ from google.oauth2.service_account import Credentials
260
+ creds_info = json.loads(SVC_JSON)
261
+ scopes = ["https://www.googleapis.com/auth/spreadsheets"]
262
+ creds = Credentials.from_service_account_info(creds_info, scopes=scopes)
263
+ gc = gspread.authorize(creds)
264
+ ws = gc.open_by_key(SHEET_ID).get_worksheet_by_id(int(WS_ID))
265
+ ws.append_row(row, value_input_option="USER_ENTERED")
266
+ return "OK"
267
+ except Exception as e:
268
+ return f"Sheets error: {e}"
269
+
270
+ # ==================== FRIENDLY INTRO ====================
271
+ def intro_message(name, klass):
272
+ if not name.strip() or not klass.strip():
273
+ return "πŸ‘‹ Please tell me your name and class first!"
274
+ return f"""
275
+ ### 🌟 Hi {name} of {klass}!
276
+ Welcome to the **Summary Grader** πŸ‘‹
277
+
278
+ How I'll mark:
279
+ 1) **Comprehension** – only if I know the story
280
+ 2) **Expression** – clarity & flow
281
+ 3) **Reflection/Moral** – what you learned
282
+ 4) **Grammar & Neatness** – lenient but tidy
283
+ 5) **Length** – word-count stars:
284
+ - <100 β†’ 0⭐, 100–150 β†’ 1⭐, 150–200 β†’ 3⭐, 200–250 β†’ 4⭐, 250+ β†’ 5⭐
285
+
286
+ Write clearly, think deeply, and press **Grade & Save!**
287
+ """
288
+
289
+ # ==================== CORE GRADING ====================
290
+ def grade_summary(student_name: str, student_class: str, title: str, student_summary: str):
291
+ try:
292
+ if not student_name.strip():
293
+ return "⚠️ Please enter the student's name.", "", ""
294
+ if not student_class.strip():
295
+ return "⚠️ Please enter the class/grade.", "", ""
296
+ if not title.strip():
297
+ return "⚠️ Please enter a title.", "", ""
298
+ if not student_summary.strip():
299
+ return "⚠️ Please paste the student's summary.", "", ""
300
+ if len(student_summary) > MAX_CHARS:
301
+ return f"⚠️ Summary too long ({len(student_summary)} chars). Limit is {MAX_CHARS}.", "", ""
302
+
303
+ prompt = f"""
304
+ TITLE: {title}
305
+
306
+ STUDENT SUMMARY (ORIGINAL TEXT BELOW):
307
+ {student_summary}
308
+
309
+ {RUBRIC_USER_TEXT}
310
+
311
+ {JSON_SCHEMA_HINT}
312
+ """
313
+ # DeepSeek (with retries) β†’ OpenAI fallback (with retries)
314
+ content, model_used = llm_chat([{"role": "user", "content": prompt}])
315
+
316
+ # Parse safely
317
+ data = salvage_json(content)
318
+
319
+ # Extract
320
+ knows_story = bool(data.get("knows_story", False))
321
+ scores = data.get("scores", {}) or {}
322
+ comp = scores.get("Comprehension", None)
323
+ expr = clamp_1_to_5(scores.get("Expression", 1))
324
+ refl = clamp_1_to_5(scores.get("Reflection/Moral Takeaway", 1))
325
+ issues = data.get("grammar_issues", []) or []
326
+ corrected = (data.get("corrected_summary") or "").strip()
327
+ remarks = (data.get("remarks") or "").strip()
328
+ if not knows_story:
329
+ comp = None
330
+
331
+ wc = count_words(student_summary)
332
+ len_s = length_stars(wc)
333
+ grammar_count = len(issues)
334
+ grammar_star = round(grammar_stars_from_count(grammar_count), 1)
335
+
336
+ vals = [float(expr), float(refl), float(len_s), float(grammar_star)]
337
+ if knows_story and comp is not None:
338
+ vals.append(float(comp))
339
+ avg = round(sum(vals) / len(vals), 2) if vals else 0.0
340
+
341
+ # Report
342
+ md = []
343
+ md.append(f"### πŸ“š Summary Grading Report")
344
+ md.append(f"**Name:** {student_name} &nbsp;&nbsp; **Class:** {student_class}")
345
+ md.append(f"**Title:** {title}")
346
+ md.append(f"**Word count:** {wc}")
347
+ md.append("")
348
+ md.append("#### ⭐ Category-wise Scores")
349
+ if knows_story and comp is not None:
350
+ md.append(f"- **Comprehension:** {stars_bar(comp)} ({comp} stars)")
351
+ else:
352
+ md.append(f"- **Comprehension:** *(omitted)*")
353
+ md.append(f"- **Expression:** {stars_bar(expr)} ({expr} stars)")
354
+ md.append(f"- **Reflection/Moral Takeaway:** {stars_bar(refl)} ({refl} stars)")
355
+ md.append(f"- **Length:** {stars_bar(len_s)} ({len_s} stars, based on {wc} words)")
356
+ md.append(f"- **Grammar:** {stars_bar(grammar_star)} ({grammar_star:.1f} stars, {grammar_count} mistakes)")
357
+ md.append("")
358
+ md.append(f"**Average Score:** {avg:.2f} {stars_bar(avg)} *(out of 5)*")
359
+ md.append("")
360
+ md.append("#### ✏️ Corrected Passage (LLM, minimal edits)")
361
+ md.append(f"{corrected if corrected else '_(No changes returned by the model.)_'}")
362
+ md.append("")
363
+ md.append("#### πŸ”Ž Annotated Original (exact mistake spans)")
364
+ md.append(annotate_original_with_issues(student_summary, issues))
365
+ md.append("")
366
+ md.append("#### 🧹 Grammar Issues (original β†’ corrected)")
367
+ if not issues:
368
+ md.append("- None πŸŽ‰")
369
+ else:
370
+ for i, it in enumerate(issues, 1):
371
+ err = (it.get('error_excerpt') or '').strip()
372
+ cor = (it.get('correction') or '').strip()
373
+ why = (it.get('explanation') or '').strip()
374
+ si = it.get('start_index', '?')
375
+ ei = it.get('end_index', '?')
376
+ md.append(f"{i}. `[{si}:{ei}]` **{err}** β†’ **{cor}** β€” {why}")
377
+ md.append("")
378
+ md.append(f"*Graded using:* **{model_used}**")
379
+ md_str = "\n".join(md)
380
+
381
+ # Save to Google Sheet (optional)
382
+ timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
383
+ row = [
384
+ timestamp, student_name, student_class, title, wc,
385
+ "Yes" if knows_story else "No",
386
+ (comp if comp is not None else ""), expr, refl, len_s,
387
+ grammar_count, grammar_star, avg, model_used, remarks,
388
+ ]
389
+ sheets_status = append_to_sheet(row)
390
+ status_text = "Saved to Google Sheet βœ…" if sheets_status == "OK" else f"Not saved ❌ β€” {sheets_status}"
391
+
392
+ return md_str, corrected, status_text
393
 
394
+ except Exception as e:
395
+ # show real reason; never crash to tiny red chip
396
+ err_md = (
397
+ "### ❌ Grading failed\n"
398
+ "```\n" + str(e) + "\n```\n"
399
+ f"- DeepSeek model: `{DEEPSEEK_MODEL}` Base: `{DEEPSEEK_BASE_URL}`\n"
400
+ f"- OpenAI model: `{OPENAI_MODEL}`\n"
401
+ "I retried DeepSeek, then fell back to OpenAI. If this keeps happening, check secrets and model IDs."
402
  )
403
+ return err_md, "", "Not saved ❌ β€” error during grading"
404
+
405
+ # ==================== UI ====================
406
+ with gr.Blocks(title="Summary Grader") as demo:
407
+ gr.Markdown("## Summary Grader")
408
 
409
  with gr.Row():
410
+ student_name = gr.Textbox(label="Student Name", placeholder="Riya Sen")
411
+ student_class = gr.Textbox(label="Class/Grade", placeholder="Class 6")
412
+
413
+ intro_out = gr.Markdown(label="Welcome Message", visible=True)
414
+ gr.Button("Show How I’ll Mark You", variant="secondary").click(
415
+ fn=intro_message, inputs=[student_name, student_class], outputs=intro_out
416
+ )
417
 
418
  with gr.Row():
419
+ title_in = gr.Textbox(label="Book/Movie Title", placeholder="The Gift of the Magi")
420
 
421
+ summary_in = gr.Textbox(label="Paste the student's summary", lines=12, placeholder="Paste text here...")
 
422
 
423
+ btn = gr.Button("Grade & Save", variant="primary")
424
+
425
+ report_out = gr.Markdown(label="Grading Report")
426
+ corrected_out = gr.Textbox(label="Corrected Passage (plain text)", lines=10)
427
+ status_out = gr.Markdown(label="Save Status")
428
+
429
+ btn.click(fn=grade_summary,
430
+ inputs=[student_name, student_class, title_in, summary_in],
431
+ outputs=[report_out, corrected_out, status_out])
432
 
433
  if __name__ == "__main__":
434
+ demo.launch()