Spaces:
Sleeping
Sleeping
Auto-form: highlight "AI" in the generate button + letter date defaults to today
Browse files- generate button: ":violet[AI]" markdown span restyled via scoped CSS (.st-key-generate_btn)
into a glowing white chip so the AI feature stands out.
- form_logic.fill_letter_date(): the header line "วันที่ ___ เดือน ___ พ.ศ." now fills with
today's date (Buddhist era) by default.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- form_logic.py +25 -1
- streamlit_app.py +14 -1
form_logic.py
CHANGED
|
@@ -8,10 +8,14 @@ Form Logic — pure logic for document generation (no UI dependencies)
|
|
| 8 |
|
| 9 |
import os
|
| 10 |
import re
|
|
|
|
| 11 |
from copy import deepcopy
|
| 12 |
from docx import Document
|
| 13 |
from docx.text.paragraph import Paragraph
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# =============================================================================
|
| 17 |
# ค่าคงที่: ข้อมูลแบบฟอร์ม
|
|
@@ -119,6 +123,22 @@ def remove_paragraphs_containing(doc, marker):
|
|
| 119 |
return count
|
| 120 |
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
def insert_body_paragraphs(doc, placeholder, body_text):
|
| 123 |
"""Replace the single `placeholder` paragraph with one paragraph per blank-line-separated
|
| 124 |
block of `body_text`, cloning the placeholder's formatting (alignment + first-line indent).
|
|
@@ -179,7 +199,7 @@ def clean_vendor_name(value):
|
|
| 179 |
# =============================================================================
|
| 180 |
def generate_form(form_id, template_path, output_path,
|
| 181 |
doc_number, po_number, doc_type, signer,
|
| 182 |
-
lookup_data=None, signer_position=None, body_text=None):
|
| 183 |
"""
|
| 184 |
สร้างเอกสาร Word จาก template
|
| 185 |
Args:
|
|
@@ -199,6 +219,10 @@ def generate_form(form_id, template_path, output_path,
|
|
| 199 |
|
| 200 |
doc = Document(template_path)
|
| 201 |
log = []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
vendor_name = clean_vendor_name((lookup_data or {}).get("VendorName"))
|
| 203 |
work_title = (lookup_data or {}).get("title") or ""
|
| 204 |
|
|
|
|
| 8 |
|
| 9 |
import os
|
| 10 |
import re
|
| 11 |
+
import datetime
|
| 12 |
from copy import deepcopy
|
| 13 |
from docx import Document
|
| 14 |
from docx.text.paragraph import Paragraph
|
| 15 |
|
| 16 |
+
_THAI_MONTHS = ["", "มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน",
|
| 17 |
+
"กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม"]
|
| 18 |
+
|
| 19 |
|
| 20 |
# =============================================================================
|
| 21 |
# ค่าคงที่: ข้อมูลแบบฟอร์ม
|
|
|
|
| 123 |
return count
|
| 124 |
|
| 125 |
|
| 126 |
+
def fill_letter_date(doc, when=None):
|
| 127 |
+
"""Fill the header date line 'วันที่ ___ เดือน ___ พ.ศ. ___' with `when` (default: today,
|
| 128 |
+
Buddhist era). Returns True if the line was found and filled."""
|
| 129 |
+
when = when or datetime.date.today()
|
| 130 |
+
new = (f"วันที่ {when.day} เดือน {_THAI_MONTHS[when.month]} "
|
| 131 |
+
f"พ.ศ. {when.year + 543}")
|
| 132 |
+
for p in _iter_paragraphs(doc):
|
| 133 |
+
t = p.text
|
| 134 |
+
if "วันที่" in t and "เดือน" in t and "พ.ศ." in t and p.runs:
|
| 135 |
+
p.runs[0].text = new
|
| 136 |
+
for r in p.runs[1:]:
|
| 137 |
+
r.text = ""
|
| 138 |
+
return True
|
| 139 |
+
return False
|
| 140 |
+
|
| 141 |
+
|
| 142 |
def insert_body_paragraphs(doc, placeholder, body_text):
|
| 143 |
"""Replace the single `placeholder` paragraph with one paragraph per blank-line-separated
|
| 144 |
block of `body_text`, cloning the placeholder's formatting (alignment + first-line indent).
|
|
|
|
| 199 |
# =============================================================================
|
| 200 |
def generate_form(form_id, template_path, output_path,
|
| 201 |
doc_number, po_number, doc_type, signer,
|
| 202 |
+
lookup_data=None, signer_position=None, body_text=None, letter_date=None):
|
| 203 |
"""
|
| 204 |
สร้างเอกสาร Word จาก template
|
| 205 |
Args:
|
|
|
|
| 219 |
|
| 220 |
doc = Document(template_path)
|
| 221 |
log = []
|
| 222 |
+
|
| 223 |
+
# header date line 'วันที่ ___ เดือน ___ พ.ศ.' -> defaults to today
|
| 224 |
+
if fill_letter_date(doc, letter_date):
|
| 225 |
+
log.append("วันที่หนังสือ: เติมวันที่แล้ว")
|
| 226 |
vendor_name = clean_vendor_name((lookup_data or {}).get("VendorName"))
|
| 227 |
work_title = (lookup_data or {}).get("title") or ""
|
| 228 |
|
streamlit_app.py
CHANGED
|
@@ -714,6 +714,18 @@ st.markdown("""
|
|
| 714 |
.st-key-po_input_widget [data-baseweb="base-input"] {
|
| 715 |
height: 3rem !important;
|
| 716 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 717 |
.stDownloadButton button {
|
| 718 |
background: linear-gradient(135deg, #F26A3A 0%, #E85D27 100%) !important;
|
| 719 |
color: #FFFFFF !important;
|
|
@@ -1470,9 +1482,10 @@ with right_col:
|
|
| 1470 |
|
| 1471 |
st.markdown("<div style='height: 0.75rem;'></div>", unsafe_allow_html=True)
|
| 1472 |
|
| 1473 |
-
generate_clicked = st.button(":material/auto_awesome: สร้างเอกสารด้วย AI",
|
| 1474 |
type="primary",
|
| 1475 |
use_container_width=True,
|
|
|
|
| 1476 |
disabled=not step1_done)
|
| 1477 |
|
| 1478 |
if generate_clicked:
|
|
|
|
| 714 |
.st-key-po_input_widget [data-baseweb="base-input"] {
|
| 715 |
height: 3rem !important;
|
| 716 |
}
|
| 717 |
+
/* Highlight the "AI" in the generate button: the :violet[AI] markdown span becomes a
|
| 718 |
+
glowing white chip/badge so it showcases the AI feature */
|
| 719 |
+
.st-key-generate_btn button span[style*="color"]:not([data-testid]) {
|
| 720 |
+
color: #fff !important;
|
| 721 |
+
font-weight: 800;
|
| 722 |
+
letter-spacing: .6px;
|
| 723 |
+
padding: 2px 10px;
|
| 724 |
+
border-radius: 8px;
|
| 725 |
+
background: rgba(255, 255, 255, .20);
|
| 726 |
+
box-shadow: 0 0 12px rgba(255, 255, 255, .5), inset 0 0 0 1px rgba(255, 255, 255, .45);
|
| 727 |
+
text-shadow: 0 0 8px rgba(255, 255, 255, .7);
|
| 728 |
+
}
|
| 729 |
.stDownloadButton button {
|
| 730 |
background: linear-gradient(135deg, #F26A3A 0%, #E85D27 100%) !important;
|
| 731 |
color: #FFFFFF !important;
|
|
|
|
| 1482 |
|
| 1483 |
st.markdown("<div style='height: 0.75rem;'></div>", unsafe_allow_html=True)
|
| 1484 |
|
| 1485 |
+
generate_clicked = st.button(":material/auto_awesome: สร้างเอกสารด้วย :violet[AI]",
|
| 1486 |
type="primary",
|
| 1487 |
use_container_width=True,
|
| 1488 |
+
key="generate_btn",
|
| 1489 |
disabled=not step1_done)
|
| 1490 |
|
| 1491 |
if generate_clicked:
|