Spaces:
Sleeping
Sleeping
Update app.py
#1
by anjaliprasannan - opened
app.py
CHANGED
|
@@ -437,40 +437,51 @@ elif page == "Upload Receipt":
|
|
| 437 |
|
| 438 |
uploaded_file = st.file_uploader("Upload a receipt image", type=["jpg", "jpeg", "png"])
|
| 439 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
if uploaded_file is not None:
|
| 441 |
# Display the uploaded image
|
| 442 |
image = Image.open(uploaded_file)
|
| 443 |
st.image(image, caption="Uploaded Receipt", width=400)
|
| 444 |
|
| 445 |
# Process button
|
| 446 |
-
if st.button("Extract Data from Receipt"):
|
| 447 |
-
|
| 448 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 449 |
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
st.
|
| 453 |
-
st.
|
|
|
|
|
|
|
|
|
|
| 454 |
|
| 455 |
-
#
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
display_df[['Date', 'Description', 'Category', 'Credit', 'Debit', 'Balance']]
|
| 469 |
-
.sort_values(by='Date', ascending=False),
|
| 470 |
-
use_container_width=True
|
| 471 |
-
)
|
| 472 |
-
else:
|
| 473 |
-
st.error("Could not extract transaction data from image. Please try a clearer image or enter the details manually.")
|
| 474 |
|
| 475 |
# Manual entry option if image processing fails
|
| 476 |
st.subheader("Manual Receipt Entry")
|
|
|
|
| 437 |
|
| 438 |
uploaded_file = st.file_uploader("Upload a receipt image", type=["jpg", "jpeg", "png"])
|
| 439 |
|
| 440 |
+
# Create session state variables if they don't exist
|
| 441 |
+
if 'receipt_data' not in st.session_state:
|
| 442 |
+
st.session_state.receipt_data = None
|
| 443 |
+
if 'receipt_processed' not in st.session_state:
|
| 444 |
+
st.session_state.receipt_processed = False
|
| 445 |
+
|
| 446 |
if uploaded_file is not None:
|
| 447 |
# Display the uploaded image
|
| 448 |
image = Image.open(uploaded_file)
|
| 449 |
st.image(image, caption="Uploaded Receipt", width=400)
|
| 450 |
|
| 451 |
# Process button
|
| 452 |
+
if st.button("Extract Data from Receipt") or st.session_state.receipt_processed:
|
| 453 |
+
if not st.session_state.receipt_processed:
|
| 454 |
+
with st.spinner("Processing receipt image..."):
|
| 455 |
+
transaction_data = extract_data_from_receipt(image)
|
| 456 |
+
st.session_state.receipt_data = transaction_data
|
| 457 |
+
st.session_state.receipt_processed = True
|
| 458 |
+
|
| 459 |
+
if st.session_state.receipt_data:
|
| 460 |
+
# Show extracted data
|
| 461 |
+
st.success("Successfully extracted data from receipt!")
|
| 462 |
+
st.json(st.session_state.receipt_data)
|
| 463 |
|
| 464 |
+
# Ask for confirmation
|
| 465 |
+
if st.button("Confirm and Add Transaction"):
|
| 466 |
+
add_expense_to_df(st.session_state.receipt_data)
|
| 467 |
+
st.success("Transaction added to your records!")
|
| 468 |
+
# Reset the receipt processing state
|
| 469 |
+
st.session_state.receipt_processed = False
|
| 470 |
+
st.session_state.receipt_data = None
|
| 471 |
|
| 472 |
+
# Show updated recent transactions
|
| 473 |
+
st.subheader("Recent Transactions")
|
| 474 |
+
display_df = st.session_state.df.copy()
|
| 475 |
+
display_df['Credit'] = display_df['Amount'].apply(lambda x: x if x > 0 else 0)
|
| 476 |
+
display_df['Debit'] = display_df['Amount'].apply(lambda x: abs(x) if x < 0 else 0)
|
| 477 |
+
st.dataframe(
|
| 478 |
+
display_df[['Date', 'Description', 'Category', 'Credit', 'Debit', 'Balance']]
|
| 479 |
+
.sort_values(by='Date', ascending=False),
|
| 480 |
+
use_container_width=True
|
| 481 |
+
)
|
| 482 |
+
else:
|
| 483 |
+
st.error("Could not extract transaction data from image. Please try a clearer image or enter the details manually.")
|
| 484 |
+
st.session_state.receipt_processed = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 485 |
|
| 486 |
# Manual entry option if image processing fails
|
| 487 |
st.subheader("Manual Receipt Entry")
|