Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,36 @@
|
|
| 1 |
-
import os, json, textwrap, re
|
| 2 |
import gradio as gr
|
| 3 |
from openai import OpenAI
|
| 4 |
|
| 5 |
-
# ====================
|
| 6 |
-
|
| 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
|
| 10 |
|
| 11 |
-
# OpenAI (fallback)
|
| 12 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 13 |
-
OPENAI_MODEL
|
| 14 |
-
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 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 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
{
|
| 51 |
"knows_story": true/false,
|
| 52 |
"scores": {
|
|
@@ -55,35 +39,23 @@ Return ONLY valid JSON with this EXACT structure:
|
|
| 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": "<
|
| 67 |
-
"remarks": "<
|
| 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
|
| 74 |
-
def llm_chat(messages
|
| 75 |
-
"""
|
| 76 |
-
|
| 77 |
-
|
| 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 |
-
|
| 86 |
-
|
| 87 |
|
| 88 |
if OPENAI_API_KEY:
|
| 89 |
try:
|
|
@@ -91,193 +63,160 @@ def llm_chat(messages, prefer_deepseek=True):
|
|
| 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 |
-
|
| 95 |
|
| 96 |
-
|
| 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
|
| 106 |
m = re.search(r"\{.*\}\s*$", s, flags=re.S)
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 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
|
|
|
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 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 |
-
|
| 181 |
-
|
|
|
|
|
|
|
| 182 |
|
| 183 |
-
|
| 184 |
-
{
|
| 185 |
|
| 186 |
-
|
|
|
|
| 187 |
|
| 188 |
-
{
|
| 189 |
"""
|
| 190 |
-
|
| 191 |
-
content, model_used = llm_chat([{"role": "user", "content": prompt}], prefer_deepseek=True)
|
| 192 |
data = salvage_json(content)
|
| 193 |
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
comp = None
|
| 206 |
-
|
| 207 |
-
wc = count_words(student_summary)
|
| 208 |
-
len_s = length_stars(wc)
|
| 209 |
grammar_count = len(issues)
|
| 210 |
-
grammar_star =
|
| 211 |
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 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
|
| 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 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
md.append(f"-
|
| 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("
|
| 241 |
-
annotated = annotate_original_with_issues(student_summary, issues)
|
| 242 |
-
md.append(f"{annotated}")
|
| 243 |
md.append("")
|
| 244 |
-
md.append("
|
| 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("
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
|
|
|
| 263 |
|
| 264 |
-
|
| 265 |
-
return md_str, corrected
|
| 266 |
|
| 267 |
# ==================== UI ====================
|
| 268 |
-
with gr.Blocks(title="Summary Grader (DeepSeek β OpenAI
|
| 269 |
-
gr.Markdown("## Summary Grader\nDeepSeek
|
|
|
|
| 270 |
with gr.Row():
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
|
|
|
|
|
|
|
|
|
| 274 |
with gr.Row():
|
| 275 |
-
|
| 276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
|
| 278 |
-
|
|
|
|
|
|
|
| 279 |
|
| 280 |
-
# Entrypoint for Spaces
|
| 281 |
if __name__ == "__main__":
|
| 282 |
demo.launch()
|
| 283 |
-
|
|
|
|
| 1 |
+
import os, json, textwrap, re, time
|
| 2 |
import gradio as gr
|
| 3 |
from openai import OpenAI
|
| 4 |
|
| 5 |
+
# ==================== LLM CONFIG ====================
|
| 6 |
+
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
|
|
|
|
| 7 |
DEEPSEEK_BASE_URL = os.getenv("DEEPSEEK_BASE_URL", "https://api.deepseek.com/v1")
|
| 8 |
+
DEEPSEEK_MODEL = os.getenv("DEEPSEEK_MODEL", "DeepSeek-V3.2-Exp")
|
| 9 |
|
|
|
|
| 10 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 11 |
+
OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-5")
|
| 12 |
+
|
| 13 |
+
# ==================== GOOGLE SHEETS CONFIG ====================
|
| 14 |
+
SVC_JSON = os.getenv("GOOGLE_SERVICE_ACCOUNT_JSON") # full JSON as string
|
| 15 |
+
SHEET_ID = os.getenv("SHEETS_SPREADSHEET_ID")
|
| 16 |
+
WS_ID = os.getenv("SHEETS_WORKSHEET_ID")
|
| 17 |
+
|
| 18 |
+
MAX_CHARS = 12000
|
| 19 |
+
|
| 20 |
+
# ==================== RUBRIC ====================
|
| 21 |
+
RUBRIC_PROMPT = """
|
| 22 |
+
You are a teacher grading a student's summary. Follow these steps:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
1. Decide if you KNOW the story.
|
| 25 |
+
- If you know it, "knows_story": true. Otherwise false.
|
| 26 |
+
|
| 27 |
+
2. Grade the following:
|
| 28 |
+
- Comprehension (1β5 stars) β only if knows_story=true
|
| 29 |
+
- Expression (1β5 stars)
|
| 30 |
+
- Reflection/Moral Takeaway (1β5 stars)
|
| 31 |
+
- Grammar (lenient): identify grammar/spelling/punctuation issues; ignore casual or stylistic ones.
|
| 32 |
+
|
| 33 |
+
Return valid JSON:
|
| 34 |
{
|
| 35 |
"knows_story": true/false,
|
| 36 |
"scores": {
|
|
|
|
| 39 |
"Reflection/Moral Takeaway": 1-5
|
| 40 |
},
|
| 41 |
"grammar_issues": [
|
| 42 |
+
{"error_excerpt": "...", "correction": "...", "explanation": "...", "start_index": 0, "end_index": 0}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
],
|
| 44 |
+
"corrected_summary": "<corrected passage>",
|
| 45 |
+
"remarks": "<brief remark>"
|
| 46 |
}
|
|
|
|
|
|
|
| 47 |
"""
|
| 48 |
|
| 49 |
+
# ==================== LLM BRIDGE ====================
|
| 50 |
+
def llm_chat(messages):
|
| 51 |
+
"""Try DeepSeek first, then OpenAI."""
|
| 52 |
+
try:
|
| 53 |
+
if DEEPSEEK_API_KEY:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
ds = OpenAI(api_key=DEEPSEEK_API_KEY, base_url=DEEPSEEK_BASE_URL)
|
| 55 |
resp = ds.chat.completions.create(model=DEEPSEEK_MODEL, messages=messages)
|
| 56 |
return resp.choices[0].message.content, f"DeepSeek ({DEEPSEEK_MODEL})"
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print("DeepSeek failed:", e)
|
| 59 |
|
| 60 |
if OPENAI_API_KEY:
|
| 61 |
try:
|
|
|
|
| 63 |
resp = oa.chat.completions.create(model=OPENAI_MODEL, messages=messages)
|
| 64 |
return resp.choices[0].message.content, f"OpenAI ({OPENAI_MODEL})"
|
| 65 |
except Exception as e:
|
| 66 |
+
print("OpenAI failed:", e)
|
| 67 |
|
| 68 |
+
raise RuntimeError("No LLM API available. Please set DEEPSEEK_API_KEY or OPENAI_API_KEY.")
|
|
|
|
|
|
|
| 69 |
|
|
|
|
| 70 |
def salvage_json(s: str) -> dict:
|
| 71 |
s = s.strip()
|
| 72 |
try:
|
| 73 |
return json.loads(s)
|
| 74 |
+
except:
|
| 75 |
m = re.search(r"\{.*\}\s*$", s, flags=re.S)
|
| 76 |
+
return json.loads(m.group(0)) if m else {"error": "bad_json"}
|
| 77 |
+
|
| 78 |
+
def stars_bar(val):
|
| 79 |
+
if not val or val <= 0: return "β"
|
| 80 |
+
return "β" * int(round(val))
|
| 81 |
+
|
| 82 |
+
def count_words(text): return len(re.findall(r"\b\w[\w'-]*\b", text))
|
| 83 |
+
|
| 84 |
+
def length_stars(wc):
|
| 85 |
+
if wc < 100: return 0
|
| 86 |
+
if wc <= 150: return 1
|
| 87 |
+
if wc <= 200: return 3
|
| 88 |
+
if wc <= 250: return 4
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
return 5
|
| 90 |
|
| 91 |
+
def grammar_stars(n):
|
| 92 |
+
return 0 if n >= 50 else round(5 - 0.1 * n, 1)
|
| 93 |
|
| 94 |
+
# ==================== GOOGLE SHEETS SAVE ====================
|
| 95 |
+
def append_to_sheet(row: list):
|
| 96 |
+
"""Try to append row. If not configured, skip gracefully."""
|
| 97 |
+
if not (SVC_JSON and SHEET_ID and WS_ID):
|
| 98 |
+
return "πΎ Save skipped: Google Sheets not configured."
|
| 99 |
|
| 100 |
+
try:
|
| 101 |
+
import gspread
|
| 102 |
+
from google.oauth2.service_account import Credentials
|
| 103 |
+
creds = Credentials.from_service_account_info(json.loads(SVC_JSON),
|
| 104 |
+
scopes=["https://www.googleapis.com/auth/spreadsheets"])
|
| 105 |
+
gc = gspread.authorize(creds)
|
| 106 |
+
ws = gc.open_by_key(SHEET_ID).get_worksheet_by_id(int(WS_ID))
|
| 107 |
+
ws.append_row(row, value_input_option="USER_ENTERED")
|
| 108 |
+
return "β
Saved to Google Sheet."
|
| 109 |
+
except Exception as e:
|
| 110 |
+
return f"β Save failed: {e}"
|
| 111 |
+
|
| 112 |
+
# ==================== INTRO MESSAGE ====================
|
| 113 |
+
def intro_message(name, klass):
|
| 114 |
+
if not name.strip() or not klass.strip():
|
| 115 |
+
return "π Please tell me your name and class first!"
|
| 116 |
+
return f"""
|
| 117 |
+
### π Hi {name} of {klass}!
|
| 118 |
+
Welcome to the **Summary Grader!**
|
| 119 |
+
|
| 120 |
+
Hereβs how Iβll mark your story:
|
| 121 |
+
|
| 122 |
+
1οΈβ£ **Comprehension** β Did you really understand the story?
|
| 123 |
+
2οΈβ£ **Expression** β How clearly you write and explain.
|
| 124 |
+
3οΈβ£ **Reflection / Moral** β What did you learn or feel?
|
| 125 |
+
4οΈβ£ **Grammar & Neatness** β Spelling and punctuation.
|
| 126 |
+
5οΈβ£ **Length** β
|
| 127 |
+
- Less than 100 words β 0β
|
| 128 |
+
- 100β150 words β 1β
|
| 129 |
+
- 150β200 words β 3β
|
| 130 |
+
- 200β250 words β 4β
|
| 131 |
+
- 250+ words β 5β
|
| 132 |
+
|
| 133 |
+
π Write in your own words, think deeply, and use neat sentences.
|
| 134 |
+
Paste your story below when ready and click **Grade My Work!**
|
| 135 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
+
# ==================== MAIN GRADER ====================
|
| 138 |
+
def grade_summary(name, klass, title, summary):
|
| 139 |
+
if not all([name.strip(), klass.strip(), title.strip(), summary.strip()]):
|
| 140 |
+
return "β οΈ Please fill all fields.", "", ""
|
| 141 |
|
| 142 |
+
prompt = f"""
|
| 143 |
+
Title: {title}
|
| 144 |
|
| 145 |
+
Student Summary:
|
| 146 |
+
{summary}
|
| 147 |
|
| 148 |
+
{RUBRIC_PROMPT}
|
| 149 |
"""
|
| 150 |
+
content, model_used = llm_chat([{"role": "user", "content": prompt}])
|
|
|
|
| 151 |
data = salvage_json(content)
|
| 152 |
|
| 153 |
+
knows_story = data.get("knows_story", False)
|
| 154 |
+
scores = data.get("scores", {})
|
| 155 |
+
comp = scores.get("Comprehension")
|
| 156 |
+
expr = scores.get("Expression", 1)
|
| 157 |
+
refl = scores.get("Reflection/Moral Takeaway", 1)
|
| 158 |
+
issues = data.get("grammar_issues", [])
|
| 159 |
+
corrected = data.get("corrected_summary", "")
|
| 160 |
+
remarks = data.get("remarks", "")
|
| 161 |
+
|
| 162 |
+
wc = count_words(summary)
|
| 163 |
+
len_star = length_stars(wc)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
grammar_count = len(issues)
|
| 165 |
+
grammar_star = grammar_stars(grammar_count)
|
| 166 |
|
| 167 |
+
values = [expr, refl, len_star, grammar_star]
|
| 168 |
+
if knows_story and comp: values.append(comp)
|
| 169 |
+
avg = round(sum(values) / len(values), 2)
|
|
|
|
|
|
|
| 170 |
|
|
|
|
| 171 |
md = []
|
| 172 |
+
md.append(f"### π Summary Report for {name} (Class {klass})")
|
| 173 |
md.append(f"**Title:** {title}")
|
|
|
|
| 174 |
md.append(f"**Model used:** `{model_used}`")
|
| 175 |
md.append("")
|
| 176 |
+
md.append("#### β Category-wise Scores")
|
| 177 |
+
md.append(f"- Comprehension: {stars_bar(comp)} ({comp if comp else 'β'})")
|
| 178 |
+
md.append(f"- Expression: {stars_bar(expr)} ({expr})")
|
| 179 |
+
md.append(f"- Reflection/Moral: {stars_bar(refl)} ({refl})")
|
| 180 |
+
md.append(f"- Length: {stars_bar(len_star)} ({len_star} from {wc} words)")
|
| 181 |
+
md.append(f"- Grammar: {stars_bar(grammar_star)} ({grammar_star} from {grammar_count} mistakes)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
md.append("")
|
| 183 |
+
md.append(f"**Final Average:** {avg} {stars_bar(avg)} out of 5 β")
|
|
|
|
|
|
|
| 184 |
md.append("")
|
| 185 |
+
md.append(f"**Remarks:** {remarks}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
md.append("")
|
| 187 |
+
md.append(f"**Corrected passage:**\n\n{corrected}")
|
| 188 |
+
report = "\n".join(md)
|
| 189 |
+
|
| 190 |
+
# Google Sheet save
|
| 191 |
+
row = [time.strftime("%Y-%m-%d %H:%M:%S"), name, klass, title, wc,
|
| 192 |
+
"Yes" if knows_story else "No", comp or "", expr, refl,
|
| 193 |
+
len_star, grammar_count, grammar_star, avg, model_used, remarks]
|
| 194 |
+
status = append_to_sheet(row)
|
| 195 |
|
| 196 |
+
return report, corrected, status
|
|
|
|
| 197 |
|
| 198 |
# ==================== UI ====================
|
| 199 |
+
with gr.Blocks(title="Summary Grader (DeepSeek β OpenAI)") as demo:
|
| 200 |
+
gr.Markdown("## β¨ Summary Grader\nDeepSeek first, OpenAI fallback. Saves to Google Sheet if configured.")
|
| 201 |
+
|
| 202 |
with gr.Row():
|
| 203 |
+
name = gr.Textbox(label="Student Name")
|
| 204 |
+
klass = gr.Textbox(label="Class/Grade")
|
| 205 |
+
|
| 206 |
+
intro_out = gr.Markdown()
|
| 207 |
+
gr.Button("Show How Iβll Mark You").click(intro_message, [name, klass], intro_out)
|
| 208 |
+
|
| 209 |
with gr.Row():
|
| 210 |
+
title = gr.Textbox(label="Book/Movie Title", placeholder="The Gift of the Magi")
|
| 211 |
+
summary = gr.Textbox(label="Your Summary", lines=10, placeholder="Paste here...")
|
| 212 |
+
|
| 213 |
+
report_out = gr.Markdown()
|
| 214 |
+
corrected_out = gr.Textbox(label="Corrected Text")
|
| 215 |
+
status_out = gr.Markdown()
|
| 216 |
|
| 217 |
+
gr.Button("Grade My Work!").click(grade_summary,
|
| 218 |
+
[name, klass, title, summary],
|
| 219 |
+
[report_out, corrected_out, status_out])
|
| 220 |
|
|
|
|
| 221 |
if __name__ == "__main__":
|
| 222 |
demo.launch()
|
|
|