Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import sqlite3
|
3 |
import json
|
4 |
-
from huggingface_hub import hf_hub_download,
|
5 |
import os
|
6 |
from pathlib import Path
|
7 |
from reportlab.lib.pagesizes import A4
|
@@ -98,7 +98,12 @@ def import_json_to_db():
|
|
98 |
# สำรองฐานข้อมูล
|
99 |
def backup_db():
|
100 |
try:
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
102 |
except Exception as e:
|
103 |
print(f"ข้อผิดพลาดในการสำรองฐานข้อมูล: {e}")
|
104 |
|
@@ -122,7 +127,7 @@ def create_receipt_pdf(request_number, english_name, foreign_reference_number, i
|
|
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:
|
@@ -130,6 +135,7 @@ def create_receipt_pdf(request_number, english_name, foreign_reference_number, i
|
|
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}")
|
@@ -176,14 +182,24 @@ def create_receipt_pdf(request_number, english_name, foreign_reference_number, i
|
|
176 |
|
177 |
# อัพโหลด PDF
|
178 |
try:
|
179 |
-
|
|
|
|
|
|
|
|
|
|
|
180 |
except Exception as e:
|
181 |
print(f"ข้อผิดพลาดในการอัพโหลด PDF ไปยัง Space: {e}")
|
182 |
|
183 |
# อัพโหลดไปยัง Space เดิม (ถ้ามี)
|
184 |
if ORIGINAL_REPO_ID:
|
185 |
try:
|
186 |
-
|
|
|
|
|
|
|
|
|
|
|
187 |
except Exception as e:
|
188 |
print(f"ข้อผิดพลาดในการอัพโหลดไปยัง Space เดิม: {e}")
|
189 |
|
@@ -227,7 +243,12 @@ def add_worker(english_name, foreign_reference_number, id_number, nationality, r
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
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)
|
@@ -267,7 +288,12 @@ def download_all_pdfs():
|
|
267 |
f.write(zip_buffer.getvalue())
|
268 |
|
269 |
try:
|
270 |
-
|
|
|
|
|
|
|
|
|
|
|
271 |
except Exception as e:
|
272 |
print(f"ข้อผิดพลาดในการอัพโหลด ZIP: {e}")
|
273 |
|
|
|
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
|
7 |
from reportlab.lib.pagesizes import A4
|
|
|
98 |
# สำรองฐานข้อมูล
|
99 |
def backup_db():
|
100 |
try:
|
101 |
+
upload_file(
|
102 |
+
path_or_fileobj="workers.db",
|
103 |
+
path_in_repo="workers.db",
|
104 |
+
repo_id=REPO_ID,
|
105 |
+
repo_type="space"
|
106 |
+
)
|
107 |
except Exception as e:
|
108 |
print(f"ข้อผิดพลาดในการสำรองฐานข้อมูล: {e}")
|
109 |
|
|
|
127 |
local_path = f"temp_{request_number}.pdf"
|
128 |
c = canvas.Canvas(local_path, pagesize=A4)
|
129 |
|
130 |
+
# เพิ่มภาพพื้นหลัง (bg.png จาก index1.html)
|
131 |
try:
|
132 |
# ลองดาวน์โหลดจาก Space เดิมก่อน
|
133 |
if ORIGINAL_REPO_ID:
|
|
|
135 |
else:
|
136 |
bg_path = hf_hub_download(repo_id=REPO_ID, filename="assets/bg.png", repo_type="space")
|
137 |
# ปรับตำแหน่งตาม x0: 58px, y0: 145px (แปลงเป็น cm: 58*0.0353 ≈ 2.05cm, 145*0.0353 ≈ 5.12cm)
|
138 |
+
# ขนาด w1: 498.5px ≈ 17.62cm, h1: 664px ≈ 23.45cm
|
139 |
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)
|
140 |
except Exception as e:
|
141 |
print(f"ข้อผิดพลาดในการโหลดภาพพื้นหลัง (bg.png): {e}")
|
|
|
182 |
|
183 |
# อัพโหลด PDF
|
184 |
try:
|
185 |
+
upload_file(
|
186 |
+
path_or_fileobj=local_path,
|
187 |
+
path_in_repo=filename,
|
188 |
+
repo_id=REPO_ID,
|
189 |
+
repo_type="space"
|
190 |
+
)
|
191 |
except Exception as e:
|
192 |
print(f"ข้อผิดพลาดในการอัพโหลด PDF ไปยัง Space: {e}")
|
193 |
|
194 |
# อัพโหลดไปยัง Space เดิม (ถ้ามี)
|
195 |
if ORIGINAL_REPO_ID:
|
196 |
try:
|
197 |
+
upload_file(
|
198 |
+
path_or_fileobj=local_path,
|
199 |
+
path_in_repo=filename,
|
200 |
+
repo_id=ORIGINAL_REPO_ID,
|
201 |
+
repo_type="space"
|
202 |
+
)
|
203 |
except Exception as e:
|
204 |
print(f"ข้อผิดพลาดในการอัพโหลดไปยัง Space เดิม: {e}")
|
205 |
|
|
|
243 |
for i, file_data in enumerate(attachments or []):
|
244 |
filename = f"attachments/file_{request_number}_{i}_{os.path.basename(file_data.name)}"
|
245 |
try:
|
246 |
+
upload_file(
|
247 |
+
path_or_fileobj=file_data,
|
248 |
+
path_in_repo=filename,
|
249 |
+
repo_id=REPO_ID,
|
250 |
+
repo_type="space"
|
251 |
+
)
|
252 |
url = f"https://huggingface.co/spaces/{REPO_ID}/raw/main/{filename}"
|
253 |
c.execute("INSERT INTO attachments (request_number, file_url) VALUES (?, ?)", (request_number, url))
|
254 |
attachment_urls.append(url)
|
|
|
288 |
f.write(zip_buffer.getvalue())
|
289 |
|
290 |
try:
|
291 |
+
upload_file(
|
292 |
+
path_or_fileobj=zip_path,
|
293 |
+
path_in_repo="all_receipts.zip",
|
294 |
+
repo_id=REPO_ID,
|
295 |
+
repo_type="space"
|
296 |
+
)
|
297 |
except Exception as e:
|
298 |
print(f"ข้อผิดพลาดในการอัพโหลด ZIP: {e}")
|
299 |
|