Spaces:
Sleeping
Sleeping
File size: 3,181 Bytes
715c3e9 6f317bd 715c3e9 6f317bd 8fb878c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
import os
# Force Streamlit to use a writable config directory
os.environ["XDG_CONFIG_HOME"] = "/tmp"
os.environ["STREAMLIT_HOME"] = "/tmp"
os.makedirs("/tmp/.streamlit", exist_ok=True)
import streamlit as st
import pandas as pd
from ocr_llm_utils import run_ocr_with_gcv, extract_table_from_text, extract_markdown_table
import tempfile
from PIL import Image
import io
# Set wide layout
st.set_page_config(page_title="Invoice Processor", layout="wide")
# Sidebar navigation
st.sidebar.title("Navigation")
page = st.sidebar.radio("Go to", ["Charges Incurred", "Invoices to Table"])
# Charges Incurred page (placeholder)
if page == "Charges Incurred":
st.title("Charges Incurred")
col1, col2 = st.columns([1, 1]) # col1 = processing, col2 = image
with col1:
st.subheader("1οΈβ£ Google Cloud Vision OCR Cost")
st.image("assets/gcv_ocr_costs.png", caption="GCV OCR Pricing", use_container_width=True)
with col2:
st.subheader("2οΈβ£ Groq API Cost (LLaMA 4 Scout)")
st.image("assets/groq_api_costs.png", caption="Groq LLM Pricing", use_container_width=True)
st.subheader("3οΈβ£ Combined Cost Summary")
st.image("assets/cost_summary_from_chatgpt.png", caption="Total Estimated Cost for 1000 Invoices", use_container_width=True)
# Invoices to Table
elif page == "Invoices to Table":
st.title("Invoice Table Extractor")
uploaded_file = st.file_uploader("π€ Upload Invoice Image", type=["jpg", "jpeg", "png"])
col1, col2 = st.columns([1.5, 1]) # col1 = processing, col2 = image
if uploaded_file is not None:
# Save image temporarily
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
temp_file.write(uploaded_file.read())
image_path = temp_file.name
with col2:
st.subheader("πΌοΈ Invoice Preview")
st.image(uploaded_file, use_container_width=True)
with col1:
with st.spinner("π Running OCR..."):
text = run_ocr_with_gcv(image_path)
with st.expander("π Extracted Text"):
st.text_area("OCR Text", text, height=300)
with st.spinner("π Extracting Table..."):
table_md = extract_table_from_text(text)
if st.button("π§ Parse Table"):
try:
df = extract_markdown_table(table_md)
st.success("β
Table parsed successfully")
# Preview table
st.subheader("π Table Preview")
st.dataframe(df)
# Download as Excel
excel_buffer = io.BytesIO()
df.to_excel(excel_buffer, index=False)
excel_buffer.seek(0)
st.download_button(
label="π₯ Download Table as Excel",
data=excel_buffer,
file_name="invoice_table.xlsx",
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)
except Exception as e:
st.error(f"β {e}")
|