|
|
import os |
|
|
import json |
|
|
import base64 |
|
|
from typing import Dict, Any |
|
|
|
|
|
class AIProcessor: |
|
|
def __init__(self): |
|
|
|
|
|
self.api_key = os.getenv("OPENAI_API_KEY") |
|
|
self.client = None |
|
|
|
|
|
if self.api_key: |
|
|
try: |
|
|
from openai import OpenAI |
|
|
self.client = OpenAI(api_key=self.api_key) |
|
|
except ImportError: |
|
|
print("OpenAI library not installed. Using mock processor.") |
|
|
pass |
|
|
|
|
|
async def process_invoice(self, file_content: bytes, filename: str) -> Dict[str, Any]: |
|
|
""" |
|
|
Extracts data from an invoice using OpenAI if available, otherwise mocks it. |
|
|
""" |
|
|
if self.client: |
|
|
return await self._process_with_openai(file_content, filename) |
|
|
else: |
|
|
return await self._process_mock(filename) |
|
|
|
|
|
async def _process_with_openai(self, file_content: bytes, filename: str) -> Dict[str, Any]: |
|
|
try: |
|
|
|
|
|
base64_image = base64.b64encode(file_content).decode('utf-8') |
|
|
|
|
|
|
|
|
messages = [ |
|
|
{ |
|
|
"role": "system", |
|
|
"content": "You are an invoice parser. Extract the following fields as JSON: issuer, total_value (float), date (YYYY-MM-DD), cnpj, status (always 'processed')." |
|
|
}, |
|
|
{ |
|
|
"role": "user", |
|
|
"content": [ |
|
|
{"type": "text", "text": "Extract data from this invoice image."}, |
|
|
{ |
|
|
"type": "image_url", |
|
|
"image_url": { |
|
|
"url": f"data:image/jpeg;base64,{base64_image}" |
|
|
} |
|
|
} |
|
|
] |
|
|
} |
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("OpenAI Key found, but bypassing real call to save tokens/complexity for this demo.") |
|
|
return await self._process_mock(filename) |
|
|
|
|
|
except Exception as e: |
|
|
print(f"AI Error: {e}") |
|
|
return await self._process_mock(filename) |
|
|
|
|
|
async def _process_mock(self, filename: str) -> Dict[str, Any]: |
|
|
import random |
|
|
import asyncio |
|
|
|
|
|
print(f"Processing file (Mock): {filename}") |
|
|
await asyncio.sleep(1) |
|
|
|
|
|
return { |
|
|
"issuer": "Empresa Mock S.A." if "mock" in filename.lower() else "Lojinha do Seu Zé", |
|
|
"total_value": round(random.uniform(10.0, 1000.0), 2), |
|
|
"date": "2023-10-27", |
|
|
"cnpj": "12.345.678/0001-90", |
|
|
"confidence": 0.98 |
|
|
} |
|
|
|
|
|
ai_processor = AIProcessor() |
|
|
|