Spaces:
Sleeping
Sleeping
Sync from GitHub f548297062c1b82c3ebf9d03b3ebc9992ba65cf7
Browse filesFiles changed (2)
131 unmodified lines1321331341351361377 unmodified lines14514614714814915052 unmodified lines20320420520620720820918 unmodified lines228229230231232233234131 unmodified linesdef yes_no_labels(value: str) -> list[str]:text = str(value or "").strip().lower()if not text:return []7 unmodified linesreturn labels if len(labels) >= 2 else []def option_text(options: list[str], label: str) -> str:label = option_label(label)for index, option in enumerate(options, 1):52 unmodified linesanswer = question.answer or ""question_type = (question.question_type or "").lower()options = normalize_options(question.get_options()) or extract_options_from_stem(question.stem)if question_type in {"hotspot", "table_choice", "matching", "ordering", "yes_no"}:analysis = visual_analysis(question)statements = analysis.get("statements")if isinstance(statements, list):18 unmodified lineslabels = option_labels_from_texts(options, selected, allow_duplicates=True)if labels:return ",".join(labels)if question_type in {"hotspot", "table_choice"} and not yes_no_labels(answer):explanation_labels = yes_no_labels(question.explanation or "")if explanation_labels:return ",".join(explanation_labels)131 unmodified lines1321331341351361371381391401411421431441451461471481491501511521531541551561571581591607 unmodified lines16816917017117217317417517617717817918018118218318418518618718818919019119219319419552 unmodified lines24824925025125225325425525625725825926026126226326426526618 unmodified lines285286287288289290291131 unmodified linesdef yes_no_labels(value: str) -> list[str]:if isinstance(value, (list, dict)):raw_value = valueelse:raw_value = Nonetry:raw_value = json.loads(value) if isinstance(value, str) else Noneexcept Exception:raw_value = Noneif isinstance(raw_value, list):values = [item.get("value") or item.get("answer") or item.get("selected_answer")for item in raw_valueif isinstance(item, dict)]labels = yes_no_labels(",".join(str(item) for item in values if item))if labels:return labelselif isinstance(raw_value, dict):labels = yes_no_labels(",".join(str(item) for item in raw_value.values()))if labels:return labelstext = str(value or "").strip().lower()if not text:return []7 unmodified linesreturn labels if len(labels) >= 2 else []def structured_answer_values(value) -> list[str]:if isinstance(value, (list, dict)):raw_value = valueelse:try:raw_value = json.loads(value) if isinstance(value, str) else Noneexcept Exception:raw_value = Noneif isinstance(raw_value, list):values = [item.get("value") or item.get("answer") or item.get("selected_answer")for item in raw_valueif isinstance(item, dict)]elif isinstance(raw_value, dict):values = list(raw_value.values())else:return []return [str(item).strip() for item in values if str(item).strip()]def option_text(options: list[str], label: str) -> str:label = option_label(label)for index, option in enumerate(options, 1):52 unmodified linesanswer = question.answer or ""question_type = (question.question_type or "").lower()options = normalize_options(question.get_options()) or extract_options_from_stem(question.stem)structured_values = structured_answer_values(answer)if structured_values:structured_yn = yes_no_labels(",".join(structured_values))return ",".join(structured_yn or structured_values)is_structured_visual = any(keyword in question_typefor keyword in ["hotspot", "table_choice", "matching", "ordering", "yes_no", "true/false"]) and "in-context" not in question_typeif is_structured_visual:answer_labels = yes_no_labels(answer)if answer_labels and "true/false" in question_type:return ",".join(answer_labels)analysis = visual_analysis(question)statements = analysis.get("statements")if isinstance(statements, list):18 unmodified lineslabels = option_labels_from_texts(options, selected, allow_duplicates=True)if labels:return ",".join(labels)if is_structured_visual and not yes_no_labels(answer):explanation_labels = yes_no_labels(question.explanation or "")if explanation_labels:return ",".join(explanation_labels)
679 unmodified lines6806816826836846855 unmodified lines6916926936946956966976987 unmodified lines70670770870971071149 unmodified lines761762763764765766767768769770771772679 unmodified linesdef yes_no_answer_labels(value: str) -> list[str]:text = str(value or "")if re.fullmatch(r"\s*[YNyn](?:\s*,\s*[YNyn])+\s*", text):return [token.upper() for token in re.findall(r"[YNyn]", text)]5 unmodified linesdef is_yes_no_hotspot(question) -> bool:question_type = (question.get("question_type") or "").lower()if question_type not in {"hotspot", "table_choice"}:return Falsetext = " ".join([question.get("question") or "",7 unmodified lines)def visual_statements(question) -> list[dict]:try:analysis = json.loads(question.get("visual_analysis_json") or "{}")49 unmodified linesif statement_rows:return render_yes_no_matrix(question, f"{key_prefix}_statements", statement_rows)if is_yes_no_hotspot(question):answer_count = len(yes_no_answer_labels(question.get("answer") or "")) or len(yes_no_answer_labels(question.get("explanation") or ""))row_count = max(3, answer_count, len(statement_rows))rows = yes_no_lines(question.get("question") or "")if statement_rows:rows = statement_rowsif len(rows) < row_count:679 unmodified lines6806816826836846856866876886896906916926936946956966976986997007017027037047057067077085 unmodified lines7147157167177187197207217227237247257 unmodified lines73373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679749 unmodified lines847848849850851852853854855856857858859860861862679 unmodified linesdef yes_no_answer_labels(value: str) -> list[str]:if isinstance(value, (list, dict)):raw_value = valueelse:raw_value = Nonetry:raw_value = json.loads(value) if isinstance(value, str) else Noneexcept Exception:raw_value = Noneif isinstance(raw_value, list):values = [item.get("value") or item.get("answer") or item.get("selected_answer")for item in raw_valueif isinstance(item, dict)]labels = yes_no_answer_labels(",".join(str(item) for item in values if item))if labels:return labelselif isinstance(raw_value, dict):labels = yes_no_answer_labels(",".join(str(item) for item in raw_value.values()))if labels:return labelstext = str(value or "")if re.fullmatch(r"\s*[YNyn](?:\s*,\s*[YNyn])+\s*", text):return [token.upper() for token in re.findall(r"[YNyn]", text)]5 unmodified linesdef is_yes_no_hotspot(question) -> bool:question_type = (question.get("question_type") or "").lower()if "in-context" in question_type:return Falseif not any(keyword in question_type for keyword in ["hotspot", "table_choice", "yes_no", "true/false"]):return Falseif "true/false" in question_type:return Truetext = " ".join([question.get("question") or "",7 unmodified lines)def statement_option_rows(options: list[str], expected_count: int) -> list[str]:rows = []for option in options or []:text = str(option or "").strip()if not text:continuecleaned = re.sub(r"^\s*(?:[0-9]+|[A-Z])[-.)]\s*", "", text).strip()if re.fullmatch(r"μ|μλμ€|μλμ|yes|no", cleaned, re.I):continuerows.append(cleaned)if expected_count and len(rows) >= expected_count:return rows[:expected_count]return rowsdef grouped_option_rows(options: list[str]) -> list[dict]:grouped = {}order = []for option in options or []:text = str(option or "").strip()match = re.match(r"^\s*(\d+)-([A-Z])[\.)]?\s*(.+)$", text, re.I)if not match:continuegroup_key = match.group(1)body = match.group(3).strip()row_label = f"νλͺ© {group_key}"value = bodyif ":" in body:row_label, value = [part.strip() for part in body.split(":", 1)]if group_key not in grouped:grouped[group_key] = {"label": row_label, "options": []}order.append(group_key)grouped[group_key]["options"].append(value)rows = [grouped[key] for key in order]return rows if len(rows) >= 2 and all(row["options"] for row in rows) else []def render_grouped_option_selects(question, key_prefix, rows):selections = []all_yes_no = all(all(str(option).strip().lower() in {"yes", "no", "μ", "μλμ€", "μλμ"} for option in row["options"])for row in rows)st.markdown("#### νλͺ©λ³ λ΅μ")for index, row in enumerate(rows, 1):options = [str(option).strip() for option in row["options"] if str(option).strip()]selected = st.selectbox(row["label"] or f"νλͺ© {index}",["μ ν μ ν¨"] + options,key=f"{key_prefix}_grouped_options_{question['id']}_{index}",)if selected != "μ ν μ ν¨":if all_yes_no:selections.append("Y" if selected.lower() in {"yes", "μ"} else "N")else:selections.append(selected)return ",".join(selections) if len(selections) == len(rows) else Nonedef visual_statements(question) -> list[dict]:try:analysis = json.loads(question.get("visual_analysis_json") or "{}")49 unmodified linesif statement_rows:return render_yes_no_matrix(question, f"{key_prefix}_statements", statement_rows)grouped_rows = grouped_option_rows(options)if grouped_rows:return render_grouped_option_selects(question, key_prefix, grouped_rows)if is_yes_no_hotspot(question):answer_count = len(yes_no_answer_labels(question.get("answer") or "")) or len(yes_no_answer_labels(question.get("explanation") or ""))row_count = max(3, answer_count, len(statement_rows))rows = statement_option_rows(options, row_count) or yes_no_lines(question.get("question") or "")if statement_rows:rows = statement_rowsif len(rows) < row_count:
cert_study_app/services/quiz_service.py+59-2
streamlit_app.py+92-2