Spaces:
Sleeping
Sleeping
Upload main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pdf_to_image import pdf_to_image
|
| 2 |
+
from image_to_text import image_to_text
|
| 3 |
+
from mirascope_extractor import extractor
|
| 4 |
+
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
import pandas as pd
|
| 7 |
+
|
| 8 |
+
import glob
|
| 9 |
+
import os
|
| 10 |
+
from dotenv import load_dotenv
|
| 11 |
+
import streamlit as st
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
global empty_df
|
| 23 |
+
openai_api_key = os.getenv('OPENAI_API_KEY')
|
| 24 |
+
genai.configure(api_key=openai_api_key)
|
| 25 |
+
|
| 26 |
+
folder_name = "/project/workspace/pdfs"
|
| 27 |
+
invoice_pdfs = glob.glob(os.path.join(folder_name, '*.pdf')) + glob.glob(os.path.join(folder_name, '*.PDF'))
|
| 28 |
+
print(f'Invoices_pdfs: {invoice_pdfs}')
|
| 29 |
+
|
| 30 |
+
for pdf_path in invoice_pdfs:
|
| 31 |
+
convert_image = pdf_to_image(pdf_path)
|
| 32 |
+
convert_image.save_image(f'{pdf_path}image')
|
| 33 |
+
print('one_pdf_converted')
|
| 34 |
+
|
| 35 |
+
all_images = glob.glob(os.path.join(folder_name, '*.jpg'))
|
| 36 |
+
all_texts = []
|
| 37 |
+
for image_path in all_images:
|
| 38 |
+
text = image_to_text(image_path)
|
| 39 |
+
all_texts.append(text)
|
| 40 |
+
print('one text appended')
|
| 41 |
+
|
| 42 |
+
empty_df = pd.DataFrame()
|
| 43 |
+
|
| 44 |
+
for text in all_texts:
|
| 45 |
+
extracted_text = extractor(text)
|
| 46 |
+
task_details_dict = extracted_text.dict()
|
| 47 |
+
df = pd.DataFrame([task_details_dict])
|
| 48 |
+
empty_df = pd.concat([empty_df, df])
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
empty_df.to_csv('extracted_data.csv')
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|