Download apps/api/app/services/case_study/html_case.py from aliroya/contiai: direct link, hf CLI and curl.
- Browser
- Download file 12.9 kB
-
https://huggingface.co/spaces/aliroya/contiai/resolve/main/apps/api/app/services/case_study/html_case.py
- Command line
-
hf download hf://spaces/aliroya/contiai/apps/api/app/services/case_study/html_case.py
-
curl -L -o html_case.py https://huggingface.co/spaces/aliroya/contiai/resolve/main/apps/api/app/services/case_study/html_case.py
12.9 kB
| """The HTML export - cell [20]. | |
| Ported from `case_study_pipeline.ipynb`. The notebook was one namespace, so | |
| each module re-imports the ones before it; every prompt and every standard is | |
| the notebook's text, compared character by character by the fidelity test. | |
| """ | |
| from __future__ import annotations | |
| import asyncio # noqa: F401 | |
| import datetime # noqa: F401 | |
| import html # noqa: F401 | |
| import json # noqa: F401 | |
| import pathlib # noqa: F401 | |
| import re # noqa: F401 | |
| from typing import Any, Callable, Literal, Optional # noqa: F401 | |
| from pydantic import ( # noqa: F401 | |
| BaseModel, | |
| ConfigDict, | |
| Field, | |
| ValidationError, | |
| model_validator, | |
| ) | |
| from app.agents.case_study.helpers import * # noqa: F403 | |
| from app.agents.case_study.profiles import * # noqa: F403 | |
| from app.agents.case_study.standards import * # noqa: F403 | |
| # The notebook was one namespace; the exporters read it too. | |
| from app.agents.case_study.llms import RUN_CONFIG # noqa: F401 | |
| # `_answer_lines` is defined in the Word exporter's cell and used by both. | |
| from app.services.case_study.docx_case import _answer_lines | |
| def _e(s) -> str: | |
| return html.escape(str(s or "")) | |
| _HTML_CSS = """ | |
| :root{--ink:#1d2433;--muted:#667085;--line:#d0d7e2;--brand:#1f4e79;--soft:#eef4fb;--ok:#e2efda;--warn:#fff2cc;--act:#c55a11;--bg:#f6f8fb;--card:#fff} | |
| *{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--ink);font:15px/1.8 "Noto Naskh Arabic","Traditional Arabic",Tahoma,Arial,sans-serif} | |
| main{max-width:1180px;margin:0 auto;padding:24px 16px 64px}h1{font-size:26px;margin:0 0 4px;color:var(--brand)} | |
| .sub{color:var(--muted);margin:0 0 24px}h2{font-size:19px;color:var(--brand);margin:32px 0 12px;border-bottom:2px solid var(--brand);padding-bottom:4px} | |
| table{width:100%;border-collapse:collapse;background:var(--card);table-layout:fixed}td,th{border:1px solid var(--line);padding:8px 10px;vertical-align:top;text-align:start;word-wrap:break-word} | |
| th{background:var(--brand);color:#fff}.lab{background:var(--soft);font-weight:700;width:24%}.num{width:8%;text-align:center;font-weight:700;font-size:20px} | |
| .kind{display:block;font-size:11px;color:var(--muted);font-weight:400}.vo{width:40%}.ln{margin:0 0 4px}.idx{color:#98a2b3;font-size:11px;margin-inline-end:4px} | |
| td p{margin:0 0 6px}.dlg b{color:#7030a0}.bul::before{content:"▪ ";color:var(--brand)}.strong{font-weight:700}.center{text-align:center} | |
| .inter-title{color:var(--act);font-weight:700;margin-bottom:6px}.itab td{font-size:14px}.ok{background:var(--ok);font-weight:700}.fb{background:var(--warn);font-weight:700} | |
| .btn{display:inline-block;border:1px solid var(--brand);border-radius:6px;padding:0 10px;color:var(--brand);font-weight:700} | |
| .k{color:var(--brand);font-weight:700}.freeze{color:#bf8f00;font-weight:700;margin-top:6px}ul{margin:4px 0;padding-inline-start:20px} | |
| .beat{margin:0 0 4px;font-size:14px}.note{color:var(--muted);font-style:italic}details{margin-top:24px;background:var(--card);border:1px solid var(--line);padding:10px 14px;border-radius:8px} | |
| @media (max-width:760px){table,tbody,tr,td,th{display:block;width:100%!important}thead{display:none}td{border-top:none}.num{text-align:start}} | |
| """ | |
| def _answers_html(it: dict, fam: str) -> str: | |
| return "".join(f"<div>{_e(a)}</div>" for a in _answer_lines(it, fam)) | |
| def _interaction_html(sc: dict) -> str: | |
| it, fam = sc["interaction"], sc["interaction_family"] | |
| rows = [f"<tr><td colspan=2><div class=k>{_e(it['header'])}</div><div class=strong>{_e(it['situation_sentence'] + ' ' + it['question'])}</div></td></tr>"] | |
| if fam == "choice" and len(it["items"]) == 2: | |
| rows.append("<tr>" + "".join(f"<td>{'أب'[i]}. {_e(x['text'])}</td>" for i, x in enumerate(it["items"])) + "</tr>") | |
| else: | |
| mark = {"multi_select": "🔘", "reveal": "▣", "poll": "○"}.get(fam, "▫") | |
| cats = f"<div class=strong>الفئات: {_e(' | '.join(it['categories']))}</div>" if fam == "categorize" else "" | |
| rows.append("<tr><td colspan=2>" + cats + "".join(f"<div>{mark} {_e(x['text'])}</div>" for x in it["items"]) + "</td></tr>") | |
| rows.append(f"<tr><td colspan=2 class=strong>{_e(it['instruction'])}" + (f"<div>{_e(it['hint_sentence'])}</div>" if it.get("hint_sentence") else "") + "</td></tr>") | |
| if it.get("hint"): | |
| rows.append("<tr><td colspan=2><b>💡 تلميح (Hint):</b><ul>" + "".join(f"<li>{_e(h)}</li>" for h in it["hint"]) + "</ul></td></tr>") | |
| rows.append(f"<tr><td colspan=2 class=ok>✅ الإجابة الصحيحة</td></tr><tr><td colspan=2>{_answers_html(it, fam)}</td></tr>") | |
| if it.get("check_button"): | |
| rows.append(f"<tr><td colspan=2 class=center><span class=btn>{_e(it['check_button_label'])}</span></td></tr>") | |
| rows.append("<tr><td colspan=2 class=fb>التغذية الراجعة</td></tr>") | |
| fc, fi = it["feedback_correct"], it.get("feedback_incorrect") | |
| if fi: | |
| rows.append(f"<tr><td>✅<div class=strong>{_e(fc['opener'])}</div>{_e(fc['text'])}</td>" | |
| f"<td>❌{' (بعد استنفاد المحاولات)' if it.get('feedback_retry') else ''}<div class=strong>{_e(fi['opener'])}</div>{_e(fi['text'])}" | |
| + "".join(f"<p class=note>({_e(x['id'])}) {_e(x['feedback'])}</p>" for x in it["items"] if x.get("feedback") and not x.get("is_correct")) | |
| + "</td></tr>") | |
| else: | |
| rows.append(f"<tr><td colspan=2>✅ تعليق ختامي<div class=strong>{_e(fc['opener'])}</div>{_e(fc['text'])}</td></tr>") | |
| if it.get("feedback_retry"): | |
| r = it["feedback_retry"] | |
| rows.append(f"<tr><td colspan=2>❌ المحاولة الأولى الخاطئة (مع إتاحة المحاولة الثانية)<div class=strong>{_e(r['opener'])}</div>{_e(r['text'])}</td></tr>") | |
| mech = "<div class=k>آلية التفاعل التقني:</div><ul>" + "".join(f"<li>{_e(m)}</li>" for m in it["mechanism"]) + "</ul>" | |
| return (f"<div class=inter-title>⏸ شاشة تفاعل: {_e(it['type_ar'])}</div><p class=note>المحاولات المتاحة: {it['attempts']}</p>" | |
| f"<table class=itab>{''.join(rows)}</table>{mech}") | |
| def _vo_html(sc: dict) -> str: | |
| out = [] | |
| for i, ln in enumerate(sc["lines"]): | |
| cls = {"bullet": "ln bul", "closing_sentence": "ln strong", "thanks": "ln strong center", "dialogue": "ln dlg"}.get(ln["role"], "ln") | |
| sp = f"<b>{_e(ln['speaker'])}:</b> " if ln["role"] == "dialogue" else "" | |
| out.append(f"<p class='{cls}'><span class=idx>[{i + 1}]</span>{sp}{_e(ln['text'])}</p>") | |
| return "".join(out) | |
| def _desc_html(sc: dict) -> str: | |
| v = sc["visual"] | |
| if not v: | |
| return "" | |
| out = ["<div class=k>وصف المشهد:</div><div class=k>التتابع البصري (beat لكل سطر):</div>"] | |
| for b in v["beats"]: | |
| parts = [b["visual_idea"], f"اللقطة: {b['shot']}", f"الحركة: {b['action']}"] | |
| if b.get("on_screen_text"): | |
| parts.append(f"على الشاشة: «{b['on_screen_text']}»" + (f" ({b['text_position']})" if b.get("text_position") else "")) | |
| parts += [f"{lab}: {b[k]}" for k, lab in (("effect", "التأثير"), ("sfx", "الصوت"), ("transition", "الانتقال")) if b.get(k)] | |
| out.append(f"<p class=beat><span class=idx>[{b['line_index'] + 1}]</span>{_e(' — '.join(parts))}</p>") | |
| out.append(f"<p><span class=k>الوصف التنفيذي:</span> {_e(v['narrative'])}</p>") | |
| d = v["description"] | |
| for k, lab in (("background", "الخلفية والبيئة"), ("characters", "الشخصيات وتعبيرات الوجه"), ("actions", "الحركات والأفعال"), | |
| ("shots", "اللقطات والزوايا"), ("visual_effects", "المؤثرات البصرية"), ("sound_effects", "المؤثرات الصوتية"), | |
| ("graphics_notes", "ملحوظات لفريق الجرافيك")): | |
| out.append(f"<p><span class=k>{lab}:</span> {_e(d[k])}</p>") | |
| iv = v.get("interaction_visual") | |
| if iv: | |
| out.append(f"<div class=inter-title>مشهد التفاعل</div><p><span class=k>إطار السؤال:</span> {_e(iv['question_frame'])}</p>") | |
| out.append("<div class=k>بطاقات العناصر/الخيارات:</div><ul>" + "".join(f"<li>({_e(o['item_id'])}) {_e(o['visual'])}</li>" for o in iv["option_cards"]) + "</ul>") | |
| for key, lab in (("instruction_frame", "إطار الجملة التوجيهية"), ("hint_visual", "التلميح 💡"), ("check_button_visual", "زر التحقق"), | |
| ("feedback_correct_visual", "✅ مشهد التغذية الإيجابية"), ("feedback_incorrect_visual", "❌ مشهد التغذية السلبية")): | |
| if iv.get(key): | |
| out.append(f"<p><span class=k>{lab}:</span> {_e(iv[key])}</p>") | |
| if iv.get("mechanism_visual"): | |
| out.append("<ul>" + "".join(f"<li>{_e(m)}</li>" for m in iv["mechanism_visual"]) + "</ul>") | |
| if v.get("reference_suggestions"): | |
| out.append("<ul>" + "".join(f"<li>مرجع بصري مقترح: {_e(r)}</li>" for r in v["reference_suggestions"]) + "</ul>") | |
| if v.get("freeze_at_end"): | |
| out.append(f"<div class=freeze>⏸ تتجمّد الشاشة عند هذه اللحظة: {_e(v.get('freeze_description'))}</div>") | |
| return "".join(out) | |
| def export_html(content: dict, path: Optional[str] = None) -> pathlib.Path: | |
| path = pathlib.Path(path or out_path("case_study_script.html")) | |
| m, cv = content["meta"], content["covers"] | |
| cc, es = cv["case_cover"], cv["end_screen"] | |
| lst = lambda xs: "<ul>" + "".join(f"<li>{_e(x)}</li>" for x in xs) + "</ul>" | |
| t1 = "".join(f"<tr><td class=lab>{lab}</td><td>{val}</td></tr>" for lab, val in ( | |
| ("نوع العنصر", _e(m["type_label"])), | |
| ("اسم العنصر (المحور الرئيس)", _e(m["title"])), ("المحاور الفرعية", lst(m["axes"])), ("الأهداف التعليمية", lst(m["objectives"])))) | |
| icons = "".join(f"<li>الأيقونة {i} (من المشهد {ic['scene_number']}): {_e(ic['description'])}</li>" for i, ic in enumerate(cc["icons"], 1)) | |
| rows = [f"""<tr><td class=num>الغلاف</td><td class=vo><p><span class=k>غلاف دراسة الحالة:</span></p><p class='strong center'>{_e(cc['title'])}</p> | |
| <p>والـ Header: <b>{_e(cc['header'])}</b></p><p>مع زر: {' '.join(f'<span class=btn>{_e(b)}</span>' for b in cc['buttons'])}</p></td> | |
| <td><p><span class=k>غلاف دراسة الحالة — التصميم:</span> {_e(cc['character_design'])}</p><ul>{icons}</ul> | |
| <p class=note>جميع الأيقونات مستخرجة من مشاهد حقيقية داخل دراسة الحالة نفسها لضمان التكامل البصري.</p></td></tr>"""] | |
| for sc in content["scenes"]: | |
| vo = _interaction_html(sc) if sc["interaction"] else _vo_html(sc) | |
| rows.append(f"<tr><td class=num>{sc['scene_number']}<span class=kind>{_e(sc['kind_ar'])}</span></td>" | |
| f"<td class=vo>{vo}</td><td>{_desc_html(sc)}</td></tr>") | |
| thanks = "".join(f"<p class='strong center'>{_e(t)}</p>" for t in es["thanks_lines"]) | |
| end_btns = ("<p>مع زر: " + " ".join(f"<span class=btn>{_e(b)}</span>" for b in es["buttons"]) + "</p>") if es.get("buttons") else "" | |
| rows.append(f"<tr><td class=num>الختام</td><td class=vo>{thanks}{end_btns}</td>" | |
| f"<td><p><span class=k>شاشة الختام:</span> {_e(es['design'])}</p><p><span class=k>عند الضغط:</span> {_e(es.get('return_to'))}</p></td></tr>") | |
| qa_items = content.get("qa", {}).get("qa_log", []) | |
| qa_html = "".join(f"<li><b>{_e(q['step'])}</b> ({_e(q['kind'])}): {_e('؛ '.join(q['messages']))}</li>" for q in qa_items) or "<li>لا ملاحظات</li>" | |
| doc = f"""<!doctype html><html lang="ar" dir="rtl"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"> | |
| <title>{_e(m['title'])}</title><style>{_HTML_CSS}</style></head><body><main> | |
| <h1>إسكربت {_e(m['type_label'])}: {_e(m['title'])}</h1><p class=sub>{_e(m['profile_label'])} — {m['actual_words']} كلمة مسموعة (المستهدف {m['target_words']})</p> | |
| <h2>الجدول الأول: المعلومات التوصيفية</h2><table>{t1}</table> | |
| <h2>الجدول الثاني: النص المسموع (VO) ووصف المشاهد</h2> | |
| <table><thead><tr><th class=num style="font-size:14px">رقم المشهد</th><th class=vo>النص المسموع (VO)</th><th>وصف المشاهد لفريق الجرافيك</th></tr></thead> | |
| <tbody>{''.join(rows)}</tbody></table> | |
| <h2>ملحوظات مهمة لفريق الجرافيك والإنتاج</h2>{lst(content['production_notes'])} | |
| <details><summary>تقرير الجودة (ملاحظات مقبولة / ضبط الطول)</summary><ul>{qa_html}</ul></details> | |
| </main></body></html>""" | |
| log(f"✓ HTML: {path}") | |
| return doc | |