Spaces:
Runtime error
Runtime error
File size: 11,931 Bytes
479cb67 |
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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
import gradio as gr
import json
import os
from pathlib import Path
import base64
from typing import List, Dict, Any
import google.generativeai as genai
from PIL import Image
import PyPDF2
import io
# Configure Gemini API
# GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
GEMINI_API_KEY = "AIzaSyB2b80YwNHs3Yj6RZOTL8wjXk2YhxCluOA"
if GEMINI_API_KEY:
genai.configure(api_key=GEMINI_API_KEY)
EXTRACTION_PROMPT = """You are a shipping document data extraction specialist. Extract structured data from the provided shipping/logistics documents.
Extract the following fields into a JSON format:
{
"poNumber": "Purchase Order Number",
"shipFrom": "Origin/Ship From Location",
"carrierType": "Transportation type (RAIL/TRUCK/etc)",
"originCarrier": "Carrier name (CN/CPRS/etc)",
"railCarNumber": "Rail car identifier",
"totalQuantity": "Total quantity as number",
"totalUnits": "Unit type (UNIT/MBF/MSFT/etc)",
"accountName": "Customer/Account name",
"inventories": {
"items": [
{
"quantityShipped": "Quantity as number",
"inventoryUnits": "Unit type",
"productName": "Full product description",
"productCode": "Product code/SKU",
"product": {
"category": "Product category (OSB/Lumber/etc)",
"unit": "Unit count as number",
"pcs": "Pieces per unit",
"mbf": "Thousand board feet (if applicable)",
"sf": "Square feet (if applicable)",
"pcsHeight": "Height in inches",
"pcsWidth": "Width in inches",
"pcsLength": "Length in feet"
},
"customFields": [
"Mill||Mill Name",
"Vendor||Vendor Name"
]
}
]
}
}
IMPORTANT INSTRUCTIONS:
1. Extract ALL products/items found in the document
2. Convert text numbers to actual numbers (e.g., "54" β 54)
3. Parse dimensions carefully (e.g., "2X6X14" means height=2, width=6, length=14)
4. Calculate MBF/SF when possible from dimensions and piece count
5. If a field is not found, use null (not empty string)
6. For multiple products, create separate items in the inventories.items array
7. Extract custom fields like Mill, Vendor from document metadata
Return ONLY valid JSON, no markdown formatting or explanations."""
def extract_text_from_pdf(pdf_file) -> str:
"""Extract text from PDF file"""
try:
pdf_reader = PyPDF2.PdfReader(pdf_file)
text = ""
for page in pdf_reader.pages:
text += page.extract_text() + "\n"
return text
except Exception as e:
return f"Error extracting PDF text: {str(e)}"
def convert_pdf_to_images(pdf_file) -> List[Image.Image]:
"""Convert PDF pages to images"""
try:
from pdf2image import convert_from_path
images = convert_from_path(pdf_file)
return images
except ImportError:
# Fallback if pdf2image not available
return []
except Exception as e:
print(f"Error converting PDF to images: {e}")
return []
def process_files(files: List[str]) -> Dict[str, Any]:
"""Process uploaded files and extract text/images"""
processed_data = {
"files": [],
"combined_text": "",
"images": []
}
if not files:
return processed_data
for file_path in files:
file_name = Path(file_path).name
file_ext = Path(file_path).suffix.lower()
file_data = {
"filename": file_name,
"type": file_ext,
"content": ""
}
try:
if file_ext == '.pdf':
# Extract text from PDF
text = extract_text_from_pdf(file_path)
file_data["content"] = text
processed_data["combined_text"] += f"\n--- {file_name} ---\n{text}\n"
# Try to convert PDF to images for visual extraction
images = convert_pdf_to_images(file_path)
processed_data["images"].extend(images)
elif file_ext in ['.jpg', '.jpeg', '.png', '.gif', '.bmp']:
# Load image
img = Image.open(file_path)
processed_data["images"].append(img)
file_data["content"] = f"Image file: {file_name}"
processed_data["combined_text"] += f"\n--- {file_name} (Image) ---\n"
elif file_ext in ['.txt']:
# Read text file
with open(file_path, 'r', encoding='utf-8') as f:
text = f.read()
file_data["content"] = text
processed_data["combined_text"] += f"\n--- {file_name} ---\n{text}\n"
processed_data["files"].append(file_data)
except Exception as e:
file_data["content"] = f"Error processing file: {str(e)}"
processed_data["files"].append(file_data)
return processed_data
def extract_with_gemini(processed_data: Dict[str, Any], api_key: str) -> Dict[str, Any]:
"""Extract structured data using Gemini API"""
if not api_key:
return {"error": "Gemini API key not provided"}
try:
# Configure Gemini with provided API key
genai.configure(api_key=api_key)
# Use Gemini Pro Vision if images available, otherwise Pro
if processed_data["images"]:
model = genai.GenerativeModel('gemini-1.5-flash')
# Prepare content with images and text
content = [EXTRACTION_PROMPT]
# Add text content
if processed_data["combined_text"]:
content.append(f"\nDocument Text:\n{processed_data['combined_text']}")
# Add images (limit to first 5 for token management)
for img in processed_data["images"][:5]:
content.append(img)
response = model.generate_content(content)
else:
# Text-only extraction
model = genai.GenerativeModel('gemini-1.5-flash')
prompt = f"{EXTRACTION_PROMPT}\n\nDocument Text:\n{processed_data['combined_text']}"
response = model.generate_content(prompt)
# Parse response
response_text = response.text.strip()
# Remove markdown code blocks if present
if response_text.startswith("```json"):
response_text = response_text[7:]
if response_text.startswith("```"):
response_text = response_text[3:]
if response_text.endswith("```"):
response_text = response_text[:-3]
response_text = response_text.strip()
# Parse JSON
extracted_data = json.loads(response_text)
return {
"success": True,
"data": extracted_data,
"raw_response": response_text
}
except json.JSONDecodeError as e:
return {
"success": False,
"error": f"JSON parsing error: {str(e)}",
"raw_response": response.text if 'response' in locals() else None
}
except Exception as e:
return {
"success": False,
"error": f"Extraction error: {str(e)}"
}
def process_documents(files, api_key):
"""Main processing function"""
if not files:
return "β οΈ Please upload at least one document.", None, None
if not api_key:
return "β οΈ Please provide your Gemini API key.", None, None
try:
# Step 1: Process files
status = "π Processing files..."
processed_data = process_files(files)
# Step 2: Extract with Gemini
status = "π€ Extracting data with Gemini AI..."
result = extract_with_gemini(processed_data, api_key)
if result.get("success"):
# Format JSON output
json_output = json.dumps(result["data"], indent=2)
# Create summary
data = result["data"]
summary = f"""β
**Extraction Successful!**
**Shipment Details:**
- PO Number: {data.get('poNumber', 'N/A')}
- Ship From: {data.get('shipFrom', 'N/A')}
- Carrier: {data.get('originCarrier', 'N/A')} ({data.get('carrierType', 'N/A')})
- Rail Car: {data.get('railCarNumber', 'N/A')}
- Total Quantity: {data.get('totalQuantity', 'N/A')} {data.get('totalUnits', '')}
- Account: {data.get('accountName', 'N/A')}
**Products Found:** {len(data.get('inventories', {}).get('items', []))}
"""
return summary, json_output, None
else:
error_msg = f"β **Extraction Failed**\n\nError: {result.get('error', 'Unknown error')}"
raw = result.get('raw_response', 'No response')
return error_msg, None, raw
except Exception as e:
return f"β **Processing Error**\n\n{str(e)}", None, None
# Create Gradio Interface
with gr.Blocks(title="Shipping Document Extractor", theme=gr.themes.Soft()) as demo:
gr.Markdown("""
# π¦ Shipping Document Data Extractor
Upload shipping documents (PDFs, images, etc.) to automatically extract structured data.
Powered by Google Gemini AI.
""")
with gr.Row():
with gr.Column(scale=1):
api_key_input = gr.Textbox(
label="π Gemini API Key",
placeholder="Enter your Gemini API key",
type="password",
value=GEMINI_API_KEY or ""
)
gr.Markdown("[Get your API key](https://makersuite.google.com/app/apikey)")
file_input = gr.File(
label="π Upload Documents",
file_count="multiple",
file_types=[".pdf", ".jpg", ".jpeg", ".png", ".txt"]
)
process_btn = gr.Button("π Extract Data", variant="primary", size="lg")
gr.Markdown("""
### Supported Files:
- PDF documents
- Images (JPG, PNG)
- Text files
""")
with gr.Column(scale=2):
status_output = gr.Markdown(label="Status")
with gr.Tabs():
with gr.Tab("π Extracted JSON"):
json_output = gr.Code(
label="Structured Data (JSON)",
language="json",
lines=20
)
download_btn = gr.DownloadButton(
label="πΎ Download JSON",
visible=False
)
with gr.Tab("π Raw Response"):
raw_output = gr.Code(
label="Raw API Response",
language="text",
lines=20
)
# Event handlers
def update_download(json_str):
if json_str:
return gr.DownloadButton(visible=True, value=json_str)
return gr.DownloadButton(visible=False)
process_btn.click(
fn=process_documents,
inputs=[file_input, api_key_input],
outputs=[status_output, json_output, raw_output]
)
json_output.change(
fn=update_download,
inputs=[json_output],
outputs=[download_btn]
)
gr.Markdown("""
---
### π‘ Tips:
- Upload multiple documents at once for batch processing
- Better quality images/PDFs = better extraction accuracy
- The AI extracts: PO numbers, carrier info, products, quantities, dimensions, and more
""")
# Launch the app
if __name__ == "__main__":
demo.launch() |