Spaces:
Runtime error
Runtime error
PISAN commited on
Update app.py
Browse files13 unmodified lines14151617181920212223242526272829303177 unmodified lines10911011111211311411511611711811912012112212312412512612710 unmodified lines13813914014114214314414514614714814915015115215316 unmodified lines170171172173174175176177178238 unmodified lines41741841942042142242342442542642713 unmodified linesimport iofrom dotenv import load_dotenvimport time# อ่าน .env (ถ้ามี)load_dotenv()REPO_ID = os.getenv("REPO_ID", "protae5544/WorkerManagement")ORIGINAL_REPO_ID = os.getenv("ORIGINAL_REPO_ID", None)# ตั้งค่าฟอนต์try:pdfmetrics.registerFont(TTFont('THSarabun', 'THSarabunNew.ttf'))pdfmetrics.registerFont(TTFont('THSarabunBold', 'THSarabunNew-Bold.ttf'))except Exception as e:print(f"Warning: Could not load fonts: {e}")# ฟังก์ชันแปลงวันที่เป็นภาษาไทยdef thai_date_time(timestamp):77 unmodified linesc = canvas.Canvas(local_path, pagesize=A4)# ใช้ฟอนต์ไทยถ้ามี ไม่งั้นใช้ Helveticatry:c.setFont("THSarabunBold", 18)font_available = Trueexcept:c.setFont("Helvetica-Bold", 16)font_available = False# Headerc.drawString(2*cm, 28*cm, "กระทรวงแรงงาน" if font_available else "Ministry of Labour")try:c.setFont("THSarabun", 14) if font_available else c.setFont("Helvetica", 12)except:c.setFont("Helvetica", 12)c.drawString(2*cm, 27.5*cm, "ใบเสร็จรับเงิน (ต้นฉบับ)" if font_available else "Receipt (Original)")10 unmodified linesc.drawString(2*cm, 21*cm, "เลขประจำตัวนายจ้าง: 0415567000061" if font_available else "Employer ID: 0415567000061")# รายการtry:c.setFont("THSarabunBold", 14) if font_available else c.setFont("Helvetica-Bold", 12)except:c.setFont("Helvetica-Bold", 12)c.drawString(2*cm, 20*cm, "รายการ" if font_available else "Items")try:c.setFont("THSarabun", 12) if font_available else c.setFont("Helvetica", 10)except:c.setFont("Helvetica", 10)c.drawString(2*cm, 19.5*cm, "1. ค่าธรรมเนียมในการยื่นคำขอ ฉบับละ 100 บาท: 100.00" if font_available else "1. Application fee 100 Baht each: 100.00")16 unmodified linesprint(f"Error creating QR code: {e}")# Timestamptry:c.setFont("THSarabun", 8) if font_available else c.setFont("Helvetica", 8)except:c.setFont("Helvetica", 8)print_time = thai_date_time(time.time())238 unmodified lineslines=1)attachments = gr.File(label="📎 แนบไฟล์ (สูงสุด 5MB ต่อไฟล์)",file_count="multiple",file_types=[".jpg", ".png", ".pdf", ".jpeg"],max_file_size="5MB")submit_btn = gr.Button(13 unmodified lines1415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767777 unmodified lines15515615715815916016116216316416516616716816917017117217317417517617717817910 unmodified lines19019119219319419519619719819920020120220320420520620720820921021116 unmodified lines228229230231232233234235236237238239238 unmodified lines47847948048148248348448548648748813 unmodified linesimport iofrom dotenv import load_dotenvimport timeimport requests# อ่าน .env (ถ้ามี)load_dotenv()REPO_ID = os.getenv("REPO_ID", "protae5544/WorkerManagement")ORIGINAL_REPO_ID = os.getenv("ORIGINAL_REPO_ID", None)# ดาวน์โหลดฟอนต์จาก Space อื่นdef download_fonts():try:# ดาวน์โหลดฟอนต์จาก protae5544/worberkit2024poefont_urls = {'THSarabunNew.ttf': 'https://huggingface.co/spaces/protae5544/worberkit2024poe/resolve/main/fontCSS/subset-THSarabunNew.woff2','THSarabunNew-Bold.ttf': 'https://huggingface.co/spaces/protae5544/worberkit2024poe/resolve/main/fontCSS/subset-THSarabunNew-Bold.woff2'}# ใช้ฟอนต์ที่มีอยู่แล้วในระบบ หรือดาวน์โหลดจาก Google Fontsgoogle_fonts = {'THSarabunNew.ttf': 'https://fonts.gstatic.com/s/sarabun/v13/DtVjJx26TKEqsc-lWcmw-wKE7wM.ttf','THSarabunNew-Bold.ttf': 'https://fonts.gstatic.com/s/sarabun/v13/DtVmJx26TKEqsc-lWcmw5wq0hwOKBwXcb0A.ttf'}for filename, url in google_fonts.items():if not os.path.exists(filename):try:response = requests.get(url, timeout=10)if response.status_code == 200:with open(filename, 'wb') as f:f.write(response.content)print(f"Downloaded font: {filename}")except Exception as e:print(f"Error downloading {filename}: {e}")except Exception as e:print(f"Error in download_fonts: {e}")# ตั้งค่าฟอนต์def setup_fonts():try:# ดาวน์โหลดฟอนต์ก่อนdownload_fonts()# ลงทะเบียนฟอนต์if os.path.exists('THSarabunNew.ttf'):pdfmetrics.registerFont(TTFont('THSarabun', 'THSarabunNew.ttf'))print("Registered THSarabun font")if os.path.exists('THSarabunNew-Bold.ttf'):pdfmetrics.registerFont(TTFont('THSarabunBold', 'THSarabunNew-Bold.ttf'))print("Registered THSarabunBold font")return Trueexcept Exception as e:print(f"Warning: Could not load fonts: {e}")return False# เริ่มต้นฟอนต์font_available = setup_fonts()# ฟังก์ชันแปลงวันที่เป็นภาษาไทยdef thai_date_time(timestamp):77 unmodified linesc = canvas.Canvas(local_path, pagesize=A4)# ใช้ฟอนต์ไทยถ้ามี ไม่งั้นใช้ Helveticaif font_available:try:c.setFont("THSarabunBold", 18)except:c.setFont("Helvetica-Bold", 16)font_available = Falseelse:c.setFont("Helvetica-Bold", 16)# Headerc.drawString(2*cm, 28*cm, "กระทรวงแรงงาน" if font_available else "Ministry of Labour")if font_available:try:c.setFont("THSarabun", 14)except:c.setFont("Helvetica", 12)font_available = Falseelse:c.setFont("Helvetica", 12)c.drawString(2*cm, 27.5*cm, "ใบเสร็จรับเงิน (ต้นฉบับ)" if font_available else "Receipt (Original)")10 unmodified linesc.drawString(2*cm, 21*cm, "เลขประจำตัวนายจ้าง: 0415567000061" if font_available else "Employer ID: 0415567000061")# รายการif font_available:try:c.setFont("THSarabunBold", 14)except:c.setFont("Helvetica-Bold", 12)else:c.setFont("Helvetica-Bold", 12)c.drawString(2*cm, 20*cm, "รายการ" if font_available else "Items")if font_available:try:c.setFont("THSarabun", 12)except:c.setFont("Helvetica", 10)else:c.setFont("Helvetica", 10)c.drawString(2*cm, 19.5*cm, "1. ค่าธรรมเนียมในการยื่นคำขอ ฉบับละ 100 บาท: 100.00" if font_available else "1. Application fee 100 Baht each: 100.00")16 unmodified linesprint(f"Error creating QR code: {e}")# Timestampif font_available:try:c.setFont("THSarabun", 8)except:c.setFont("Helvetica", 8)else:c.setFont("Helvetica", 8)print_time = thai_date_time(time.time())238 unmodified lineslines=1)# แก้ไข File component - ลบ max_file_sizeattachments = gr.File(label="📎 แนบไฟล์",file_count="multiple",file_types=[".jpg", ".png", ".pdf", ".jpeg"])submit_btn = gr.Button(
app.py+86-25