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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
import os
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
import pdf2image
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import io
|
| 9 |
+
import tempfile
|
| 10 |
+
|
| 11 |
+
# Load environment variables
|
| 12 |
+
|
| 13 |
+
# Initialize OpenAI client
|
| 14 |
+
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
|
| 15 |
+
|
| 16 |
+
def convert_pdf_to_images(pdf_file):
|
| 17 |
+
"""Convert PDF to list of images"""
|
| 18 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp_file:
|
| 19 |
+
tmp_file.write(pdf_file.getvalue())
|
| 20 |
+
pdf_path = tmp_file.name
|
| 21 |
+
|
| 22 |
+
images = pdf2image.convert_from_path(pdf_path)
|
| 23 |
+
os.unlink(pdf_path)
|
| 24 |
+
return images
|
| 25 |
+
|
| 26 |
+
def format_response(text):
|
| 27 |
+
"""Format the analysis response with clean styling"""
|
| 28 |
+
formatted_text = """
|
| 29 |
+
<div style="
|
| 30 |
+
background-color: white;
|
| 31 |
+
padding: 20px;
|
| 32 |
+
border-radius: 5px;
|
| 33 |
+
font-family: Arial, sans-serif;
|
| 34 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 35 |
+
">
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
+
# Split into pages
|
| 39 |
+
pages = text.split("Page")
|
| 40 |
+
|
| 41 |
+
for page_num, page_content in enumerate(pages[1:], 1): # Skip first empty split
|
| 42 |
+
# Add page header
|
| 43 |
+
formatted_text += f'<div style="margin-bottom: 30px;">'
|
| 44 |
+
formatted_text += f'<h3 style="color: #2c3e50; margin-bottom: 15px;">Page {page_num}</h3>'
|
| 45 |
+
|
| 46 |
+
# Process each line
|
| 47 |
+
lines = page_content.split('\n')
|
| 48 |
+
for line in lines:
|
| 49 |
+
# Skip empty lines and lines with asterisks
|
| 50 |
+
if line.strip() and not line.strip().startswith('*') and not line.strip().startswith('Here'):
|
| 51 |
+
# Remove asterisks and dashes
|
| 52 |
+
line = line.replace('**', '').replace('- ', '')
|
| 53 |
+
|
| 54 |
+
if ':' in line:
|
| 55 |
+
label, value = line.split(':', 1)
|
| 56 |
+
formatted_text += f'<div style="margin-bottom: 10px; display: flex;">'
|
| 57 |
+
formatted_text += f'<span style="font-weight: bold; color: #2c3e50; min-width: 200px;">{label.strip()}</span>'
|
| 58 |
+
formatted_text += f'<span style="color: #34495e; flex: 1;">{value.strip()}</span>'
|
| 59 |
+
formatted_text += '</div>'
|
| 60 |
+
|
| 61 |
+
formatted_text += '</div>'
|
| 62 |
+
|
| 63 |
+
# Add separator between pages except for the last page
|
| 64 |
+
if page_num < len(pages) - 1:
|
| 65 |
+
formatted_text += '<hr style="border: 1px solid #eee; margin: 20px 0;">'
|
| 66 |
+
|
| 67 |
+
formatted_text += '</div>'
|
| 68 |
+
return formatted_text
|
| 69 |
+
|
| 70 |
+
def analyze_image(image):
|
| 71 |
+
"""Analyze image using OpenAI API"""
|
| 72 |
+
try:
|
| 73 |
+
img_byte_arr = io.BytesIO()
|
| 74 |
+
image.save(img_byte_arr, format='PNG')
|
| 75 |
+
img_byte_arr = img_byte_arr.getvalue()
|
| 76 |
+
|
| 77 |
+
base64_image = base64.b64encode(img_byte_arr).decode("utf-8")
|
| 78 |
+
|
| 79 |
+
response = client.chat.completions.create(
|
| 80 |
+
model="gpt-4o-mini",
|
| 81 |
+
messages=[
|
| 82 |
+
{
|
| 83 |
+
"role": "user",
|
| 84 |
+
"content": [
|
| 85 |
+
{
|
| 86 |
+
"type": "text",
|
| 87 |
+
"text": """Please analyze the image and extract the following information:
|
| 88 |
+
- Sender information
|
| 89 |
+
- Recipient information
|
| 90 |
+
- Container details
|
| 91 |
+
- Weights and measurements
|
| 92 |
+
- Dates and reference numbers
|
| 93 |
+
- Cargo details
|
| 94 |
+
|
| 95 |
+
Format the response as 'Label: Value' pairs."""
|
| 96 |
+
},
|
| 97 |
+
{
|
| 98 |
+
"type": "image_url",
|
| 99 |
+
"image_url": {
|
| 100 |
+
"url": f"data:image/jpeg;base64,{base64_image}"
|
| 101 |
+
},
|
| 102 |
+
},
|
| 103 |
+
],
|
| 104 |
+
}
|
| 105 |
+
],
|
| 106 |
+
max_tokens=1000
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
return response.choices[0].message.content
|
| 110 |
+
except Exception as e:
|
| 111 |
+
return f"An error occurred: {str(e)}"
|
| 112 |
+
|
| 113 |
+
def main():
|
| 114 |
+
st.set_page_config(page_title="Document Analysis App", layout="wide")
|
| 115 |
+
|
| 116 |
+
# Custom CSS to set light background and improve button styling
|
| 117 |
+
st.markdown("""
|
| 118 |
+
<style>
|
| 119 |
+
.stApp {
|
| 120 |
+
background-color: white;
|
| 121 |
+
}
|
| 122 |
+
.stButton>button {
|
| 123 |
+
width: 100%;
|
| 124 |
+
background-color: #2c3e50;
|
| 125 |
+
color: white;
|
| 126 |
+
border: none;
|
| 127 |
+
padding: 10px 20px;
|
| 128 |
+
border-radius: 5px;
|
| 129 |
+
margin-top: 20px;
|
| 130 |
+
margin-bottom: 20px;
|
| 131 |
+
}
|
| 132 |
+
.stButton>button:hover {
|
| 133 |
+
background-color: #34495e;
|
| 134 |
+
}
|
| 135 |
+
.uploadedFile {
|
| 136 |
+
margin-bottom: 20px;
|
| 137 |
+
}
|
| 138 |
+
</style>
|
| 139 |
+
""", unsafe_allow_html=True)
|
| 140 |
+
|
| 141 |
+
col1, col2, col3 = st.columns([1,2,1])
|
| 142 |
+
with col2:
|
| 143 |
+
st.title("Document Analysis App")
|
| 144 |
+
|
| 145 |
+
uploaded_file = st.file_uploader("Upload document (PDF/Image)", type=['pdf', 'png', 'jpg', 'jpeg'])
|
| 146 |
+
|
| 147 |
+
if uploaded_file is not None:
|
| 148 |
+
if uploaded_file.type == "application/pdf":
|
| 149 |
+
# Handle PDF
|
| 150 |
+
with st.spinner("Processing PDF..."):
|
| 151 |
+
images = convert_pdf_to_images(uploaded_file)
|
| 152 |
+
|
| 153 |
+
if st.button("Extract Information"):
|
| 154 |
+
with st.spinner("Analyzing document..."):
|
| 155 |
+
all_results = []
|
| 156 |
+
for i, image in enumerate(images, 1):
|
| 157 |
+
result = analyze_image(image)
|
| 158 |
+
all_results.append(f"Page {i} Information:\n{result}")
|
| 159 |
+
|
| 160 |
+
combined_results = "\n\n".join(all_results)
|
| 161 |
+
st.markdown(format_response(combined_results), unsafe_allow_html=True)
|
| 162 |
+
|
| 163 |
+
else:
|
| 164 |
+
# Handle single image
|
| 165 |
+
image = Image.open(uploaded_file)
|
| 166 |
+
|
| 167 |
+
if st.button("Extract Information"):
|
| 168 |
+
with st.spinner("Analyzing document..."):
|
| 169 |
+
result = analyze_image(image)
|
| 170 |
+
st.markdown(format_response(result), unsafe_allow_html=True)
|
| 171 |
+
|
| 172 |
+
if __name__ == "__main__":
|
| 173 |
+
main()
|