--- title: Auto Form Generator emoji: 📄 colorFrom: blue colorTo: indigo sdk: docker app_port: 7860 pinned: false short_description: Auto-generate procurement letters from Excel data --- # Auto Form Generator ระบบสร้างหนังสือราชการอัตโนมัติจากแบบฟอร์มมาตรฐาน 5 ประเภท พร้อมดึงข้อมูล PO อัตโนมัติจากไฟล์ Excel [![Python](https://img.shields.io/badge/Python-3.8+-blue.svg)](https://www.python.org/) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE) --- ## ✨ Features - 📄 รองรับ 5 แบบฟอร์มมาตรฐาน - 🔍 Auto-fill จาก Excel — ใส่ PO แล้วระบบเติม Vendor, ชื่องาน ฯลฯ ให้อัตโนมัติ - ✏️ รักษารูปแบบเอกสารต้นฉบับ (Font, สี, layout) - 🌐 รันบนเว็บได้เลย ไม่ต้องติดตั้งอะไร - 🚀 ทำงานบน Hugging Face Spaces ฟรี ## 📦 Tech Stack - **Python 3.11** + **Streamlit** (web UI) - **python-docx** (สร้างเอกสาร Word) - **openpyxl** (อ่านไฟล์ Excel) - **Docker** (deployment on Hugging Face Spaces) ## 🚀 Run Locally ```bash pip install -r requirements.txt streamlit run streamlit_app.py ``` เปิดเบราว์เซอร์ที่ http://localhost:8501 ## 🔐 Authentication (Microsoft Entra ID) The app is gated behind Microsoft sign-in using Streamlit's native OIDC auth (`st.login`). It reuses an existing Azure App Registration. **Local:** copy `.streamlit/secrets.toml.template` to `.streamlit/secrets.toml` and fill in the values (this file is gitignored). Register `http://localhost:8501/oauth2callback` as a **Web** redirect URI in Azure. **Hugging Face Spaces:** `secrets.toml` is built at startup by `entrypoint.sh` from these Space secrets — set them in *Settings → Variables and secrets*: | Secret | Value | | --- | --- | | `AUTH_CLIENT_ID` | Azure app (client) id | | `AUTH_CLIENT_SECRET` | Azure client secret | | `AUTH_COOKIE_SECRET` | any long random hex string | | `AUTH_REDIRECT_URI` | `https:///oauth2callback` | | `AUTH_SERVER_METADATA_URL` | `https://login.microsoftonline.com//v2.0/.well-known/openid-configuration` | | `DATA_URL` | OneDrive "anyone with the link" URL of the PO-data Excel (Power BI export) | | `SMP_TOKEN` | no-reply service-account bearer for the signer lookup (see below) | ## 🔏 Signer auto-fill (ผู้มีอำนาจอนุมัติ / ประธานกรรมการตรวจรับ) When the user picks a ผู้ลงนาม role, the app resolves the real name live from the PTT SMP system — **in the user's browser**, not on the server. The HF server can't reach the PTT-internal gateway, but the user's browser (on PTT wifi) can, and the gateway sends `Access-Control-Allow-Origin: *`, so the cross-origin fetch is allowed. `signer_lookup.py` builds the client-side fetch (PO → SAP PR → approver + receiving-committee chairman); `streamlit-javascript` runs it and returns the names. - Set the **`SMP_TOKEN`** Space secret to the no-reply bearer (mint/refresh it with `smp_api\harvest_token.py`; the value is the `bearer` field of `te_session_tsopmm.json`, with or without the `Bearer ` prefix). It is **read-only** and only works on the PTT network. ⚠️ This token is injected into client-side JS, so it is visible to signed-in users — it's a service account (not a personal login), gated behind Entra sign-in. **Rotate it periodically** (it expires ~166 days after minting). - Requires the user to be **on PTT wifi** at generation time. Off-network (or if the token expired), the app falls back to a manual ชื่อผู้ลงนาม text box automatically. - Local testing: set `SMP_TOKEN` as an env var and `pip install streamlit-javascript`. Register the same `https:///oauth2callback` as a **Web** redirect URI in Azure. Sign-in stays broken until both the Space secrets and the Azure redirect URI are in place. ## 📋 Project Structure ``` . ├── Dockerfile # HF Spaces container config ├── README.md # This file (with HF YAML frontmatter) ├── requirements.txt # Python dependencies ├── streamlit_app.py # Web UI (main entry point) ├── form_logic.py # Document generation logic ├── excel_lookup.py # Excel data lookup helper (legacy, unused by web app) ├── sample_po_data.xlsx # Sample data for demo ├── .streamlit/ │ └── config.toml # Theme & toolbar config └── templates/ └── form1-5.docx # Word templates ``` ## 📄 License MIT