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", "
|
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"
|
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")
|
@@ -93,16 +93,16 @@ def import_json_to_db():
|
|
93 |
conn.commit()
|
94 |
conn.close()
|
95 |
except Exception as e:
|
96 |
-
print(f"
|
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"
|
104 |
|
105 |
-
#
|
106 |
def get_next_request_number():
|
107 |
conn = sqlite3.connect("workers.db")
|
108 |
c = conn.cursor()
|
@@ -116,24 +116,25 @@ 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 |
try:
|
127 |
-
#
|
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 |
-
|
|
|
133 |
except Exception as e:
|
134 |
-
print(f"
|
135 |
|
136 |
-
#
|
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
|
@@ -160,12 +161,12 @@ def create_receipt_pdf(request_number, english_name, foreign_reference_number, i
|
|
160 |
c.drawString(2*cm, 17*cm, "ตำแหน่ง: นักวิชาการแรงงานชำนาญการ")
|
161 |
|
162 |
# QR Code
|
163 |
-
pdf_url = f"
|
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 |
-
#
|
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}")
|
@@ -173,20 +174,20 @@ def create_receipt_pdf(request_number, english_name, foreign_reference_number, i
|
|
173 |
c.showPage()
|
174 |
c.save()
|
175 |
|
176 |
-
#
|
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"
|
181 |
|
182 |
-
#
|
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"
|
188 |
|
189 |
-
#
|
190 |
conn = sqlite3.connect("workers.db")
|
191 |
c = conn.cursor()
|
192 |
c.execute("INSERT OR REPLACE INTO pdfs (request_number, pdf_url) VALUES (?, ?)",
|
@@ -196,7 +197,7 @@ def create_receipt_pdf(request_number, english_name, foreign_reference_number, i
|
|
196 |
|
197 |
return filename
|
198 |
|
199 |
-
#
|
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,7 +205,7 @@ def validate_input(english_name, foreign_reference_number, id_number, nationalit
|
|
204 |
return "กรุณากรอกเลขที่ใบเสร็จและหมายเลขชำระเงิน"
|
205 |
return None
|
206 |
|
207 |
-
#
|
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,7 +213,7 @@ def add_worker(english_name, foreign_reference_number, id_number, nationality, r
|
|
212 |
|
213 |
request_number = get_next_request_number()
|
214 |
|
215 |
-
#
|
216 |
conn = sqlite3.connect("workers.db")
|
217 |
c = conn.cursor()
|
218 |
c.execute('''INSERT INTO workers (
|
@@ -221,30 +222,30 @@ def add_worker(english_name, foreign_reference_number, id_number, nationality, r
|
|
221 |
request_number, english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number
|
222 |
))
|
223 |
|
224 |
-
#
|
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"
|
231 |
c.execute("INSERT INTO attachments (request_number, file_url) VALUES (?, ?)", (request_number, url))
|
232 |
attachment_urls.append(url)
|
233 |
except Exception as e:
|
234 |
-
print(f"
|
235 |
|
236 |
conn.commit()
|
237 |
conn.close()
|
238 |
|
239 |
-
#
|
240 |
pdf_path = create_receipt_pdf(request_number, english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number)
|
241 |
|
242 |
-
#
|
243 |
backup_db()
|
244 |
|
245 |
return f"เพิ่มข้อมูลพนักงานสำเร็จ! เลขคำขอ: {request_number}\nPDF: {pdf_path}\nไฟล์แนบ: {', '.join(attachment_urls) if attachment_urls else 'ไม่มีไฟล์แนบ'}"
|
246 |
|
247 |
-
#
|
248 |
def download_all_pdfs():
|
249 |
conn = sqlite3.connect("workers.db")
|
250 |
c = conn.cursor()
|
@@ -268,15 +269,15 @@ def download_all_pdfs():
|
|
268 |
try:
|
269 |
hf_hub_upload(repo_id=REPO_ID, path_in_repo="all_receipts.zip", path_or_fileobj=zip_path, repo_type="space")
|
270 |
except Exception as e:
|
271 |
-
print(f"
|
272 |
|
273 |
return f"pdfs/all_receipts.zip"
|
274 |
|
275 |
-
#
|
276 |
init_db()
|
277 |
import_json_to_db()
|
278 |
|
279 |
-
# Gradio
|
280 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
281 |
gr.Markdown("## เพิ่มข้อมูลพนักงานใหม่")
|
282 |
with gr.Row():
|
|
|
15 |
from dotenv import load_dotenv
|
16 |
import time
|
17 |
|
18 |
+
# อ่าน .env
|
19 |
load_dotenv()
|
20 |
+
REPO_ID = os.getenv("REPO_ID", "protae5544/WorkerManagement")
|
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"ข้อผิดพลาดในการโหลดฟอนต์: {e}")
|
29 |
|
30 |
+
# ฟังก์ชันแปลงวันที่เป็นภาษาไทย
|
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 |
+
# เริ่มต้นฐานข้อมูล
|
47 |
def init_db():
|
48 |
conn = sqlite3.connect("workers.db")
|
49 |
c = conn.cursor()
|
|
|
69 |
conn.commit()
|
70 |
conn.close()
|
71 |
|
72 |
+
# นำเข้า JSON
|
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")
|
|
|
93 |
conn.commit()
|
94 |
conn.close()
|
95 |
except Exception as e:
|
96 |
+
print(f"ข้อผิดพลาดในการนำเข้า 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"ข้อผิดพลาดในการสำรองฐานข้อมูล: {e}")
|
104 |
|
105 |
+
# ดึงเลขคำขอถัดไป
|
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 |
+
# สร้าง PDF ด้วยภาพพื้นหลัง
|
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 |
+
# เพิ่มภาพพื้นหลัง (จาก index1.html)
|
126 |
try:
|
127 |
+
# ลองดาวน์โหลดจาก Space เดิมก่อน
|
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 |
+
# ปรับตำแหน่งตาม x0: 58px, y0: 145px (แปลงเป็น cm: 58*0.0353 ≈ 2.05cm, 145*0.0353 ≈ 5.12cm)
|
133 |
+
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)
|
134 |
except Exception as e:
|
135 |
+
print(f"ข้อผิดพลาดในการโหลดภาพพื้นหลัง (bg.png): {e}")
|
136 |
|
137 |
+
# ใช้สไตล์จาก main (5).css
|
138 |
c.setFont("THSarabunBold", 52.284 / 2.83465) # fs0
|
139 |
c.drawString(2*cm, 28*cm, "กระทรวงแรงงาน")
|
140 |
c.setFont("THSarabun", 35.212 / 2.83465) # fs3
|
|
|
161 |
c.drawString(2*cm, 17*cm, "ตำแหน่ง: นักวิชาการแรงงานชำนาญการ")
|
162 |
|
163 |
# QR Code
|
164 |
+
pdf_url = f"https://huggingface.co/spaces/{REPO_ID}/raw/main/{filename}"
|
165 |
qr = qrcode.make(pdf_url)
|
166 |
qr.save("temp_qr.png")
|
167 |
c.drawImage("temp_qr.png", 15*cm, 15*cm, width=3*cm, height=3*cm)
|
168 |
|
169 |
+
# เพิ่ม print timestamp
|
170 |
c.setFont("THSarabun", 20 / 2.83465)
|
171 |
print_time = thai_date_time(time.time())
|
172 |
c.drawString(2*cm, 2*cm, f"พิมพ์เมื่อ: {print_time}")
|
|
|
174 |
c.showPage()
|
175 |
c.save()
|
176 |
|
177 |
+
# อัพโหลด PDF
|
178 |
try:
|
179 |
hf_hub_upload(repo_id=REPO_ID, path_in_repo=filename, path_or_fileobj=local_path, repo_type="space")
|
180 |
except Exception as e:
|
181 |
+
print(f"ข้อผิดพลาดในการอัพโหลด PDF ไปยัง Space: {e}")
|
182 |
|
183 |
+
# อัพโหลดไปยัง Space เดิม (ถ้ามี)
|
184 |
if ORIGINAL_REPO_ID:
|
185 |
try:
|
186 |
hf_hub_upload(repo_id=ORIGINAL_REPO_ID, path_in_repo=filename, path_or_fileobj=local_path, repo_type="space")
|
187 |
except Exception as e:
|
188 |
+
print(f"ข้อผิดพลาดในการอัพโหลดไปยัง Space เดิม: {e}")
|
189 |
|
190 |
+
# บันทึกข้อมูล PDF
|
191 |
conn = sqlite3.connect("workers.db")
|
192 |
c = conn.cursor()
|
193 |
c.execute("INSERT OR REPLACE INTO pdfs (request_number, pdf_url) VALUES (?, ?)",
|
|
|
197 |
|
198 |
return filename
|
199 |
|
200 |
+
# ตรวจสอบข้อมูล
|
201 |
def validate_input(english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number):
|
202 |
if not id_number or not id_number.isdigit() or len(id_number) != 13:
|
203 |
return "หมายเลขประจำตัวต้องเป็นตัวเลข 13 หลัก"
|
|
|
205 |
return "กรุณากรอกเลขที่ใบเสร็จและหมายเลขชำระเงิน"
|
206 |
return None
|
207 |
|
208 |
+
# เพิ่มข้อมูลพนักงาน
|
209 |
def add_worker(english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number, attachments):
|
210 |
error = validate_input(english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number)
|
211 |
if error:
|
|
|
213 |
|
214 |
request_number = get_next_request_number()
|
215 |
|
216 |
+
# บันทึกข้อมูล
|
217 |
conn = sqlite3.connect("workers.db")
|
218 |
c = conn.cursor()
|
219 |
c.execute('''INSERT INTO workers (
|
|
|
222 |
request_number, english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number
|
223 |
))
|
224 |
|
225 |
+
# จัดการไฟล์แนบ
|
226 |
attachment_urls = []
|
227 |
for i, file_data in enumerate(attachments or []):
|
228 |
filename = f"attachments/file_{request_number}_{i}_{os.path.basename(file_data.name)}"
|
229 |
try:
|
230 |
hf_hub_upload(repo_id=REPO_ID, path_in_repo=filename, path_or_fileobj=file_data, repo_type="space")
|
231 |
+
url = f"https://huggingface.co/spaces/{REPO_ID}/raw/main/{filename}"
|
232 |
c.execute("INSERT INTO attachments (request_number, file_url) VALUES (?, ?)", (request_number, url))
|
233 |
attachment_urls.append(url)
|
234 |
except Exception as e:
|
235 |
+
print(f"ข้อผิดพลาดในการอัพโหลดไฟล์แนบ: {e}")
|
236 |
|
237 |
conn.commit()
|
238 |
conn.close()
|
239 |
|
240 |
+
# สร้าง PDF
|
241 |
pdf_path = create_receipt_pdf(request_number, english_name, foreign_reference_number, id_number, nationality, receipt_number, payment_number)
|
242 |
|
243 |
+
# สำรองฐานข้อมูล
|
244 |
backup_db()
|
245 |
|
246 |
return f"เพิ่มข้อมูลพนักงานสำเร็จ! เลขคำขอ: {request_number}\nPDF: {pdf_path}\nไฟล์แนบ: {', '.join(attachment_urls) if attachment_urls else 'ไม่มีไฟล์แนบ'}"
|
247 |
|
248 |
+
# ดาวน์โหลด PDF ทั้งหมด
|
249 |
def download_all_pdfs():
|
250 |
conn = sqlite3.connect("workers.db")
|
251 |
c = conn.cursor()
|
|
|
269 |
try:
|
270 |
hf_hub_upload(repo_id=REPO_ID, path_in_repo="all_receipts.zip", path_or_fileobj=zip_path, repo_type="space")
|
271 |
except Exception as e:
|
272 |
+
print(f"ข้อผิดพลาดในการอัพโหลด ZIP: {e}")
|
273 |
|
274 |
return f"pdfs/all_receipts.zip"
|
275 |
|
276 |
+
# เริ่มต้นฐานข้อมูลและนำเข้า JSON
|
277 |
init_db()
|
278 |
import_json_to_db()
|
279 |
|
280 |
+
# อินเท���ร์เฟซ Gradio
|
281 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
282 |
gr.Markdown("## เพิ่มข้อมูลพนักงานใหม่")
|
283 |
with gr.Row():
|