Spaces:
Sleeping
Sleeping
File size: 8,402 Bytes
6dbd939 3a79f66 6dbd939 3a79f66 6dbd939 3a79f66 6dbd939 995c275 6dbd939 995c275 6dbd939 |
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
import os
import streamlit as st
import pandas as pd
from io import BytesIO
from azure.ai.formrecognizer import DocumentAnalysisClient
from azure.core.credentials import AzureKeyCredential
from PyPDF2 import PdfReader, PdfWriter
from openai import OpenAI
import re
import json
from dotenv import load_dotenv
load_dotenv()
OPENAI_API_KEY=os.getenv("OPENAI_API_KEY")
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
AZURE_KEY=os.getenv("AZURE_KEY")
openaiClient = OpenAI()
# Initialize the DocumentAnalysisClient
document_analysis_client = DocumentAnalysisClient(
endpoint="https://youdata-demo.cognitiveservices.azure.com/",
credential=AzureKeyCredential(AZURE_KEY) # Replace with your Azure key
)
# Function to split PDF and extract the first 4 pages
# def split_pdf_to_first_4_pages(pdf_file, output_pdf_path):
# reader = PdfReader(pdf_file)
# writer = PdfWriter()
# # Only add the first 4 pages
# for i in range(min(4, len(reader.pages))): # Ensure it doesn't exceed the total pages
# writer.add_page(reader.pages[i])
# # Write the small PDF to a file
# with open(output_pdf_path, 'wb') as output_pdf:
# writer.write(output_pdf)
def split_pdf_to_first_4_pages(pdf_file, output_pdf_path):
reader = PdfReader(pdf_file)
writer = PdfWriter()
for i in range(start_page - 1, end_page):
writer.add_page(reader.pages[i])
with open(output_pdf_path, 'wb') as output_pdf:
writer.write(output_pdf)
def split_pdf(pdf_path, start_page, end_page, output_pdf_path):
reader = PdfReader(pdf_path)
writer = PdfWriter()
for i in range(start_page - 1, end_page):
writer.add_page(reader.pages[i])
with open(output_pdf_path, 'wb') as output_pdf:
writer.write(output_pdf)
# Function to extract text from the first 4 pages of a PDF
def extract_text_from_pdf(pdf_file):
# Split the original PDF to get a smaller PDF with only the first 4 pages
small_pdf_path = "small_document.pdf"
split_pdf(pdf_file, 1, 4, "small_document.pdf")
extracted_text = ""
# Check if the small PDF has the correct number of pages
with open("small_document.pdf", "rb") as f:
reader = PdfReader(f)
number_of_pages = len(reader.pages)
print(f"Number of pages in the small PDF: {number_of_pages}")
# Read the smaller PDF for analysis
with open("small_document.pdf", "rb") as f:
document = f.read()
# Analyze the smaller document
poller = document_analysis_client.begin_analyze_document("prebuilt-document", document)
result = poller.result()
# Check how many pages were actually processed by Azure
print(f"Number of pages processed: {len(result.pages)}")
# Extract and print text for each page that was processed
for page_number, page in enumerate(result.pages, start=1):
# print(f"--- Page {page_number} ---")
for line in page.lines:
extracted_text+=line.content
# print("-" * 40)
# Optional: Analyze each page separately if needed
for i in range(1, number_of_pages + 1):
split_pdf(pdf_file, i, i, f"page_{i}.pdf")
with open(f"page_{i}.pdf", "rb") as f:
document = f.read()
poller = document_analysis_client.begin_analyze_document("prebuilt-document", document)
result = poller.result()
# Extract and print text for each page individually
# print(f"--- Separate Analysis for Page {i} ---")
for page in result.pages:
for line in page.lines:
extracted_text+=line.content
# print("-" * 40)
# # Clean up the small PDF file if needed
# os.remove(small_pdf_path)
return extracted_text
output_structure = {
"Name": "String",
"Phone No": "List",
"Designation": "String",
"Date Of Joining": "String",
"Present Address": "String",
"Permanent Address": "String",
"PAN No": "String",
"UAN No": "String",
"AADHAR No": "String",
"Site Code": "String",
"Is Mobile Linked with UAN": "String",
"Uniform Type": "String",
"Shoe Size": "String",
"Height": "String",
"Weight": "String",
"Waist Size": "String",
"Chest Size": "String",
"Do you have any major/minor surgery?": "String",
"Surgey Details": "String",
"Identification Mark": "String",
"Have you ever worked with Govt?": "String",
"Have you ever worked with State Govt?": "String",
"Have you ever worked with PSU?": "String",
"Have you ever worked with Statutory Bodies?": "String",
"Have you ever been convicted?": "String",
"Details of the conviction": "String",
"Father Details": "Dict",
"Mother Details": "Dict",
"Spouse Details": "Dict",
"Brother/Sister Details": "Dict",
"Children Details": "Dict",
"Noinee 1": "String",
"Noinee 2": "String",
"Reference 1": "String",
"Reference 2": "String",
"Account Holder Name": "String",
"Bank Account No": "String",
"IFSC Code": "String",
"Bank Name": "String",
"Branch Location": "String",
# "Is Signed?": "String"
}
# Function to create key-value pairs using GPT-3.5 Turbo
def create_key_value_pairs_from_text(text):
# Make a request to the OpenAI GPT-3.5 Turbo model
response = openaiClient.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": f"""Extract the important details of a person from this text. Always return the response in JSON key-value pairs from the following text: {text}.
here is the desired output strcutre:
{output_structure}. Always write "No" for the surgery and conviction. Only add the details which are there in the given text. Always write "No" for the surgery and conviction.""",
}
]
)
# Extract the content from the response
response_content = response.choices[0].message.content
# Attempt to parse the response content as JSON
try:
key_value_pairs = json.loads(response_content)
except json.JSONDecodeError:
# If the response is not valid JSON, return the raw text instead
key_value_pairs = response_content
return key_value_pairs
# Function to extract JSON from text using regex
def extract_json_from_text(text):
# Use regex to find the JSON block within the text
json_match = re.search(r'```(.*?)```', text, re.DOTALL)
if json_match:
json_str = json_match.group(1).strip() # Extract the JSON string and strip any leading/trailing whitespace
try:
# Parse the JSON string into a Python dictionary
data = json.loads(json_str)
return data
except json.JSONDecodeError as e:
print("Failed to decode JSON:", e)
return None
else:
print("No JSON block found in the text.")
return None
# Streamlit app interface
st.title("Joining Form Details Extractor")
uploaded_files = st.file_uploader("Upload PDF files", accept_multiple_files=True, type="pdf")
if uploaded_files:
st.write("Processing...")
all_data = []
for pdf_file in uploaded_files:
# Extract text from the PDF
extracted_text = extract_text_from_pdf(pdf_file)
# Get key-value pairs using OpenAI GPT-3.5 Turbo
key_value_pairs = create_key_value_pairs_from_text(extracted_text)
# Extract JSON from the returned content
if key_value_pairs:
# data = extract_json_from_text(key_value_pairs)
# if data:
all_data.append(key_value_pairs)
# Convert the list of dictionaries to a DataFrame
if all_data:
df = pd.DataFrame(all_data)
# Display the DataFrame in Streamlit
st.write("Extracted Data:")
st.dataframe(df)
# Convert DataFrame to Excel
output = BytesIO()
with pd.ExcelWriter(output, engine='openpyxl') as writer:
df.to_excel(writer, index=False, sheet_name="Extracted Data")
# Download link for the Excel file
st.download_button(
label="Download Excel",
data=output.getvalue(),
file_name="extracted_data.xlsx",
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)
else:
st.write("No data extracted.")
|