File size: 2,052 Bytes
5daa7b8
 
 
 
 
 
 
 
 
 
 
 
1408b1a
5daa7b8
 
 
1408b1a
5daa7b8
 
 
 
 
 
 
1408b1a
5daa7b8
 
 
 
 
 
 
 
 
1408b1a
5daa7b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1aca446
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
import streamlit as st
from dataclasses import dataclass
import pytesseract
from PIL import Image
import io
import re
import cv2
import numpy as np
import OCR
import os
from  OCR import *


if "messages" not in st.session_state:
    st.session_state.messages = [{"role": "Invoice Reader", "content": "Submit an invoice and I will read it."}]


for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])
HF_TOKEN = os.environ.get("HF_TOKEN")
USER = "user"
ASSISTANT = "Invoice Reader"


uploaded_file = st.file_uploader("Upload an invoice", type=["pdf", "png", "jpg", "jpeg"])
if uploaded_file is not None:
    file_content = uploaded_file.getvalue()
    st.session_state.messages.append({"role": USER, "content": f"Uploaded file: {uploaded_file.name}"})
    with st.chat_message(USER):
        st.markdown(f"Uploaded file: {uploaded_file.name}")

    # Preprocess and extract text from image or PDF
    try:
        text = extract_text_from_image(file_content)

        # Extract specific details
        details = extract_invoice_details(text)

        # Create and display assistant's response to extracted text
        assistant_response = (
            f"Extracted text from the uploaded file:\n\n{text}\n\n"
            f"**Extracted Details:**\n"
            f"**Invoice Number:** {details['Invoice Number']}\n"
            f"**Amount:** {details['Amount']}\n"
            f"**Invoice Date:** {details['Invoice Date']}\n"
        )
        st.session_state.messages.append({"role": ASSISTANT, "content": assistant_response})
        with st.chat_message(ASSISTANT):
            st.markdown(assistant_response)
    except Exception as e:
        error_message = f"An error occurred while processing the file: {e}"
        st.session_state.messages.append({"role": ASSISTANT, "content": error_message})
        with st.chat_message(ASSISTANT):
            st.markdown(error_message)


#streamlit run C:/Users/leahw/PycharmProjects/Int-to-Artificial-Intelligence-Final-Project/app.py