Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,19 +15,19 @@ import io
|
|
| 15 |
from dotenv import load_dotenv
|
| 16 |
import time
|
| 17 |
|
| 18 |
-
#
|
| 19 |
load_dotenv()
|
| 20 |
REPO_ID = os.getenv("REPO_ID", "your-username/worker-management")
|
| 21 |
ORIGINAL_REPO_ID = os.getenv("ORIGINAL_REPO_ID", None)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
try:
|
| 25 |
pdfmetrics.registerFont(TTFont('THSarabun', 'THSarabunNew.ttf'))
|
| 26 |
pdfmetrics.registerFont(TTFont('THSarabunBold', 'THSarabunNew-Bold.ttf'))
|
| 27 |
except Exception as e:
|
| 28 |
raise Exception(f"Error loading fonts: {e}")
|
| 29 |
|
| 30 |
-
#
|
| 31 |
def thai_date_time(timestamp):
|
| 32 |
months = {
|
| 33 |
1: "มกราคม", 2: "กุมภาพันธ์", 3: "มีนาคม", 4: "เมษายน",
|
|
@@ -43,7 +43,7 @@ def thai_date_time(timestamp):
|
|
| 43 |
period = "น"
|
| 44 |
return f"{day:02d} {month} {year} {hour:02d}:{minute:02d} {period}"
|
| 45 |
|
| 46 |
-
#
|
| 47 |
def init_db():
|
| 48 |
conn = sqlite3.connect("workers.db")
|
| 49 |
c = conn.cursor()
|
|
@@ -69,7 +69,7 @@ def init_db():
|
|
| 69 |
conn.commit()
|
| 70 |
conn.close()
|
| 71 |
|
| 72 |
-
#
|
| 73 |
def import_json_to_db():
|
| 74 |
try:
|
| 75 |
json_path = hf_hub_download(repo_id=REPO_ID, filename="worker-database.json", repo_type="space")
|
|
@@ -95,14 +95,14 @@ def import_json_to_db():
|
|
| 95 |
except Exception as e:
|
| 96 |
print(f"Error importing JSON: {e}")
|
| 97 |
|
| 98 |
-
#
|
| 99 |
def backup_db():
|
| 100 |
try:
|
| 101 |
hf_hub_upload(repo_id=REPO_ID, path_in_repo="workers.db", path_or_fileobj="workers.db", repo_type="space")
|
| 102 |
except Exception as e:
|
| 103 |
print(f"Error backing up database: {e}")
|
| 104 |
|
| 105 |
-
#
|
| 106 |
def get_next_request_number():
|
| 107 |
conn = sqlite3.connect("workers.db")
|
| 108 |
c = conn.cursor()
|
|
@@ -116,20 +116,31 @@ def get_next_request_number():
|
|
| 116 |
max_num = max([int(num[0].split('-')[-1]) for num in request_numbers])
|
| 117 |
return f"WP-68-366-{max_num + 1:03d}"
|
| 118 |
|
| 119 |
-
#
|
| 120 |
def create_receipt_pdf(request_number, english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number):
|
| 121 |
filename = f"pdfs/receipt_{request_number}.pdf"
|
| 122 |
local_path = f"temp_{request_number}.pdf"
|
| 123 |
c = canvas.Canvas(local_path, pagesize=A4)
|
| 124 |
|
| 125 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
c.setFont("THSarabunBold", 52.284 / 2.83465) # fs0
|
| 127 |
c.drawString(2*cm, 28*cm, "กระทรวงแรงงาน")
|
| 128 |
c.setFont("THSarabun", 35.212 / 2.83465) # fs3
|
| 129 |
c.drawString(2*cm, 27.5*cm, "ใบเสร็จรับเงิน (ต้นฉบับ)")
|
| 130 |
c.drawString(2*cm, 26*cm, f"เลขที่: {receipt_number}")
|
| 131 |
c.drawString(2*cm, 25.5*cm, "ที่ทำการ: สำนักบริหารแรงงานต่างด้าว")
|
| 132 |
-
c.drawString(2*cm, 25*cm, "วันที่: 01 เมษายน 2568")
|
| 133 |
c.drawString(2*cm, 24.5*cm, f"เลขที่ใบชำระเงิน: {payment_number}")
|
| 134 |
c.drawString(2*cm, 24*cm, f"เลขรับคำขอที่: {request_number}")
|
| 135 |
c.drawString(2*cm, 23.5*cm, f"ชื่อผู้ชำระเงิน: {english_name}")
|
|
@@ -148,34 +159,34 @@ def create_receipt_pdf(request_number, english_name, foreign_reference_number, i
|
|
| 148 |
c.drawString(2*cm, 17.5*cm, "(ลงชื่อ) นางสาวอารีวรรณ โพธิ์นิ่มแดง (ผู้รับเงิน)")
|
| 149 |
c.drawString(2*cm, 17*cm, "ตำแหน่ง: นักวิชาการแรงงานชำนาญการ")
|
| 150 |
|
| 151 |
-
# QR Code
|
| 152 |
-
pdf_url = f"
|
| 153 |
qr = qrcode.make(pdf_url)
|
| 154 |
qr.save("temp_qr.png")
|
| 155 |
c.drawImage("temp_qr.png", 15*cm, 15*cm, width=3*cm, height=3*cm)
|
| 156 |
|
| 157 |
-
#
|
| 158 |
-
c.setFont("THSarabun", 20 / 2.83465)
|
| 159 |
print_time = thai_date_time(time.time())
|
| 160 |
c.drawString(2*cm, 2*cm, f"พิมพ์เมื่อ: {print_time}")
|
| 161 |
|
| 162 |
c.showPage()
|
| 163 |
c.save()
|
| 164 |
|
| 165 |
-
#
|
| 166 |
try:
|
| 167 |
hf_hub_upload(repo_id=REPO_ID, path_in_repo=filename, path_or_fileobj=local_path, repo_type="space")
|
| 168 |
except Exception as e:
|
| 169 |
print(f"Error uploading PDF to Space: {e}")
|
| 170 |
|
| 171 |
-
#
|
| 172 |
if ORIGINAL_REPO_ID:
|
| 173 |
try:
|
| 174 |
hf_hub_upload(repo_id=ORIGINAL_REPO_ID, path_in_repo=filename, path_or_fileobj=local_path, repo_type="space")
|
| 175 |
except Exception as e:
|
| 176 |
print(f"Error uploading to original Space: {e}")
|
| 177 |
|
| 178 |
-
#
|
| 179 |
conn = sqlite3.connect("workers.db")
|
| 180 |
c = conn.cursor()
|
| 181 |
c.execute("INSERT OR REPLACE INTO pdfs (request_number, pdf_url) VALUES (?, ?)",
|
|
@@ -183,9 +194,9 @@ def create_receipt_pdf(request_number, english_name, foreign_reference_number, i
|
|
| 183 |
conn.commit()
|
| 184 |
conn.close()
|
| 185 |
|
| 186 |
-
return filename
|
| 187 |
|
| 188 |
-
#
|
| 189 |
def validate_input(english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number):
|
| 190 |
if not id_number or not id_number.isdigit() or len(id_number) != 13:
|
| 191 |
return "หมายเลขประจำตัวต้องเป็นตัวเลข 13 หลัก"
|
|
@@ -193,7 +204,7 @@ def validate_input(english_name, foreign_reference_number, id_number, nationalit
|
|
| 193 |
return "กรุณากรอกเลขที่ใบเสร็จและหมายเลขชำระเงิน"
|
| 194 |
return None
|
| 195 |
|
| 196 |
-
#
|
| 197 |
def add_worker(english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number, attachments):
|
| 198 |
error = validate_input(english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number)
|
| 199 |
if error:
|
|
@@ -201,7 +212,7 @@ def add_worker(english_name, foreign_reference_number, id_number, nationality, r
|
|
| 201 |
|
| 202 |
request_number = get_next_request_number()
|
| 203 |
|
| 204 |
-
#
|
| 205 |
conn = sqlite3.connect("workers.db")
|
| 206 |
c = conn.cursor()
|
| 207 |
c.execute('''INSERT INTO workers (
|
|
@@ -210,13 +221,13 @@ def add_worker(english_name, foreign_reference_number, id_number, nationality, r
|
|
| 210 |
request_number, english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number
|
| 211 |
))
|
| 212 |
|
| 213 |
-
#
|
| 214 |
attachment_urls = []
|
| 215 |
for i, file_data in enumerate(attachments or []):
|
| 216 |
filename = f"attachments/file_{request_number}_{i}_{os.path.basename(file_data.name)}"
|
| 217 |
try:
|
| 218 |
hf_hub_upload(repo_id=REPO_ID, path_in_repo=filename, path_or_fileobj=file_data, repo_type="space")
|
| 219 |
-
url = f"
|
| 220 |
c.execute("INSERT INTO attachments (request_number, file_url) VALUES (?, ?)", (request_number, url))
|
| 221 |
attachment_urls.append(url)
|
| 222 |
except Exception as e:
|
|
@@ -225,15 +236,15 @@ def add_worker(english_name, foreign_reference_number, id_number, nationality, r
|
|
| 225 |
conn.commit()
|
| 226 |
conn.close()
|
| 227 |
|
| 228 |
-
#
|
| 229 |
pdf_path = create_receipt_pdf(request_number, english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number)
|
| 230 |
|
| 231 |
-
#
|
| 232 |
backup_db()
|
| 233 |
|
| 234 |
return f"เพิ่มข้อมูลพนักงานสำเร็จ! เลขคำขอ: {request_number}\nPDF: {pdf_path}\nไฟล์แนบ: {', '.join(attachment_urls) if attachment_urls else 'ไม่มีไฟล์แนบ'}"
|
| 235 |
|
| 236 |
-
#
|
| 237 |
def download_all_pdfs():
|
| 238 |
conn = sqlite3.connect("workers.db")
|
| 239 |
c = conn.cursor()
|
|
@@ -261,11 +272,11 @@ def download_all_pdfs():
|
|
| 261 |
|
| 262 |
return f"pdfs/all_receipts.zip"
|
| 263 |
|
| 264 |
-
#
|
| 265 |
init_db()
|
| 266 |
import_json_to_db()
|
| 267 |
|
| 268 |
-
#
|
| 269 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 270 |
gr.Markdown("## เพิ่มข้อมูลพนักงานใหม่")
|
| 271 |
with gr.Row():
|
|
|
|
| 15 |
from dotenv import load_dotenv
|
| 16 |
import time
|
| 17 |
|
| 18 |
+
# Load .env
|
| 19 |
load_dotenv()
|
| 20 |
REPO_ID = os.getenv("REPO_ID", "your-username/worker-management")
|
| 21 |
ORIGINAL_REPO_ID = os.getenv("ORIGINAL_REPO_ID", None)
|
| 22 |
|
| 23 |
+
# Register fonts
|
| 24 |
try:
|
| 25 |
pdfmetrics.registerFont(TTFont('THSarabun', 'THSarabunNew.ttf'))
|
| 26 |
pdfmetrics.registerFont(TTFont('THSarabunBold', 'THSarabunNew-Bold.ttf'))
|
| 27 |
except Exception as e:
|
| 28 |
raise Exception(f"Error loading fonts: {e}")
|
| 29 |
|
| 30 |
+
# Convert timestamp to Thai format
|
| 31 |
def thai_date_time(timestamp):
|
| 32 |
months = {
|
| 33 |
1: "มกราคม", 2: "กุมภาพันธ์", 3: "มีนาคม", 4: "เมษายน",
|
|
|
|
| 43 |
period = "น"
|
| 44 |
return f"{day:02d} {month} {year} {hour:02d}:{minute:02d} {period}"
|
| 45 |
|
| 46 |
+
# Initialize database
|
| 47 |
def init_db():
|
| 48 |
conn = sqlite3.connect("workers.db")
|
| 49 |
c = conn.cursor()
|
|
|
|
| 69 |
conn.commit()
|
| 70 |
conn.close()
|
| 71 |
|
| 72 |
+
# Import JSON data
|
| 73 |
def import_json_to_db():
|
| 74 |
try:
|
| 75 |
json_path = hf_hub_download(repo_id=REPO_ID, filename="worker-database.json", repo_type="space")
|
|
|
|
| 95 |
except Exception as e:
|
| 96 |
print(f"Error importing JSON: {e}")
|
| 97 |
|
| 98 |
+
# Backup database
|
| 99 |
def backup_db():
|
| 100 |
try:
|
| 101 |
hf_hub_upload(repo_id=REPO_ID, path_in_repo="workers.db", path_or_fileobj="workers.db", repo_type="space")
|
| 102 |
except Exception as e:
|
| 103 |
print(f"Error backing up database: {e}")
|
| 104 |
|
| 105 |
+
# Generate next request number
|
| 106 |
def get_next_request_number():
|
| 107 |
conn = sqlite3.connect("workers.db")
|
| 108 |
c = conn.cursor()
|
|
|
|
| 116 |
max_num = max([int(num[0].split('-')[-1]) for num in request_numbers])
|
| 117 |
return f"WP-68-366-{max_num + 1:03d}"
|
| 118 |
|
| 119 |
+
# Create PDF with background
|
| 120 |
def create_receipt_pdf(request_number, english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number):
|
| 121 |
filename = f"pdfs/receipt_{request_number}.pdf"
|
| 122 |
local_path = f"temp_{request_number}.pdf"
|
| 123 |
c = canvas.Canvas(local_path, pagesize=A4)
|
| 124 |
|
| 125 |
+
# Add background image (from index1.html)
|
| 126 |
+
try:
|
| 127 |
+
# Try original Space first
|
| 128 |
+
if ORIGINAL_REPO_ID:
|
| 129 |
+
bg_path = hf_hub_download(repo_id=ORIGINAL_REPO_ID, filename="assets/bg.png", repo_type="space")
|
| 130 |
+
else:
|
| 131 |
+
bg_path = hf_hub_download(repo_id=REPO_ID, filename="assets/bg.png", repo_type="space")
|
| 132 |
+
c.drawImage(bg_path, 0, 0, width=A4[0], height=A4[1], preserveAspectRatio=True)
|
| 133 |
+
except Exception as e:
|
| 134 |
+
print(f"Error loading background image (bg.png): {e}")
|
| 135 |
+
|
| 136 |
+
# Apply styles from main (5).css
|
| 137 |
c.setFont("THSarabunBold", 52.284 / 2.83465) # fs0
|
| 138 |
c.drawString(2*cm, 28*cm, "กระทรวงแรงงาน")
|
| 139 |
c.setFont("THSarabun", 35.212 / 2.83465) # fs3
|
| 140 |
c.drawString(2*cm, 27.5*cm, "ใบเสร็จรับเงิน (ต้นฉบับ)")
|
| 141 |
c.drawString(2*cm, 26*cm, f"เลขที่: {receipt_number}")
|
| 142 |
c.drawString(2*cm, 25.5*cm, "ที่ทำการ: สำนักบริหารแรงงานต่างด้าว")
|
| 143 |
+
c.drawString(2*cm, 25*cm, "วันที่: 01 เมษายน 2568")
|
| 144 |
c.drawString(2*cm, 24.5*cm, f"เลขที่ใบชำระเงิน: {payment_number}")
|
| 145 |
c.drawString(2*cm, 24*cm, f"เลขรับคำขอที่: {request_number}")
|
| 146 |
c.drawString(2*cm, 23.5*cm, f"ชื่อผู้ชำระเงิน: {english_name}")
|
|
|
|
| 159 |
c.drawString(2*cm, 17.5*cm, "(ลงชื่อ) นางสาวอารีวรรณ โพธิ์นิ่มแดง (ผู้รับเงิน)")
|
| 160 |
c.drawString(2*cm, 17*cm, "ตำแหน่ง: นักวิชาการแรงงานชำนาญการ")
|
| 161 |
|
| 162 |
+
# QR Code
|
| 163 |
+
pdf_url = f"[invalid url, do not cite]
|
| 164 |
qr = qrcode.make(pdf_url)
|
| 165 |
qr.save("temp_qr.png")
|
| 166 |
c.drawImage("temp_qr.png", 15*cm, 15*cm, width=3*cm, height=3*cm)
|
| 167 |
|
| 168 |
+
# Add print timestamp
|
| 169 |
+
c.setFont("THSarabun", 20 / 2.83465)
|
| 170 |
print_time = thai_date_time(time.time())
|
| 171 |
c.drawString(2*cm, 2*cm, f"พิมพ์เมื่อ: {print_time}")
|
| 172 |
|
| 173 |
c.showPage()
|
| 174 |
c.save()
|
| 175 |
|
| 176 |
+
# Upload PDF
|
| 177 |
try:
|
| 178 |
hf_hub_upload(repo_id=REPO_ID, path_in_repo=filename, path_or_fileobj=local_path, repo_type="space")
|
| 179 |
except Exception as e:
|
| 180 |
print(f"Error uploading PDF to Space: {e}")
|
| 181 |
|
| 182 |
+
# Upload to original Space (if specified)
|
| 183 |
if ORIGINAL_REPO_ID:
|
| 184 |
try:
|
| 185 |
hf_hub_upload(repo_id=ORIGINAL_REPO_ID, path_in_repo=filename, path_or_fileobj=local_path, repo_type="space")
|
| 186 |
except Exception as e:
|
| 187 |
print(f"Error uploading to original Space: {e}")
|
| 188 |
|
| 189 |
+
# Save PDF info
|
| 190 |
conn = sqlite3.connect("workers.db")
|
| 191 |
c = conn.cursor()
|
| 192 |
c.execute("INSERT OR REPLACE INTO pdfs (request_number, pdf_url) VALUES (?, ?)",
|
|
|
|
| 194 |
conn.commit()
|
| 195 |
conn.close()
|
| 196 |
|
| 197 |
+
return filename
|
| 198 |
|
| 199 |
+
# Validate input
|
| 200 |
def validate_input(english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number):
|
| 201 |
if not id_number or not id_number.isdigit() or len(id_number) != 13:
|
| 202 |
return "หมายเลขประจำตัวต้องเป็นตัวเลข 13 หลัก"
|
|
|
|
| 204 |
return "กรุณากรอกเลขที่ใบเสร็จและหมายเลขชำระเงิน"
|
| 205 |
return None
|
| 206 |
|
| 207 |
+
# Add worker
|
| 208 |
def add_worker(english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number, attachments):
|
| 209 |
error = validate_input(english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number)
|
| 210 |
if error:
|
|
|
|
| 212 |
|
| 213 |
request_number = get_next_request_number()
|
| 214 |
|
| 215 |
+
# Save data
|
| 216 |
conn = sqlite3.connect("workers.db")
|
| 217 |
c = conn.cursor()
|
| 218 |
c.execute('''INSERT INTO workers (
|
|
|
|
| 221 |
request_number, english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number
|
| 222 |
))
|
| 223 |
|
| 224 |
+
# Handle attachments
|
| 225 |
attachment_urls = []
|
| 226 |
for i, file_data in enumerate(attachments or []):
|
| 227 |
filename = f"attachments/file_{request_number}_{i}_{os.path.basename(file_data.name)}"
|
| 228 |
try:
|
| 229 |
hf_hub_upload(repo_id=REPO_ID, path_in_repo=filename, path_or_fileobj=file_data, repo_type="space")
|
| 230 |
+
url = f"[invalid url, do not cite]
|
| 231 |
c.execute("INSERT INTO attachments (request_number, file_url) VALUES (?, ?)", (request_number, url))
|
| 232 |
attachment_urls.append(url)
|
| 233 |
except Exception as e:
|
|
|
|
| 236 |
conn.commit()
|
| 237 |
conn.close()
|
| 238 |
|
| 239 |
+
# Create PDF
|
| 240 |
pdf_path = create_receipt_pdf(request_number, english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number)
|
| 241 |
|
| 242 |
+
# Backup database
|
| 243 |
backup_db()
|
| 244 |
|
| 245 |
return f"เพิ่มข้อมูลพนักงานสำเร็จ! เลขคำขอ: {request_number}\nPDF: {pdf_path}\nไฟล์แนบ: {', '.join(attachment_urls) if attachment_urls else 'ไม่มีไฟล์แนบ'}"
|
| 246 |
|
| 247 |
+
# Download all PDFs
|
| 248 |
def download_all_pdfs():
|
| 249 |
conn = sqlite3.connect("workers.db")
|
| 250 |
c = conn.cursor()
|
|
|
|
| 272 |
|
| 273 |
return f"pdfs/all_receipts.zip"
|
| 274 |
|
| 275 |
+
# Initialize database and import JSON
|
| 276 |
init_db()
|
| 277 |
import_json_to_db()
|
| 278 |
|
| 279 |
+
# Gradio interface
|
| 280 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 281 |
gr.Markdown("## เพิ่มข้อมูลพนักงานใหม่")
|
| 282 |
with gr.Row():
|