celik-muhammed commited on
Commit
1b792a3
Β·
verified Β·
1 Parent(s): b4423d9

Upload 2 files

Browse files
Files changed (2) hide show
  1. _dataset_schema.py +21 -3
  2. app.py +7 -12
_dataset_schema.py CHANGED
@@ -168,6 +168,18 @@ _QUICK_OPTS: list[dict[str, Any]] = [
168
  {"slug": "helpful", "title": "Helpful", "value": +1, "sentiment": "positive"},
169
  ]
170
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  # Derived lookup tables.
172
  _SLUG_TO_TITLE: dict[str, str] = {
173
  **{e["slug"]: e["title"] for e in _PANEL_SCALE},
@@ -309,7 +321,13 @@ def normalize_rating(
309
  elif label_str in _KNOWN_TITLES:
310
  detected_mode = "quick"
311
  elif label_str and _SLUG_RE.match(label_str):
312
- detected_mode = "panel"
 
 
 
 
 
 
313
  elif rating_value in (-1, 1) and not label_str:
314
  detected_mode = "quick"
315
  else:
@@ -546,8 +564,8 @@ def normalize_contribution_record(
546
  rating_fields = normalize_rating(
547
  rec.get("ratingValue"),
548
  rec.get("ratingLabel"),
549
- rating_mode=rec.get("ratingMode"), # new JS field (None if old)
550
- rating_title=rec.get("ratingTitle"), # new JS field (None if old)
551
  feedback_id=None, # no per-event session ID for contributions
552
  )
553
 
 
168
  {"slug": "helpful", "title": "Helpful", "value": +1, "sentiment": "positive"},
169
  ]
170
 
171
+ #: Set of slug values associated with quick (πŸ‘/πŸ‘Ž) feedback options.
172
+ #: Disjoint from all panel slugs β€” used for deterministic ratingMode detection
173
+ #: when ``ratingMode`` is not explicitly provided in the payload (old records).
174
+ _QUICK_SLUGS: frozenset[str] = frozenset(e["slug"] for e in _QUICK_OPTS)
175
+
176
+ #: Set of sentiment strings used as quick feedback mode indicators.
177
+ #: Old records written before the slug fix may carry "positive"/"negative" here.
178
+ _QUICK_SENTIMENTS: frozenset[str] = frozenset(e["sentiment"] for e in _QUICK_OPTS)
179
+
180
+ #: All identifiers that unambiguously indicate quick (πŸ‘/πŸ‘Ž) rating mode.
181
+ _QUICK_IDENTIFIERS: frozenset[str] = _QUICK_SLUGS | _QUICK_SENTIMENTS
182
+
183
  # Derived lookup tables.
184
  _SLUG_TO_TITLE: dict[str, str] = {
185
  **{e["slug"]: e["title"] for e in _PANEL_SCALE},
 
321
  elif label_str in _KNOWN_TITLES:
322
  detected_mode = "quick"
323
  elif label_str and _SLUG_RE.match(label_str):
324
+ # Slug-based mode detection: quick slugs ("helpful", "not_helpful")
325
+ # and panel slugs ("mostly_positive", "excellent", …) are disjoint
326
+ # sets β€” membership check is sufficient and deterministic.
327
+ # This handles contribution records where _feedbackStore.ratingMode
328
+ # is forwarded in ratingMode (new JS) but also back-compats old
329
+ # records that only carried ratingLabel (slug or Title Case).
330
+ detected_mode = "quick" if label_str in _QUICK_IDENTIFIERS else "panel"
331
  elif rating_value in (-1, 1) and not label_str:
332
  detected_mode = "quick"
333
  else:
 
564
  rating_fields = normalize_rating(
565
  rec.get("ratingValue"),
566
  rec.get("ratingLabel"),
567
+ rating_mode=rec.get("ratingMode"), # from _feedbackStore.ratingMode (new JS)
568
+ rating_title=rec.get("ratingTitle"), # from _feedbackStore.ratingTitle (new JS)
569
  feedback_id=None, # no per-event session ID for contributions
570
  )
571
 
app.py CHANGED
@@ -1309,7 +1309,7 @@ async def contribute(request: Request) -> JSONResponse: # noqa: PLR0912
1309
  path_or_fileobj=rows_jsonl.encode(),
1310
  )
1311
  ],
1312
- commit_message=f"Add {len(records)} feedback record(s)",
1313
  )
1314
  except Exception as exc:
1315
  logger.error(json.dumps({"event": "contribute.hf_fail", "error": str(exc)}))
@@ -1829,18 +1829,13 @@ async def feedback(request: Request) -> JSONResponse:
1829
  # The resulting record is structurally identical to a contribution row
1830
  # so both sources concatenate directly into one pandas DataFrame.
1831
  # See _dataset_schema.py for the full canonical column list.
1832
- record = json.dumps(
1833
- normalize_feedback_record(
1834
- payload,
1835
- server_ts_ms=int(_time.time() * 1000),
1836
- ),
1837
- ensure_ascii=False,
1838
  )
1839
- # Derive dedup_key from the normalised record (avoids duplicating logic).
1840
- import json as _json # noqa: PLC0415 β€” already imported at module scope
1841
- _rec_dict = _json.loads(record)
1842
- conversation_id = _rec_dict.get("conversationId") or ""
1843
- answer_index = _rec_dict.get("answerIndex", "")
1844
  filename = f"feedback/{int(_time.time() * 1000)}.jsonl"
1845
  api = HfApi(token=HF_DATASET_TOKEN)
1846
  # Fix 3 (feedback): same event-loop fix as /v1/contribute β€” offload
 
1309
  path_or_fileobj=rows_jsonl.encode(),
1310
  )
1311
  ],
1312
+ commit_message=f"Add {len(records)} contribution record(s)",
1313
  )
1314
  except Exception as exc:
1315
  logger.error(json.dumps({"event": "contribute.hf_fail", "error": str(exc)}))
 
1829
  # The resulting record is structurally identical to a contribution row
1830
  # so both sources concatenate directly into one pandas DataFrame.
1831
  # See _dataset_schema.py for the full canonical column list.
1832
+ _rec_dict: dict = normalize_feedback_record(
1833
+ payload,
1834
+ server_ts_ms=int(_time.time() * 1000),
 
 
 
1835
  )
1836
+ record: str = json.dumps(_rec_dict, ensure_ascii=False)
1837
+ conversation_id: str = str(_rec_dict.get("conversationId") or "")
1838
+ answer_index: Any = _rec_dict.get("answerIndex", "")
 
 
1839
  filename = f"feedback/{int(_time.time() * 1000)}.jsonl"
1840
  api = HfApi(token=HF_DATASET_TOKEN)
1841
  # Fix 3 (feedback): same event-loop fix as /v1/contribute β€” offload