Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,26 +6,13 @@ import tempfile
|
|
| 6 |
import shutil
|
| 7 |
from typing import Dict, List
|
| 8 |
import base64
|
|
|
|
|
|
|
| 9 |
|
| 10 |
import pandas as pd
|
| 11 |
import streamlit as st
|
| 12 |
from num2words import num2words
|
| 13 |
|
| 14 |
-
# Install Playwright browsers on first run (for Hugging Face Spaces)
|
| 15 |
-
import subprocess
|
| 16 |
-
import sys
|
| 17 |
-
|
| 18 |
-
if not os.path.exists('/tmp/playwright_installed'):
|
| 19 |
-
try:
|
| 20 |
-
print("π§ Installing Playwright browsers...")
|
| 21 |
-
subprocess.run([sys.executable, "-m", "playwright", "install", "chromium"], check=True)
|
| 22 |
-
with open('/tmp/playwright_installed', 'w') as f:
|
| 23 |
-
f.write('installed')
|
| 24 |
-
print("β
Playwright browsers installed!")
|
| 25 |
-
except Exception as e:
|
| 26 |
-
print(f"β οΈ Failed to install Playwright browsers: {e}")
|
| 27 |
-
print("App will continue but PDF generation may not work")
|
| 28 |
-
|
| 29 |
# Try to import Playwright for PDF conversion
|
| 30 |
PLAYWRIGHT_AVAILABLE = False
|
| 31 |
|
|
@@ -35,6 +22,17 @@ try:
|
|
| 35 |
except ImportError:
|
| 36 |
pass
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# Try to import PyPDF for PDF page extraction
|
| 39 |
PYPDF_AVAILABLE = False
|
| 40 |
|
|
@@ -59,7 +57,7 @@ def format_amount(x):
|
|
| 59 |
num = float(x)
|
| 60 |
if num == 0:
|
| 61 |
return "0.00"
|
| 62 |
-
|
| 63 |
except Exception:
|
| 64 |
return "0.00"
|
| 65 |
|
|
@@ -310,7 +308,7 @@ if __name__ == "__main__":
|
|
| 310 |
|
| 311 |
if result.returncode == 0 and os.path.exists(pdf_path):
|
| 312 |
return True
|
| 313 |
-
|
| 314 |
print(f"Subprocess approach failed: {result.stderr}")
|
| 315 |
|
| 316 |
except Exception as e2:
|
|
@@ -345,7 +343,7 @@ def extract_first_page_pdf(input_pdf_path: str, output_pdf_path: str) -> bool:
|
|
| 345 |
reader = PdfReader(input_pdf_path)
|
| 346 |
|
| 347 |
# Create a new PDF writer
|
| 348 |
-
|
| 349 |
|
| 350 |
# Add only the first page (if it exists)
|
| 351 |
if len(reader.pages) > 0:
|
|
@@ -530,7 +528,7 @@ with col2:
|
|
| 530 |
|
| 531 |
# Processing section
|
| 532 |
if template_file:
|
| 533 |
-
st.markdown("---")
|
| 534 |
st.subheader("π Generate Invoices")
|
| 535 |
|
| 536 |
col3, col4 = st.columns(2)
|
|
@@ -544,8 +542,8 @@ st.markdown("---")
|
|
| 544 |
help="Select Inephos template to enable this button"):
|
| 545 |
if not data_file:
|
| 546 |
st.error("Please upload customer data file.")
|
| 547 |
-
|
| 548 |
-
|
| 549 |
try:
|
| 550 |
zip_buffer, count = process_invoices_html(data_file, "inephos.html", "inephos")
|
| 551 |
st.success(f"β
Generated {count} Inephos invoices successfully!")
|
|
@@ -571,8 +569,8 @@ st.markdown("---")
|
|
| 571 |
help="Select Srestham template to enable this button"):
|
| 572 |
if not data_file:
|
| 573 |
st.error("Please upload customer data file.")
|
| 574 |
-
|
| 575 |
-
|
| 576 |
try:
|
| 577 |
zip_buffer, count = process_invoices_html(data_file, "srestham.html", "srestham")
|
| 578 |
st.success(f"β
Generated {count} Srestham invoices successfully!")
|
|
|
|
| 6 |
import shutil
|
| 7 |
from typing import Dict, List
|
| 8 |
import base64
|
| 9 |
+
import subprocess
|
| 10 |
+
import sys
|
| 11 |
|
| 12 |
import pandas as pd
|
| 13 |
import streamlit as st
|
| 14 |
from num2words import num2words
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# Try to import Playwright for PDF conversion
|
| 17 |
PLAYWRIGHT_AVAILABLE = False
|
| 18 |
|
|
|
|
| 22 |
except ImportError:
|
| 23 |
pass
|
| 24 |
|
| 25 |
+
# Install Playwright browsers on first run
|
| 26 |
+
_playwright_flag = os.path.join(tempfile.gettempdir(), 'playwright_installed')
|
| 27 |
+
if PLAYWRIGHT_AVAILABLE and not os.path.exists(_playwright_flag):
|
| 28 |
+
try:
|
| 29 |
+
subprocess.run([sys.executable, "-m", "playwright", "install", "chromium"], check=True)
|
| 30 |
+
with open(_playwright_flag, 'w') as f:
|
| 31 |
+
f.write('installed')
|
| 32 |
+
print("β
Playwright browsers installed!")
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"β οΈ Failed to install Playwright browsers: {e}")
|
| 35 |
+
|
| 36 |
# Try to import PyPDF for PDF page extraction
|
| 37 |
PYPDF_AVAILABLE = False
|
| 38 |
|
|
|
|
| 57 |
num = float(x)
|
| 58 |
if num == 0:
|
| 59 |
return "0.00"
|
| 60 |
+
return f"{num:,.2f}"
|
| 61 |
except Exception:
|
| 62 |
return "0.00"
|
| 63 |
|
|
|
|
| 308 |
|
| 309 |
if result.returncode == 0 and os.path.exists(pdf_path):
|
| 310 |
return True
|
| 311 |
+
else:
|
| 312 |
print(f"Subprocess approach failed: {result.stderr}")
|
| 313 |
|
| 314 |
except Exception as e2:
|
|
|
|
| 343 |
reader = PdfReader(input_pdf_path)
|
| 344 |
|
| 345 |
# Create a new PDF writer
|
| 346 |
+
writer = PdfWriter()
|
| 347 |
|
| 348 |
# Add only the first page (if it exists)
|
| 349 |
if len(reader.pages) > 0:
|
|
|
|
| 528 |
|
| 529 |
# Processing section
|
| 530 |
if template_file:
|
| 531 |
+
st.markdown("---")
|
| 532 |
st.subheader("π Generate Invoices")
|
| 533 |
|
| 534 |
col3, col4 = st.columns(2)
|
|
|
|
| 542 |
help="Select Inephos template to enable this button"):
|
| 543 |
if not data_file:
|
| 544 |
st.error("Please upload customer data file.")
|
| 545 |
+
else:
|
| 546 |
+
with st.spinner("Generating Inephos invoices..."):
|
| 547 |
try:
|
| 548 |
zip_buffer, count = process_invoices_html(data_file, "inephos.html", "inephos")
|
| 549 |
st.success(f"β
Generated {count} Inephos invoices successfully!")
|
|
|
|
| 569 |
help="Select Srestham template to enable this button"):
|
| 570 |
if not data_file:
|
| 571 |
st.error("Please upload customer data file.")
|
| 572 |
+
else:
|
| 573 |
+
with st.spinner("Generating Srestham invoices..."):
|
| 574 |
try:
|
| 575 |
zip_buffer, count = process_invoices_html(data_file, "srestham.html", "srestham")
|
| 576 |
st.success(f"β
Generated {count} Srestham invoices successfully!")
|