Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import sqlite3
|
| 3 |
import json
|
|
|
|
| 4 |
from huggingface_hub import hf_hub_download, upload_file
|
| 5 |
import os
|
| 6 |
from pathlib import Path
|
|
@@ -14,6 +15,7 @@ import zipfile
|
|
| 14 |
import io
|
| 15 |
from dotenv import load_dotenv
|
| 16 |
import time
|
|
|
|
| 17 |
|
| 18 |
# อ่าน .env
|
| 19 |
load_dotenv()
|
|
@@ -21,18 +23,19 @@ REPO_ID = os.getenv("REPO_ID", "protae5544/WorkerManagement")
|
|
| 21 |
ORIGINAL_REPO_ID = os.getenv("ORIGINAL_REPO_ID", None)
|
| 22 |
|
| 23 |
# ตั้งค่าฟอนต์
|
| 24 |
-
|
| 25 |
if platform.system() == "Linux":
|
| 26 |
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
|
| 27 |
if os.path.exists(font_path):
|
| 28 |
pdfmetrics.registerFont(TTFont('THSarabunBold', font_path))
|
| 29 |
else:
|
| 30 |
# ใช้ฟอนต์ default
|
| 31 |
-
|
| 32 |
else:
|
|
|
|
| 33 |
pdfmetrics.registerFont(TTFont('THSarabunBold', 'THSarabunNew-Bold.ttf'))
|
| 34 |
except Exception as e:
|
| 35 |
-
print(f"ใช้ฟอนต์ default
|
| 36 |
|
| 37 |
# ฟังก์ชันแปลงวันที่เป็นภาษาไทย
|
| 38 |
def thai_date_time(timestamp):
|
|
@@ -130,95 +133,104 @@ def get_next_request_number():
|
|
| 130 |
|
| 131 |
# สร้าง PDF ด้วยภาพพื้นหลัง
|
| 132 |
def create_receipt_pdf(request_number, english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number):
|
|
|
|
|
|
|
| 133 |
filename = f"pdfs/receipt_{request_number}.pdf"
|
| 134 |
local_path = f"temp_{request_number}.pdf"
|
| 135 |
-
c = canvas.Canvas(local_path, pagesize=A4)
|
| 136 |
|
| 137 |
-
# เพิ่มภาพพื้นหลัง (bg.png จาก index1.html)
|
| 138 |
try:
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
path_or_fileobj=local_path,
|
| 194 |
-
path_in_repo=filename,
|
| 195 |
-
repo_id=REPO_ID,
|
| 196 |
-
repo_type="space"
|
| 197 |
-
)
|
| 198 |
-
except Exception as e:
|
| 199 |
-
print(f"ข้อผิดพลาดในการอัพโหลด PDF ไปยัง Space: {e}")
|
| 200 |
-
|
| 201 |
-
# อัพโหลดไปยัง Space เดิม (ถ้ามี)
|
| 202 |
-
if ORIGINAL_REPO_ID:
|
| 203 |
try:
|
| 204 |
upload_file(
|
| 205 |
path_or_fileobj=local_path,
|
| 206 |
path_in_repo=filename,
|
| 207 |
-
repo_id=
|
| 208 |
repo_type="space"
|
| 209 |
)
|
| 210 |
except Exception as e:
|
| 211 |
-
print(f"
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
# ตรวจสอบข้อมูล
|
| 224 |
def validate_input(english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number):
|
|
@@ -247,20 +259,37 @@ def add_worker(english_name, foreign_reference_number, id_number, nationality, r
|
|
| 247 |
|
| 248 |
# จัดการไฟล์แนบ
|
| 249 |
attachment_urls = []
|
| 250 |
-
|
| 251 |
-
|
|
|
|
| 252 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
upload_file(
|
| 254 |
-
path_or_fileobj=
|
| 255 |
path_in_repo=filename,
|
| 256 |
repo_id=REPO_ID,
|
| 257 |
repo_type="space"
|
| 258 |
)
|
| 259 |
-
url = f"https://huggingface.co/spaces/{REPO_ID}/
|
| 260 |
c.execute("INSERT INTO attachments (request_number, file_url) VALUES (?, ?)", (request_number, url))
|
| 261 |
attachment_urls.append(url)
|
|
|
|
|
|
|
|
|
|
| 262 |
except Exception as e:
|
| 263 |
-
print(f"
|
| 264 |
|
| 265 |
conn.commit()
|
| 266 |
conn.close()
|
|
@@ -271,7 +300,7 @@ def add_worker(english_name, foreign_reference_number, id_number, nationality, r
|
|
| 271 |
# สำรองฐานข้อมูล
|
| 272 |
backup_db()
|
| 273 |
|
| 274 |
-
return f"เพิ่มข้อมูลพนักงานสำเร็จ! เลขคำขอ: {request_number}\nPDF: {
|
| 275 |
|
| 276 |
# ดาวน์โหลด PDF ทั้งหมด
|
| 277 |
def download_all_pdfs():
|
|
@@ -281,19 +310,30 @@ def download_all_pdfs():
|
|
| 281 |
pdfs = c.fetchall()
|
| 282 |
conn.close()
|
| 283 |
|
| 284 |
-
|
| 285 |
-
|
|
|
|
|
|
|
| 286 |
for request_number, pdf_url in pdfs:
|
| 287 |
try:
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
|
|
|
|
| 297 |
try:
|
| 298 |
upload_file(
|
| 299 |
path_or_fileobj=zip_path,
|
|
@@ -302,35 +342,47 @@ def download_all_pdfs():
|
|
| 302 |
repo_type="space"
|
| 303 |
)
|
| 304 |
except Exception as e:
|
| 305 |
-
print(f"
|
| 306 |
|
| 307 |
-
return f"
|
| 308 |
|
| 309 |
# เริ่มต้นฐานข้อมูลและนำเข้า JSON
|
| 310 |
init_db()
|
| 311 |
import_json_to_db()
|
| 312 |
|
| 313 |
# อินเทอร์เฟซ Gradio
|
| 314 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 315 |
gr.Markdown("## เพิ่มข้อมูลพนักงานใหม่")
|
| 316 |
with gr.Row():
|
| 317 |
-
english_name = gr.Textbox(label="ชื่อภาษาอังกฤษ", placeholder="เช่น MR. AUNG KYAW"
|
| 318 |
-
foreign_reference_number = gr.Textbox(label="หมายเลขอ้างอิงคนต่างด้าว", placeholder="เช่น 2492102076212"
|
| 319 |
with gr.Row():
|
| 320 |
-
id_number = gr.Textbox(label="หมายเลขประจำตัว (13 หลัก)", placeholder="เช่น 6685490000472"
|
| 321 |
-
nationality = gr.Textbox(label="สัญชาติ", value="เมียนมา"
|
| 322 |
with gr.Row():
|
| 323 |
-
receipt_number = gr.Textbox(label="เลขที่บนขวาใบเสร็จ", placeholder="เช่น 2100680001130"
|
| 324 |
-
payment_number = gr.Textbox(label="หมายเลขชำระเงิน", placeholder="เช่น IV680210/002350"
|
| 325 |
-
attachments = gr.File(label="แนบไฟล์ (สูงสุด 5MB)", file_count="multiple", file_types=["
|
| 326 |
-
submit = gr.Button("บันทึกข้อมูล")
|
| 327 |
output = gr.Textbox(label="ผลลัพธ์")
|
| 328 |
|
| 329 |
-
gr.Markdown("##
|
| 330 |
download_all = gr.Button("ดาวน์โหลด PDF ทั้งหมด")
|
| 331 |
download_output = gr.Textbox(label="ลิงก์ดาวน์โหลด")
|
| 332 |
|
| 333 |
-
submit.click(
|
| 334 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
|
| 336 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import sqlite3
|
| 3 |
import json
|
| 4 |
+
import platform
|
| 5 |
from huggingface_hub import hf_hub_download, upload_file
|
| 6 |
import os
|
| 7 |
from pathlib import Path
|
|
|
|
| 15 |
import io
|
| 16 |
from dotenv import load_dotenv
|
| 17 |
import time
|
| 18 |
+
import shutil
|
| 19 |
|
| 20 |
# อ่าน .env
|
| 21 |
load_dotenv()
|
|
|
|
| 23 |
ORIGINAL_REPO_ID = os.getenv("ORIGINAL_REPO_ID", None)
|
| 24 |
|
| 25 |
# ตั้งค่าฟอนต์
|
| 26 |
+
try:
|
| 27 |
if platform.system() == "Linux":
|
| 28 |
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
|
| 29 |
if os.path.exists(font_path):
|
| 30 |
pdfmetrics.registerFont(TTFont('THSarabunBold', font_path))
|
| 31 |
else:
|
| 32 |
# ใช้ฟอนต์ default
|
| 33 |
+
print("ใช้ฟอนต์มาตรฐานของ ReportLab เนื่องจากไม่พบ DejaVuSans-Bold")
|
| 34 |
else:
|
| 35 |
+
# ใช้ฟอนต์สำหรับ Windows/Mac
|
| 36 |
pdfmetrics.registerFont(TTFont('THSarabunBold', 'THSarabunNew-Bold.ttf'))
|
| 37 |
except Exception as e:
|
| 38 |
+
print(f"ใช้ฟอนต์ default เนื่องจากเกิดข้อผิดพลาด: {e}")
|
| 39 |
|
| 40 |
# ฟังก์ชันแปลงวันที่เป็นภาษาไทย
|
| 41 |
def thai_date_time(timestamp):
|
|
|
|
| 133 |
|
| 134 |
# สร้าง PDF ด้วยภาพพื้นหลัง
|
| 135 |
def create_receipt_pdf(request_number, english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number):
|
| 136 |
+
# สร้างโฟลเดอร์ pdfs ถ้ายังไม่มี
|
| 137 |
+
os.makedirs("pdfs", exist_ok=True)
|
| 138 |
filename = f"pdfs/receipt_{request_number}.pdf"
|
| 139 |
local_path = f"temp_{request_number}.pdf"
|
|
|
|
| 140 |
|
|
|
|
| 141 |
try:
|
| 142 |
+
c = canvas.Canvas(local_path, pagesize=A4)
|
| 143 |
+
|
| 144 |
+
# เพิ่มภาพพื้นหลัง
|
| 145 |
+
try:
|
| 146 |
+
if ORIGINAL_REPO_ID:
|
| 147 |
+
bg_path = hf_hub_download(repo_id=ORIGINAL_REPO_ID, filename="assets/bg.png", repo_type="space")
|
| 148 |
+
else:
|
| 149 |
+
bg_path = hf_hub_download(repo_id=REPO_ID, filename="assets/bg.png", repo_type="space")
|
| 150 |
+
c.drawImage(bg_path, 2.05*cm, A4[1] - (5.12*cm + 23.45*cm), width=17.62*cm, height=23.45*cm, preserveAspectRatio=True)
|
| 151 |
+
except Exception as e:
|
| 152 |
+
print(f"ไม่พบภาพพื้นหลัง: {e}")
|
| 153 |
+
|
| 154 |
+
# ข้อมูลใบเสร็จ
|
| 155 |
+
c.setFont("THSarabunBold", 18) # 52.284/2.83465 ≈ 18.45
|
| 156 |
+
c.drawString(2*cm, 28*cm, "กระทรวงแรงงาน")
|
| 157 |
+
c.setFont("THSarabun", 12) # 35.212/2.83465 ≈ 12.42
|
| 158 |
+
c.drawString(2*cm, 27.5*cm, "ใบเสร็จรับเงิน (ต้นฉบับ)")
|
| 159 |
+
c.drawString(2*cm, 26*cm, f"เลขที่: {receipt_number}")
|
| 160 |
+
c.drawString(2*cm, 25.5*cm, "ที่ทำการ: สำนักบริหารแรงงานต่างด้าว")
|
| 161 |
+
c.drawString(2*cm, 25*cm, "วันที่: 01 เมษายน 2568")
|
| 162 |
+
c.drawString(2*cm, 24.5*cm, f"เลขที่ใบชำระเงิน: {payment_number}")
|
| 163 |
+
c.drawString(2*cm, 24*cm, f"เลขรับคำขอที่: {request_number}")
|
| 164 |
+
c.drawString(2*cm, 23.5*cm, f"ชื่อผู้ชำระเงิน: {english_name}")
|
| 165 |
+
c.drawString(2*cm, 23*cm, f"สัญชาติ: {nationality}")
|
| 166 |
+
c.drawString(2*cm, 22.5*cm, f"เลขอ้างอิงคนต่างด้าว: {foreign_reference_number}")
|
| 167 |
+
c.drawString(2*cm, 22*cm, f"หมายเลขประจำตัวคนต่างด้าว: {id_number}")
|
| 168 |
+
c.drawString(2*cm, 21.5*cm, "ชื่อนายจ้าง / สถานประกอบการ: บริษัท บาน กง เอ็นจิเนียริ่ง จำกัด")
|
| 169 |
+
c.drawString(2*cm, 21*cm, "เลขประจำตัวนายจ้าง: 0415567000061")
|
| 170 |
+
c.setFont("THSarabunBold", 12)
|
| 171 |
+
c.drawString(2*cm, 20*cm, "รายการ")
|
| 172 |
+
c.setFont("THSarabun", 12)
|
| 173 |
+
c.drawString(2*cm, 19.5*cm, "1. ค่าธรรมเนียมในการยื่นคำขอ ฉบับละ 100 บาท: 100.00")
|
| 174 |
+
c.drawString(2*cm, 19*cm, "2. ค่าธรรมเนียม: 900.00")
|
| 175 |
+
c.drawString(2*cm, 18.5*cm, "รวมเป็นเงินทั้งสิ้น (บาท): (หนึ่งพันบาทถ้วน) 1,000.00")
|
| 176 |
+
c.drawString(2*cm, 18*cm, "ได้รับเงินไว้เป็นการถูกต้องแล้ว")
|
| 177 |
+
c.drawString(2*cm, 17.5*cm, "(ลงชื่อ) นางสาวอารีวรรณ โพธิ์นิ่มแดง (ผู้รับเงิน)")
|
| 178 |
+
c.drawString(2*cm, 17*cm, "ตำแหน่ง: นักวิชาการแรงงานชำนาญการ")
|
| 179 |
+
|
| 180 |
+
# QR Code
|
| 181 |
+
pdf_url = f"https://huggingface.co/spaces/{REPO_ID}/resolve/main/{filename}"
|
| 182 |
+
qr = qrcode.make(pdf_url)
|
| 183 |
+
qr_img_path = f"temp_qr_{request_number}.png"
|
| 184 |
+
qr.save(qr_img_path)
|
| 185 |
+
c.drawImage(qr_img_path, 15*cm, 15*cm, width=3*cm, height=3*cm)
|
| 186 |
+
|
| 187 |
+
# เวลาพิมพ์
|
| 188 |
+
c.setFont("THSarabun", 7)
|
| 189 |
+
print_time = thai_date_time(time.time())
|
| 190 |
+
c.drawString(2*cm, 2*cm, f"พิมพ์เมื่อ: {print_time}")
|
| 191 |
+
|
| 192 |
+
c.showPage()
|
| 193 |
+
c.save()
|
| 194 |
+
|
| 195 |
+
# อัพโหลด PDF
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
try:
|
| 197 |
upload_file(
|
| 198 |
path_or_fileobj=local_path,
|
| 199 |
path_in_repo=filename,
|
| 200 |
+
repo_id=REPO_ID,
|
| 201 |
repo_type="space"
|
| 202 |
)
|
| 203 |
except Exception as e:
|
| 204 |
+
print(f"อัพโหลด PDF ไปยัง Space หลักไม่สำเร็จ: {e}")
|
| 205 |
+
|
| 206 |
+
# อัพโหลดไปยัง Space เดิม
|
| 207 |
+
if ORIGINAL_REPO_ID:
|
| 208 |
+
try:
|
| 209 |
+
upload_file(
|
| 210 |
+
path_or_fileobj=local_path,
|
| 211 |
+
path_in_repo=filename,
|
| 212 |
+
repo_id=ORIGINAL_REPO_ID,
|
| 213 |
+
repo_type="space"
|
| 214 |
+
)
|
| 215 |
+
except Exception as e:
|
| 216 |
+
print(f"อัพโหลด PDF ไปยัง Space เดิมไม่สำเร็จ: {e}")
|
| 217 |
+
|
| 218 |
+
# บันทึกข้อมูล PDF
|
| 219 |
+
conn = sqlite3.connect("workers.db")
|
| 220 |
+
c = conn.cursor()
|
| 221 |
+
c.execute("INSERT OR REPLACE INTO pdfs (request_number, pdf_url) VALUES (?, ?)",
|
| 222 |
+
(request_number, pdf_url))
|
| 223 |
+
conn.commit()
|
| 224 |
+
conn.close()
|
| 225 |
+
|
| 226 |
+
return filename
|
| 227 |
+
|
| 228 |
+
finally:
|
| 229 |
+
# ลบไฟล์ชั่วคราว
|
| 230 |
+
if os.path.exists(local_path):
|
| 231 |
+
os.remove(local_path)
|
| 232 |
+
if os.path.exists(qr_img_path):
|
| 233 |
+
os.remove(qr_img_path)
|
| 234 |
|
| 235 |
# ตรวจสอบข้อมูล
|
| 236 |
def validate_input(english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number):
|
|
|
|
| 259 |
|
| 260 |
# จัดการไฟล์แนบ
|
| 261 |
attachment_urls = []
|
| 262 |
+
os.makedirs("attachments", exist_ok=True)
|
| 263 |
+
|
| 264 |
+
for i, file in enumerate(attachments or []):
|
| 265 |
try:
|
| 266 |
+
filename = f"attachments/{request_number}_{i}_{os.path.basename(file.name)}"
|
| 267 |
+
repo_path = f"attachments/{os.path.basename(file.name)}"
|
| 268 |
+
|
| 269 |
+
# บันทึกไฟล์ชั่วคราว
|
| 270 |
+
temp_path = f"temp_{request_number}_{i}_{os.path.basename(file.name)}"
|
| 271 |
+
with open(temp_path, "wb") as f:
|
| 272 |
+
if hasattr(file, "read"):
|
| 273 |
+
f.write(file.read())
|
| 274 |
+
else:
|
| 275 |
+
with open(file.name, "rb") as src:
|
| 276 |
+
f.write(src.read())
|
| 277 |
+
|
| 278 |
+
# อัพโหลดไฟล์
|
| 279 |
upload_file(
|
| 280 |
+
path_or_fileobj=temp_path,
|
| 281 |
path_in_repo=filename,
|
| 282 |
repo_id=REPO_ID,
|
| 283 |
repo_type="space"
|
| 284 |
)
|
| 285 |
+
url = f"https://huggingface.co/spaces/{REPO_ID}/resolve/main/{filename}"
|
| 286 |
c.execute("INSERT INTO attachments (request_number, file_url) VALUES (?, ?)", (request_number, url))
|
| 287 |
attachment_urls.append(url)
|
| 288 |
+
|
| 289 |
+
# ลบไฟล์ชั่วคราว
|
| 290 |
+
os.remove(temp_path)
|
| 291 |
except Exception as e:
|
| 292 |
+
print(f"อัพโหลดไฟล์แนบไม่สำเร็จ: {e}")
|
| 293 |
|
| 294 |
conn.commit()
|
| 295 |
conn.close()
|
|
|
|
| 300 |
# สำรองฐานข้อมูล
|
| 301 |
backup_db()
|
| 302 |
|
| 303 |
+
return f"เพิ่มข้อมูลพนักงานสำเร็จ! เลขคำขอ: {request_number}\nPDF: https://huggingface.co/spaces/{REPO_ID}/resolve/main/{pdf_path}"
|
| 304 |
|
| 305 |
# ดาวน์โหลด PDF ทั้งหมด
|
| 306 |
def download_all_pdfs():
|
|
|
|
| 310 |
pdfs = c.fetchall()
|
| 311 |
conn.close()
|
| 312 |
|
| 313 |
+
os.makedirs("downloads", exist_ok=True)
|
| 314 |
+
zip_path = "downloads/all_receipts.zip"
|
| 315 |
+
|
| 316 |
+
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zip_file:
|
| 317 |
for request_number, pdf_url in pdfs:
|
| 318 |
try:
|
| 319 |
+
# ดาวน์โหลด PDF จาก URL
|
| 320 |
+
filename = f"receipt_{request_number}.pdf"
|
| 321 |
+
local_path = f"downloads/{filename}"
|
| 322 |
+
|
| 323 |
+
# ดาวน์โหลดไฟล์
|
| 324 |
+
downloaded_path = hf_hub_download(
|
| 325 |
+
repo_id=REPO_ID,
|
| 326 |
+
filename=f"pdfs/receipt_{request_number}.pdf",
|
| 327 |
+
repo_type="space",
|
| 328 |
+
local_path=local_path
|
| 329 |
+
)
|
| 330 |
+
|
| 331 |
+
# เพิ่มไฟล์ลง ZIP
|
| 332 |
+
zip_file.write(downloaded_path, filename)
|
| 333 |
+
except Exception as e:
|
| 334 |
+
print(f"ไม่สามารถเพิ่ม PDF {request_number}: {e}")
|
| 335 |
|
| 336 |
+
# อัพโหลด ZIP
|
| 337 |
try:
|
| 338 |
upload_file(
|
| 339 |
path_or_fileobj=zip_path,
|
|
|
|
| 342 |
repo_type="space"
|
| 343 |
)
|
| 344 |
except Exception as e:
|
| 345 |
+
print(f"อัพโหลด ZIP ไม่สำเร็จ: {e}")
|
| 346 |
|
| 347 |
+
return f"https://huggingface.co/spaces/{REPO_ID}/resolve/main/all_receipts.zip"
|
| 348 |
|
| 349 |
# เริ่มต้นฐานข้อมูลและนำเข้า JSON
|
| 350 |
init_db()
|
| 351 |
import_json_to_db()
|
| 352 |
|
| 353 |
# อินเทอร์เฟซ Gradio
|
| 354 |
+
with gr.Blocks(title="ระบบจัดการข้อมูลพนักงาน", theme=gr.themes.Soft()) as demo:
|
| 355 |
gr.Markdown("## เพิ่มข้อมูลพนักงานใหม่")
|
| 356 |
with gr.Row():
|
| 357 |
+
english_name = gr.Textbox(label="ชื่อภาษาอังกฤษ", placeholder="เช่น MR. AUNG KYAW")
|
| 358 |
+
foreign_reference_number = gr.Textbox(label="หมายเลขอ้างอิงคนต่างด้าว", placeholder="เช่น 2492102076212")
|
| 359 |
with gr.Row():
|
| 360 |
+
id_number = gr.Textbox(label="หมายเลขประจำตัว (13 หลัก)", placeholder="เช่น 6685490000472")
|
| 361 |
+
nationality = gr.Textbox(label="สัญชาติ", value="เมียนมา")
|
| 362 |
with gr.Row():
|
| 363 |
+
receipt_number = gr.Textbox(label="เลขที่บนขวาใบเสร็จ", placeholder="เช่น 2100680001130")
|
| 364 |
+
payment_number = gr.Textbox(label="หมายเลขชำระเงิน", placeholder="เช่น IV680210/002350")
|
| 365 |
+
attachments = gr.File(label="แนบไฟล์ (สูงสุด 5MB)", file_count="multiple", file_types=["image", ".pdf"], max_size=5*1024*1024)
|
| 366 |
+
submit = gr.Button("บันทึกข้อมูล", variant="primary")
|
| 367 |
output = gr.Textbox(label="ผลลัพธ์")
|
| 368 |
|
| 369 |
+
gr.Markdown("## ดาวน์โหลดเอกสาร")
|
| 370 |
download_all = gr.Button("ดาวน์โหลด PDF ทั้งหมด")
|
| 371 |
download_output = gr.Textbox(label="ลิงก์ดาวน์โหลด")
|
| 372 |
|
| 373 |
+
submit.click(
|
| 374 |
+
fn=add_worker,
|
| 375 |
+
inputs=[english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number, attachments],
|
| 376 |
+
outputs=output
|
| 377 |
+
)
|
| 378 |
+
|
| 379 |
+
download_all.click(
|
| 380 |
+
fn=download_all_pdfs,
|
| 381 |
+
outputs=download_output
|
| 382 |
+
)
|
| 383 |
|
| 384 |
+
# เริ่มต้นแอปพลิเคชัน
|
| 385 |
+
if __name__ == "__main__":
|
| 386 |
+
demo.launch(
|
| 387 |
+
server_name="0.0.0.0",
|
| 388 |
+
server_port=int(os.getenv("PORT", 7860))
|