Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
# ====================
|
| 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
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
# ====================
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
"""
|
| 59 |
-
|
| 60 |
-
|
| 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 |
-
|
|
|
|
|
|
|
| 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
|
| 93 |
-
raise RuntimeError("Empty response content.")
|
| 94 |
-
return content
|
| 95 |
except Exception as e:
|
| 96 |
last_err = e
|
| 97 |
-
if
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
raise RuntimeError(f"LLM call failed after retries: {last_err}")
|
| 102 |
|
| 103 |
-
def
|
| 104 |
"""
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
If DeepSeek fails, automatically falls back to OpenAI.
|
| 108 |
"""
|
| 109 |
-
|
| 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 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
else:
|
| 131 |
-
|
| 132 |
|
| 133 |
-
#
|
| 134 |
-
if
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
except Exception as e:
|
| 180 |
-
|
| 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 |
-
|
| 197 |
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
| 208 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
with gr.Row():
|
| 211 |
-
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
with gr.Row():
|
| 215 |
-
|
| 216 |
|
| 217 |
-
|
| 218 |
-
output_stars = gr.Number(label="Stars Written", interactive=False, precision=0)
|
| 219 |
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
if __name__ == "__main__":
|
| 227 |
-
|
| 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} **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()
|
|
|