Spaces:
Build error
Build error
sachitksh123
commited on
Commit
•
de76468
1
Parent(s):
a688f02
Upload 2 files
Browse files- main.py +122 -0
- requirements.txt +0 -0
main.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from paddleocr import PaddleOCR
|
4 |
+
import cv2
|
5 |
+
from langchain.chains import LLMChain
|
6 |
+
from langchain_core.prompts import ChatPromptTemplate
|
7 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
from sqlalchemy import create_engine, Column, Integer, String, JSON
|
10 |
+
from sqlalchemy.ext.declarative import declarative_base
|
11 |
+
from sqlalchemy.orm import sessionmaker
|
12 |
+
import google.generativeai as genai
|
13 |
+
|
14 |
+
# Load environment variables
|
15 |
+
load_dotenv()
|
16 |
+
|
17 |
+
# Set up environment variables
|
18 |
+
api_key = os.getenv('API_KEY')
|
19 |
+
DATABASE_URL = "sqlite:///mydatabase.db"
|
20 |
+
|
21 |
+
# Setup database
|
22 |
+
Base = declarative_base()
|
23 |
+
|
24 |
+
class MyDataModel(Base):
|
25 |
+
__tablename__ = 'my_data_table'
|
26 |
+
|
27 |
+
id = Column(Integer, primary_key=True)
|
28 |
+
name = Column(String)
|
29 |
+
data = Column(JSON)
|
30 |
+
|
31 |
+
engine = create_engine(DATABASE_URL)
|
32 |
+
Session = sessionmaker(bind=engine)
|
33 |
+
session = Session()
|
34 |
+
|
35 |
+
Base.metadata.create_all(engine)
|
36 |
+
|
37 |
+
# Initialize Google Generative AI API
|
38 |
+
genai.configure(api_key=api_key)
|
39 |
+
|
40 |
+
# Define OCR function using PaddleOCR
|
41 |
+
def ocr_with_paddle(img_path):
|
42 |
+
finaltext = ''
|
43 |
+
ocr = PaddleOCR(lang='en', use_angle_cls=True)
|
44 |
+
|
45 |
+
|
46 |
+
img = cv2.imread(img_path)
|
47 |
+
|
48 |
+
|
49 |
+
result = ocr.ocr(img)
|
50 |
+
|
51 |
+
|
52 |
+
for line in result[0]:
|
53 |
+
for word_info in line:
|
54 |
+
|
55 |
+
if isinstance(word_info[1], list):
|
56 |
+
text = word_info[1][0]
|
57 |
+
text=str(text)
|
58 |
+
finaltext += text + ' '
|
59 |
+
else:
|
60 |
+
|
61 |
+
finaltext += str(word_info)+' '
|
62 |
+
|
63 |
+
return finaltext.strip()
|
64 |
+
|
65 |
+
# Define the prompt template for extracting invoice details
|
66 |
+
prompt = ChatPromptTemplate.from_messages(
|
67 |
+
[
|
68 |
+
("system", "You are a helpful assistant that extracts invoice details such as invoice number, customer name, date, amount, and other relevant information from a provided invoice text."),
|
69 |
+
("human", "{input}"),
|
70 |
+
]
|
71 |
+
)
|
72 |
+
|
73 |
+
llm = ChatGoogleGenerativeAI(
|
74 |
+
model="gemini-1.5-pro",
|
75 |
+
temperature=0.5,
|
76 |
+
max_tokens=None,
|
77 |
+
timeout=None,
|
78 |
+
max_retries=2,
|
79 |
+
api_key=api_key
|
80 |
+
)
|
81 |
+
|
82 |
+
invoice_chain = LLMChain(prompt=prompt, llm=llm)
|
83 |
+
|
84 |
+
def extract_invoice_details(input_text):
|
85 |
+
response = invoice_chain({"input": input_text})
|
86 |
+
extracted_details = response["text"].strip()
|
87 |
+
return extracted_details
|
88 |
+
|
89 |
+
# Streamlit UI
|
90 |
+
st.title("Invoice OCR and Details Extraction")
|
91 |
+
|
92 |
+
st.write(
|
93 |
+
"Upload an image file to extract the text and invoice details such as invoice number, customer name, date, and amount."
|
94 |
+
)
|
95 |
+
|
96 |
+
# Image Upload
|
97 |
+
uploaded_image = st.file_uploader("Choose an Image", type=["jpg", "jpeg", "png"])
|
98 |
+
|
99 |
+
if uploaded_image is not None:
|
100 |
+
# Save uploaded image to a temporary file
|
101 |
+
img_path = "temp_image.png"
|
102 |
+
with open(img_path, "wb") as f:
|
103 |
+
f.write(uploaded_image.getbuffer())
|
104 |
+
|
105 |
+
# Perform OCR on the uploaded image
|
106 |
+
text = ocr_with_paddle(img_path)
|
107 |
+
st.write("Extracted Text:")
|
108 |
+
st.text_area("OCR Output", text, height=300)
|
109 |
+
|
110 |
+
# Extract invoice details from the text
|
111 |
+
invoice_details = extract_invoice_details(text)
|
112 |
+
|
113 |
+
st.write("Extracted Invoice Details:")
|
114 |
+
st.text_area("Invoice Details", invoice_details, height=300)
|
115 |
+
|
116 |
+
# Save details to the database
|
117 |
+
new_entry = MyDataModel(name="invoice_details", data=invoice_details)
|
118 |
+
session.add(new_entry)
|
119 |
+
session.commit()
|
120 |
+
session.close()
|
121 |
+
|
122 |
+
st.success("Invoice details saved to the database!")
|
requirements.txt
ADDED
Binary file (4.84 kB). View file
|
|