File size: 4,625 Bytes
7f4a99d
4584aec
 
 
 
7f4a99d
4584aec
7f4a99d
4584aec
7f4a99d
 
4584aec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6660cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99146e1
 
 
 
5c120b3
 
 
 
 
 
 
 
 
 
 
 
e6660cd
 
 
 
 
4584aec
 
 
 
 
 
 
 
ea5abfd
 
4584aec
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
---
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://<space-host>/oauth2callback` |
| `AUTH_SERVER_METADATA_URL` | `https://login.microsoftonline.com/<tenant>/v2.0/.well-known/openid-configuration` |
| `DATA_URL` | OneDrive "anyone with the link" URL of the PO-data Excel (Power BI export) |

## 🔏 Signer auto-fill (ผู้มีอำนาจอนุมัติ / ประธานกรรมการตรวจรับ)

The two signer names ride the **same OneDrive `DATA_URL` file** the app already reads — the app
never calls the PTT gateway itself. It *can't*: the gateway is a private `10.x` host, and a public
webpage is blocked from it by Chrome's **Private Network Access** (so neither a browser nor the HF
server can fetch it). Instead, the **Power BI model** fetches the names during its scheduled
refresh, which runs **through an On-premises Data Gateway (personal mode)** on a PTT-connected PC
where the gateway *is* reachable. The model adds two columns — **`approverName`** and
**`chairmanName`** — and the app reads them per PO (`signer_lookup.SIGNER_COLUMN`); if a column is
blank it offers a manual ชื่อผู้ลงนาม box.

- Model-side query + full setup steps live in **`powerquery_signers.pq`** (create the `SMPToken`
  parameter = the no-reply bearer, bind the dataset to your personal gateway, schedule refresh).
- Nothing is needed on the Space for this: no `SMP_TOKEN` secret, no `streamlit-javascript`.

Register the same `https://<space-host>/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