f
No files matched your search
@@ -0,0 +1 @@
|
||||
*/**.env
|
||||
@@ -0,0 +1,10 @@
|
||||
# Dependencies
|
||||
/node_modules
|
||||
/client/node_modules
|
||||
|
||||
# Environment variables
|
||||
.env
|
||||
server/.env
|
||||
|
||||
# Build output
|
||||
/client/build
|
||||
@@ -0,0 +1,23 @@
|
||||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||
|
||||
# dependencies
|
||||
/node_modules
|
||||
/.pnp
|
||||
.pnp.js
|
||||
|
||||
# testing
|
||||
/coverage
|
||||
|
||||
# production
|
||||
/build
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
.env.local
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
@@ -0,0 +1,70 @@
|
||||
# Getting Started with Create React App
|
||||
|
||||
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
|
||||
|
||||
## Available Scripts
|
||||
|
||||
In the project directory, you can run:
|
||||
|
||||
### `npm start`
|
||||
|
||||
Runs the app in the development mode.\
|
||||
Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
|
||||
|
||||
The page will reload when you make changes.\
|
||||
You may also see any lint errors in the console.
|
||||
|
||||
### `npm test`
|
||||
|
||||
Launches the test runner in the interactive watch mode.\
|
||||
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
|
||||
|
||||
### `npm run build`
|
||||
|
||||
Builds the app for production to the `build` folder.\
|
||||
It correctly bundles React in production mode and optimizes the build for the best performance.
|
||||
|
||||
The build is minified and the filenames include the hashes.\
|
||||
Your app is ready to be deployed!
|
||||
|
||||
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
|
||||
|
||||
### `npm run eject`
|
||||
|
||||
**Note: this is a one-way operation. Once you `eject`, you can't go back!**
|
||||
|
||||
If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
|
||||
|
||||
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
|
||||
|
||||
You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.
|
||||
|
||||
## Learn More
|
||||
|
||||
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
|
||||
|
||||
To learn React, check out the [React documentation](https://reactjs.org/).
|
||||
|
||||
### Code Splitting
|
||||
|
||||
This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
|
||||
|
||||
### Analyzing the Bundle Size
|
||||
|
||||
This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
|
||||
|
||||
### Making a Progressive Web App
|
||||
|
||||
This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
|
||||
|
||||
### Advanced Configuration
|
||||
|
||||
This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
|
||||
|
||||
### Deployment
|
||||
|
||||
This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
|
||||
|
||||
### `npm run build` fails to minify
|
||||
|
||||
This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
|
||||
|
After Width: | Height: | Size: 3.8 KiB |
@@ -0,0 +1,4 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="currentColor">
|
||||
<path d="M4 1h8a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V3a2 2 0 0 1 2-2zm0 1a1 1 0 0 0-1 1v10a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1V3a1 1 0 0 0-1-1H4z"/>
|
||||
<path d="M5 5h6v1H5V5zm0 2h6v1H5V7zm0 2h4v1H5V9z"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 297 B |
@@ -0,0 +1,43 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<link rel="icon" href="%PUBLIC_URL%/favicon.svg" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<meta name="theme-color" content="#000000" />
|
||||
<meta
|
||||
name="description"
|
||||
content="Web site created using create-react-app"
|
||||
/>
|
||||
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
|
||||
<!--
|
||||
manifest.json provides metadata used when your web app is installed on a
|
||||
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
|
||||
-->
|
||||
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
|
||||
<!--
|
||||
Notice the use of %PUBLIC_URL% in the tags above.
|
||||
It will be replaced with the URL of the `public` folder during the build.
|
||||
Only files inside the `public` folder can be referenced from the HTML.
|
||||
|
||||
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
|
||||
work correctly both with client-side routing and a non-root public URL.
|
||||
Learn how to configure a non-root public URL by running `npm run build`.
|
||||
-->
|
||||
<title>Azure Document Analyzer</title>
|
||||
</head>
|
||||
<body>
|
||||
<noscript>You need to enable JavaScript to run this app.</noscript>
|
||||
<div id="root"></div>
|
||||
<!--
|
||||
This HTML file is a template.
|
||||
If you open it directly in the browser, you will see an empty page.
|
||||
|
||||
You can add webfonts, meta tags, or analytics to this file.
|
||||
The build step will place the bundled scripts into the <body> tag.
|
||||
|
||||
To begin the development, run `npm start` or `yarn start`.
|
||||
To create a production bundle, use `npm run build` or `yarn build`.
|
||||
-->
|
||||
</body>
|
||||
</html>
|
||||
|
After Width: | Height: | Size: 5.2 KiB |
|
After Width: | Height: | Size: 9.4 KiB |
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"short_name": "React App",
|
||||
"name": "Create React App Sample",
|
||||
"icons": [
|
||||
{
|
||||
"src": "favicon.ico",
|
||||
"sizes": "64x64 32x32 24x24 16x16",
|
||||
"type": "image/x-icon"
|
||||
},
|
||||
{
|
||||
"src": "logo192.png",
|
||||
"type": "image/png",
|
||||
"sizes": "192x192"
|
||||
},
|
||||
{
|
||||
"src": "logo512.png",
|
||||
"type": "image/png",
|
||||
"sizes": "512x512"
|
||||
}
|
||||
],
|
||||
"start_url": ".",
|
||||
"display": "standalone",
|
||||
"theme_color": "#000000",
|
||||
"background_color": "#ffffff"
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
# https://www.robotstxt.org/robotstxt.html
|
||||
User-agent: *
|
||||
Disallow:
|
||||
@@ -0,0 +1,91 @@
|
||||
body {
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
|
||||
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
|
||||
sans-serif;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
background-color: #282c34;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.App {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.App-header {
|
||||
background-color: #1a1d24;
|
||||
padding: 20px;
|
||||
color: white;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 2.5rem;
|
||||
}
|
||||
|
||||
main {
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.upload-section {
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
input[type="file"] {
|
||||
margin-right: 10px;
|
||||
}
|
||||
|
||||
button {
|
||||
background-color: #61dafb;
|
||||
color: #282c34;
|
||||
border: none;
|
||||
padding: 10px 20px;
|
||||
font-size: 1rem;
|
||||
cursor: pointer;
|
||||
border-radius: 5px;
|
||||
transition: background-color 0.3s ease;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background-color: #4fa8c5;
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
background-color: #ccc;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.error-message {
|
||||
color: #ff6b6b;
|
||||
margin: 20px 0;
|
||||
}
|
||||
|
||||
.results-section {
|
||||
margin-top: 20px;
|
||||
text-align: left;
|
||||
background-color: #20232a;
|
||||
padding: 20px;
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
pre {
|
||||
white-space: pre-wrap;
|
||||
word-wrap: break-word;
|
||||
font-family: 'Courier New', Courier, monospace;
|
||||
}
|
||||
|
||||
.results-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.clear-button {
|
||||
background-color: #ff6b6b;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.clear-button:hover {
|
||||
background-color: #e05252;
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
import React, { useState, useRef } from 'react';
|
||||
import './App.css';
|
||||
|
||||
function App() {
|
||||
const [file, setFile] = useState(null);
|
||||
const [analysisResult, setAnalysisResult] = useState(null);
|
||||
const [error, setError] = useState(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const fileInputRef = useRef(null);
|
||||
|
||||
const handleFileChange = (e) => {
|
||||
setFile(e.target.files[0]);
|
||||
};
|
||||
|
||||
const handleAnalyzeClick = async () => {
|
||||
if (!file) {
|
||||
setError('Please select a file first.');
|
||||
return;
|
||||
}
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('document', file);
|
||||
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
setAnalysisResult(null);
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/analyze', {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(result.error || 'Failed to analyze document.');
|
||||
}
|
||||
|
||||
setAnalysisResult(result);
|
||||
|
||||
// Trigger download
|
||||
const blob = new Blob([JSON.stringify(result, null, 2)], { type: 'application/json' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = `analysis-result.json`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
} catch (err) {
|
||||
setError(err.message);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleClear = () => {
|
||||
setFile(null);
|
||||
setAnalysisResult(null);
|
||||
setError(null);
|
||||
if (fileInputRef.current) {
|
||||
fileInputRef.current.value = '';
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="App">
|
||||
<header className="App-header">
|
||||
<h1>Azure Document Analyzer</h1>
|
||||
<p>Upload a document (PDF, Word, Excel) to analyze its content.</p>
|
||||
<p style={{fontSize: '0.8rem', opacity: 0.8}}>(Recommended formats: .pdf, .docx, .xlsx)</p>
|
||||
</header>
|
||||
<main>
|
||||
<div className="upload-section">
|
||||
<input
|
||||
type="file"
|
||||
onChange={handleFileChange}
|
||||
accept=".pdf,.docx,.xlsx,.pptx,.jpg,.png"
|
||||
ref={fileInputRef}
|
||||
/>
|
||||
<button onClick={handleAnalyzeClick} disabled={loading || !file}>
|
||||
{loading ? 'Analyzing...' : 'Analyze Document'}
|
||||
</button>
|
||||
</div>
|
||||
{error && <div className="error-message">{error}</div>}
|
||||
{analysisResult && (
|
||||
<div className="results-section">
|
||||
<div className="results-header">
|
||||
<h2>Analysis Result</h2>
|
||||
<button onClick={handleClear} className="clear-button">Clear</button>
|
||||
</div>
|
||||
<pre>{JSON.stringify(analysisResult, null, 2)}</pre>
|
||||
</div>
|
||||
)}
|
||||
</main>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default App;
|
||||
@@ -0,0 +1,8 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import App from './App';
|
||||
|
||||
test('renders learn react link', () => {
|
||||
render(<App />);
|
||||
const linkElement = screen.getByText(/learn react/i);
|
||||
expect(linkElement).toBeInTheDocument();
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
body {
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
|
||||
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
|
||||
sans-serif;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
}
|
||||
|
||||
code {
|
||||
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
|
||||
monospace;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom/client';
|
||||
import './index.css';
|
||||
import App from './App';
|
||||
import reportWebVitals from './reportWebVitals';
|
||||
|
||||
const root = ReactDOM.createRoot(document.getElementById('root'));
|
||||
root.render(
|
||||
<React.StrictMode>
|
||||
<App />
|
||||
</React.StrictMode>
|
||||
);
|
||||
|
||||
// If you want to start measuring performance in your app, pass a function
|
||||
// to log results (for example: reportWebVitals(console.log))
|
||||
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
|
||||
reportWebVitals();
|
||||
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 841.9 595.3"><g fill="#61DAFB"><path d="M666.3 296.5c0-32.5-40.7-63.3-103.1-82.4 14.4-63.6 8-114.2-20.2-130.4-6.5-3.8-14.1-5.6-22.4-5.6v22.3c4.6 0 8.3.9 11.4 2.6 13.6 7.8 19.5 37.5 14.9 75.7-1.1 9.4-2.9 19.3-5.1 29.4-19.6-4.8-41-8.5-63.5-10.9-13.5-18.5-27.5-35.3-41.6-50 32.6-30.3 63.2-46.9 84-46.9V78c-27.5 0-63.5 19.6-99.9 53.6-36.4-33.8-72.4-53.2-99.9-53.2v22.3c20.7 0 51.4 16.5 84 46.6-14 14.7-28 31.4-41.3 49.9-22.6 2.4-44 6.1-63.6 11-2.3-10-4-19.7-5.2-29-4.7-38.2 1.1-67.9 14.6-75.8 3-1.8 6.9-2.6 11.5-2.6V78.5c-8.4 0-16 1.8-22.6 5.6-28.1 16.2-34.4 66.7-19.9 130.1-62.2 19.2-102.7 49.9-102.7 82.3 0 32.5 40.7 63.3 103.1 82.4-14.4 63.6-8 114.2 20.2 130.4 6.5 3.8 14.1 5.6 22.5 5.6 27.5 0 63.5-19.6 99.9-53.6 36.4 33.8 72.4 53.2 99.9 53.2 8.4 0 16-1.8 22.6-5.6 28.1-16.2 34.4-66.7 19.9-130.1 62-19.1 102.5-49.9 102.5-82.3zm-130.2-66.7c-3.7 12.9-8.3 26.2-13.5 39.5-4.1-8-8.4-16-13.1-24-4.6-8-9.5-15.8-14.4-23.4 14.2 2.1 27.9 4.7 41 7.9zm-45.8 106.5c-7.8 13.5-15.8 26.3-24.1 38.2-14.9 1.3-30 2-45.2 2-15.1 0-30.2-.7-45-1.9-8.3-11.9-16.4-24.6-24.2-38-7.6-13.1-14.5-26.4-20.8-39.8 6.2-13.4 13.2-26.8 20.7-39.9 7.8-13.5 15.8-26.3 24.1-38.2 14.9-1.3 30-2 45.2-2 15.1 0 30.2.7 45 1.9 8.3 11.9 16.4 24.6 24.2 38 7.6 13.1 14.5 26.4 20.8 39.8-6.3 13.4-13.2 26.8-20.7 39.9zm32.3-13c5.4 13.4 10 26.8 13.8 39.8-13.1 3.2-26.9 5.9-41.2 8 4.9-7.7 9.8-15.6 14.4-23.7 4.6-8 8.9-16.1 13-24.1zM421.2 430c-9.3-9.6-18.6-20.3-27.8-32 9 .4 18.2.7 27.5.7 9.4 0 18.7-.2 27.8-.7-9 11.7-18.3 22.4-27.5 32zm-74.4-58.9c-14.2-2.1-27.9-4.7-41-7.9 3.7-12.9 8.3-26.2 13.5-39.5 4.1 8 8.4 16 13.1 24 4.7 8 9.5 15.8 14.4 23.4zM420.7 163c9.3 9.6 18.6 20.3 27.8 32-9-.4-18.2-.7-27.5-.7-9.4 0-18.7.2-27.8.7 9-11.7 18.3-22.4 27.5-32zm-74 58.9c-4.9 7.7-9.8 15.6-14.4 23.7-4.6 8-8.9 16-13 24-5.4-13.4-10-26.8-13.8-39.8 13.1-3.1 26.9-5.8 41.2-7.9zm-90.5 125.2c-35.4-15.1-58.3-34.9-58.3-50.6 0-15.7 22.9-35.6 58.3-50.6 8.6-3.7 18-7 27.7-10.1 5.7 19.6 13.2 40 22.5 60.9-9.2 20.8-16.6 41.1-22.2 60.6-9.9-3.1-19.3-6.5-28-10.2zM310 490c-13.6-7.8-19.5-37.5-14.9-75.7 1.1-9.4 2.9-19.3 5.1-29.4 19.6 4.8 41 8.5 63.5 10.9 13.5 18.5 27.5 35.3 41.6 50-32.6 30.3-63.2 46.9-84 46.9-4.5-.1-8.3-1-11.3-2.7zm237.2-76.2c4.7 38.2-1.1 67.9-14.6 75.8-3 1.8-6.9 2.6-11.5 2.6-20.7 0-51.4-16.5-84-46.6 14-14.7 28-31.4 41.3-49.9 22.6-2.4 44-6.1 63.6-11 2.3 10.1 4.1 19.8 5.2 29.1zm38.5-66.7c-8.6 3.7-18 7-27.7 10.1-5.7-19.6-13.2-40-22.5-60.9 9.2-20.8 16.6-41.1 22.2-60.6 9.9 3.1 19.3 6.5 28.1 10.2 35.4 15.1 58.3 34.9 58.3 50.6-.1 15.7-23 35.6-58.4 50.6zM320.8 78.4z"/><circle cx="420.9" cy="296.5" r="45.7"/><path d="M520.5 78.1z"/></g></svg>
|
||||
|
After Width: | Height: | Size: 2.6 KiB |
@@ -0,0 +1,13 @@
|
||||
const reportWebVitals = onPerfEntry => {
|
||||
if (onPerfEntry && onPerfEntry instanceof Function) {
|
||||
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
|
||||
getCLS(onPerfEntry);
|
||||
getFID(onPerfEntry);
|
||||
getFCP(onPerfEntry);
|
||||
getLCP(onPerfEntry);
|
||||
getTTFB(onPerfEntry);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export default reportWebVitals;
|
||||
@@ -0,0 +1,5 @@
|
||||
// jest-dom adds custom jest matchers for asserting on DOM nodes.
|
||||
// allows you to do things like:
|
||||
// expect(element).toHaveTextContent(/react/i)
|
||||
// learn more: https://github.com/testing-library/jest-dom
|
||||
import '@testing-library/jest-dom';
|
||||
@@ -0,0 +1,330 @@
|
||||
{
|
||||
"name": "document-analyzer",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "document-analyzer",
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"concurrently": "^9.2.1"
|
||||
}
|
||||
},
|
||||
"node_modules/ansi-regex": {
|
||||
"version": "5.0.1",
|
||||
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz",
|
||||
"integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/ansi-styles": {
|
||||
"version": "4.3.0",
|
||||
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
|
||||
"integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"color-convert": "^2.0.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/chalk/ansi-styles?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/chalk": {
|
||||
"version": "4.1.2",
|
||||
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
|
||||
"integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"ansi-styles": "^4.1.0",
|
||||
"supports-color": "^7.1.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/chalk/chalk?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/chalk/node_modules/supports-color": {
|
||||
"version": "7.2.0",
|
||||
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
|
||||
"integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"has-flag": "^4.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/cliui": {
|
||||
"version": "8.0.1",
|
||||
"resolved": "https://registry.npmjs.org/cliui/-/cliui-8.0.1.tgz",
|
||||
"integrity": "sha512-BSeNnyus75C4//NQ9gQt1/csTXyo/8Sb+afLAkzAptFuMsod9HFokGNudZpi/oQV73hnVK+sR+5PVRMd+Dr7YQ==",
|
||||
"dev": true,
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"string-width": "^4.2.0",
|
||||
"strip-ansi": "^6.0.1",
|
||||
"wrap-ansi": "^7.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
}
|
||||
},
|
||||
"node_modules/color-convert": {
|
||||
"version": "2.0.1",
|
||||
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
|
||||
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"color-name": "~1.1.4"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=7.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/color-name": {
|
||||
"version": "1.1.4",
|
||||
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
|
||||
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/concurrently": {
|
||||
"version": "9.2.1",
|
||||
"resolved": "https://registry.npmjs.org/concurrently/-/concurrently-9.2.1.tgz",
|
||||
"integrity": "sha512-fsfrO0MxV64Znoy8/l1vVIjjHa29SZyyqPgQBwhiDcaW8wJc2W3XWVOGx4M3oJBnv/zdUZIIp1gDeS98GzP8Ng==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"chalk": "4.1.2",
|
||||
"rxjs": "7.8.2",
|
||||
"shell-quote": "1.8.3",
|
||||
"supports-color": "8.1.1",
|
||||
"tree-kill": "1.2.2",
|
||||
"yargs": "17.7.2"
|
||||
},
|
||||
"bin": {
|
||||
"conc": "dist/bin/concurrently.js",
|
||||
"concurrently": "dist/bin/concurrently.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/open-cli-tools/concurrently?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/emoji-regex": {
|
||||
"version": "8.0.0",
|
||||
"resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz",
|
||||
"integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/escalade": {
|
||||
"version": "3.2.0",
|
||||
"resolved": "https://registry.npmjs.org/escalade/-/escalade-3.2.0.tgz",
|
||||
"integrity": "sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
}
|
||||
},
|
||||
"node_modules/get-caller-file": {
|
||||
"version": "2.0.5",
|
||||
"resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz",
|
||||
"integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==",
|
||||
"dev": true,
|
||||
"license": "ISC",
|
||||
"engines": {
|
||||
"node": "6.* || 8.* || >= 10.*"
|
||||
}
|
||||
},
|
||||
"node_modules/has-flag": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
|
||||
"integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/is-fullwidth-code-point": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz",
|
||||
"integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/require-directory": {
|
||||
"version": "2.1.1",
|
||||
"resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",
|
||||
"integrity": "sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/rxjs": {
|
||||
"version": "7.8.2",
|
||||
"resolved": "https://registry.npmjs.org/rxjs/-/rxjs-7.8.2.tgz",
|
||||
"integrity": "sha512-dhKf903U/PQZY6boNNtAGdWbG85WAbjT/1xYoZIC7FAY0yWapOBQVsVrDl58W86//e1VpMNBtRV4MaXfdMySFA==",
|
||||
"dev": true,
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"tslib": "^2.1.0"
|
||||
}
|
||||
},
|
||||
"node_modules/shell-quote": {
|
||||
"version": "1.8.3",
|
||||
"resolved": "https://registry.npmjs.org/shell-quote/-/shell-quote-1.8.3.tgz",
|
||||
"integrity": "sha512-ObmnIF4hXNg1BqhnHmgbDETF8dLPCggZWBjkQfhZpbszZnYur5DUljTcCHii5LC3J5E0yeO/1LIMyH+UvHQgyw==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">= 0.4"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/ljharb"
|
||||
}
|
||||
},
|
||||
"node_modules/string-width": {
|
||||
"version": "4.2.3",
|
||||
"resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz",
|
||||
"integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"emoji-regex": "^8.0.0",
|
||||
"is-fullwidth-code-point": "^3.0.0",
|
||||
"strip-ansi": "^6.0.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/strip-ansi": {
|
||||
"version": "6.0.1",
|
||||
"resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz",
|
||||
"integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"ansi-regex": "^5.0.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/supports-color": {
|
||||
"version": "8.1.1",
|
||||
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-8.1.1.tgz",
|
||||
"integrity": "sha512-MpUEN2OodtUzxvKQl72cUF7RQ5EiHsGvSsVG0ia9c5RbWGL2CI4C7EpPS8UTBIplnlzZiNuV56w+FuNxy3ty2Q==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"has-flag": "^4.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/chalk/supports-color?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/tree-kill": {
|
||||
"version": "1.2.2",
|
||||
"resolved": "https://registry.npmjs.org/tree-kill/-/tree-kill-1.2.2.tgz",
|
||||
"integrity": "sha512-L0Orpi8qGpRG//Nd+H90vFB+3iHnue1zSSGmNOOCh1GLJ7rUKVwV2HvijphGQS2UmhUZewS9VgvxYIdgr+fG1A==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"bin": {
|
||||
"tree-kill": "cli.js"
|
||||
}
|
||||
},
|
||||
"node_modules/tslib": {
|
||||
"version": "2.8.1",
|
||||
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz",
|
||||
"integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==",
|
||||
"dev": true,
|
||||
"license": "0BSD"
|
||||
},
|
||||
"node_modules/wrap-ansi": {
|
||||
"version": "7.0.0",
|
||||
"resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz",
|
||||
"integrity": "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"ansi-styles": "^4.0.0",
|
||||
"string-width": "^4.1.0",
|
||||
"strip-ansi": "^6.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/chalk/wrap-ansi?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/y18n": {
|
||||
"version": "5.0.8",
|
||||
"resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz",
|
||||
"integrity": "sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==",
|
||||
"dev": true,
|
||||
"license": "ISC",
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
}
|
||||
},
|
||||
"node_modules/yargs": {
|
||||
"version": "17.7.2",
|
||||
"resolved": "https://registry.npmjs.org/yargs/-/yargs-17.7.2.tgz",
|
||||
"integrity": "sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"cliui": "^8.0.1",
|
||||
"escalade": "^3.1.1",
|
||||
"get-caller-file": "^2.0.5",
|
||||
"require-directory": "^2.1.1",
|
||||
"string-width": "^4.2.3",
|
||||
"y18n": "^5.0.5",
|
||||
"yargs-parser": "^21.1.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
}
|
||||
},
|
||||
"node_modules/yargs-parser": {
|
||||
"version": "21.1.1",
|
||||
"resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-21.1.1.tgz",
|
||||
"integrity": "sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw==",
|
||||
"dev": true,
|
||||
"license": "ISC",
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
[{
|
||||
"order": {
|
||||
"Naam":"",
|
||||
"Datum": "yyyy-mm-dd",
|
||||
"Relatiecode":"",
|
||||
"Referentie":"",
|
||||
"regels": [
|
||||
{
|
||||
"productcode": 0,
|
||||
"omschrijving": "",
|
||||
"aantal": 0,
|
||||
"prijs": 0.00
|
||||
}
|
||||
]
|
||||
}
|
||||
}]
|
||||
@@ -0,0 +1,6 @@
|
||||
# Azure Document Intelligence credentials
|
||||
AZURE_ENDPOINT="https://alvinzilverstand-8432-resource.cognitiveservices.azure.com/"
|
||||
AZURE_API_KEY="YOUR_AZURE_API_KEY"
|
||||
|
||||
# Server port (optional)
|
||||
PORT=3001
|
||||
@@ -0,0 +1,213 @@
|
||||
#!/usr/bin/env node
|
||||
require('dotenv').config();
|
||||
const fs = require('fs').promises;
|
||||
const path = require('path');
|
||||
const yargs = require('yargs/yargs');
|
||||
const { hideBin } = require('yargs/helpers');
|
||||
const { DocumentAnalysisClient, AzureKeyCredential } = require('@azure/ai-form-recognizer');
|
||||
|
||||
const formatDate = (dateStr) => {
|
||||
if (!dateStr) return "yyyy-mm-dd";
|
||||
const date = new Date(dateStr);
|
||||
if (!isNaN(date.getTime())) {
|
||||
return date.toISOString().split('T')[0];
|
||||
}
|
||||
const match = dateStr.match(/(\d{1,2})[-/](\d{1,2})[-/](\d{4})/);
|
||||
if (match) {
|
||||
return `${match[3]}-${match[2].padStart(2, '0')}-${match[1].padStart(2, '0')}`;
|
||||
}
|
||||
return dateStr;
|
||||
};
|
||||
|
||||
const parseNumber = (text, isFloat = false) => {
|
||||
if (!text) return isFloat ? 0.00 : 0;
|
||||
const cleanText = text.replace(/[^\d.,-]/g, '').replace(',', '.');
|
||||
const val = isFloat ? parseFloat(cleanText) : parseInt(cleanText);
|
||||
return isNaN(val) ? (isFloat ? 0.00 : 0) : val;
|
||||
};
|
||||
|
||||
async function analyzeDocument(filePath) {
|
||||
const endpoint = process.env.AZURE_ENDPOINT;
|
||||
const apiKey = process.env.AZURE_API_KEY;
|
||||
|
||||
if (!endpoint || !apiKey) {
|
||||
throw new Error('Azure credentials not configured. Please check your .env file.');
|
||||
}
|
||||
|
||||
const fileBuffer = await fs.readFile(filePath);
|
||||
const client = new DocumentAnalysisClient(endpoint, new AzureKeyCredential(apiKey), {
|
||||
apiVersion: "2023-07-31"
|
||||
});
|
||||
const poller = await client.beginAnalyzeDocument('prebuilt-layout', fileBuffer);
|
||||
const { content, pages, tables, keyValuePairs = [] } = await poller.pollUntilDone();
|
||||
|
||||
const findValue = (keys) => {
|
||||
const kv = keyValuePairs.find(p =>
|
||||
keys.some(k => p.key.content.toLowerCase().includes(k.toLowerCase())) &&
|
||||
p.value && p.value.content.trim() !== ""
|
||||
);
|
||||
if (kv && kv.value) return kv.value.content;
|
||||
return "";
|
||||
};
|
||||
|
||||
// Improved Naam detection
|
||||
let detectedName = findValue(["Naam", "Name", "Klant", "Customer", "Billed to", "Factuur aan"]);
|
||||
|
||||
if (!detectedName) {
|
||||
for (const table of tables) {
|
||||
const billedToCell = table.cells.find(c => c.content.toLowerCase().includes("billed to") || c.content.toLowerCase().includes("factuur aan"));
|
||||
if (billedToCell) {
|
||||
const belowCell = table.cells.find(c => c.rowIndex === billedToCell.rowIndex + 1 && c.columnIndex === billedToCell.columnIndex);
|
||||
if (belowCell && belowCell.content.trim()) {
|
||||
detectedName = belowCell.content.trim();
|
||||
break;
|
||||
}
|
||||
const rightCell = table.cells.find(c => c.rowIndex === billedToCell.rowIndex && c.columnIndex === billedToCell.columnIndex + 1);
|
||||
if (rightCell && rightCell.content.trim()) {
|
||||
detectedName = rightCell.content.trim();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!detectedName && content) {
|
||||
const lines = content.split('\n');
|
||||
const billedToIndex = lines.findIndex(l => l.toLowerCase().includes("billed to") || l.toLowerCase().includes("factuur aan"));
|
||||
if (billedToIndex !== -1 && billedToIndex + 1 < lines.length) {
|
||||
for (let i = billedToIndex + 1; i < Math.min(billedToIndex + 5, lines.length); i++) {
|
||||
const candidate = lines[i].trim();
|
||||
if (candidate && !candidate.toLowerCase().includes("from") && !candidate.toLowerCase().includes("invoice") && !candidate.toLowerCase().includes("purchase") && !candidate.toLowerCase().includes("billed by")) {
|
||||
detectedName = candidate;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (detectedName && detectedName.includes('\n')) {
|
||||
detectedName = detectedName.split('\n')[0].trim();
|
||||
}
|
||||
|
||||
const orderData = {
|
||||
Naam: detectedName,
|
||||
Datum: formatDate(findValue(["Datum", "Date", "Factuurdatum", "Issue Date", "Invoice Date"])),
|
||||
Relatiecode: findValue(["Relatiecode", "Relatie nr", "Klantnummer", "Customer Code", "Customer ID"]),
|
||||
Referentie: findValue(["Referentie", "Reference", "Your reference", "Uw referentie", "Betalingskenmerk", "Kenmerk"]),
|
||||
regels: []
|
||||
};
|
||||
|
||||
// Find the item table
|
||||
let itemTable = null;
|
||||
let maxScore = -1;
|
||||
let columnMap = { productcode: -1, omschrijving: -1, aantal: -1, prijs: -1 };
|
||||
|
||||
for (const table of tables) {
|
||||
let currentColumnMap = { productcode: -1, omschrijving: -1, aantal: -1, prijs: -1 };
|
||||
let score = 0;
|
||||
|
||||
const headerRow = table.cells.filter(c => c.rowIndex === 0);
|
||||
headerRow.forEach(cell => {
|
||||
const text = cell.content.toLowerCase();
|
||||
if (text.includes("description") || text.includes("omschrijving") || text.includes("product") || text.includes("item")) {
|
||||
currentColumnMap.omschrijving = cell.columnIndex;
|
||||
score += 10;
|
||||
} else if (text.includes("code") || text.includes("sku") || text.includes("art")) {
|
||||
currentColumnMap.productcode = cell.columnIndex;
|
||||
score += 5;
|
||||
} else if (text.includes("qty") || text.includes("aantal") || text.includes("quantity") || text.includes("stuks")) {
|
||||
currentColumnMap.aantal = cell.columnIndex;
|
||||
score += 10;
|
||||
} else if (text.includes("price") || text.includes("prijs") || text.includes("unit") || text.includes("rate") || text.includes("cost") || text.includes("kosten")) {
|
||||
currentColumnMap.prijs = cell.columnIndex;
|
||||
score += 10;
|
||||
} else if (text.includes("amount") || text.includes("totaal") || text.includes("subtotal")) {
|
||||
if (currentColumnMap.prijs === -1) currentColumnMap.prijs = cell.columnIndex;
|
||||
score += 5;
|
||||
}
|
||||
});
|
||||
|
||||
if (score > maxScore) {
|
||||
maxScore = score;
|
||||
itemTable = table;
|
||||
columnMap = currentColumnMap;
|
||||
}
|
||||
}
|
||||
|
||||
if (itemTable && maxScore > 5) {
|
||||
for (let r = 1; r < itemTable.rowCount; r++) {
|
||||
const rowCells = itemTable.cells.filter(c => c.rowIndex === r);
|
||||
if (rowCells.length > 0) {
|
||||
const row = {
|
||||
productcode: 0,
|
||||
omschrijving: "",
|
||||
aantal: 0,
|
||||
prijs: 0.00
|
||||
};
|
||||
|
||||
rowCells.forEach(cell => {
|
||||
const content = cell.content.trim();
|
||||
if (cell.columnIndex === columnMap.productcode) {
|
||||
row.productcode = parseNumber(content);
|
||||
} else if (cell.columnIndex === columnMap.omschrijving) {
|
||||
row.omschrijving = content;
|
||||
} else if (cell.columnIndex === columnMap.aantal) {
|
||||
row.aantal = parseNumber(content);
|
||||
} else if (cell.columnIndex === columnMap.prijs) {
|
||||
row.prijs = parseNumber(content, true);
|
||||
}
|
||||
});
|
||||
|
||||
if (row.omschrijving || row.productcode !== 0 || row.prijs !== 0) {
|
||||
orderData.regels.push(row);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (orderData.regels.length === 0) {
|
||||
orderData.regels.push({
|
||||
productcode: 0,
|
||||
omschrijving: "",
|
||||
aantal: 0,
|
||||
prijs: 0.00
|
||||
});
|
||||
}
|
||||
|
||||
const standardizedJson = [{ order: orderData }];
|
||||
return standardizedJson;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const argv = yargs(hideBin(process.argv))
|
||||
.option('file', {
|
||||
alias: 'f',
|
||||
describe: 'Path to the document to analyze',
|
||||
demandOption: true,
|
||||
type: 'string',
|
||||
})
|
||||
.option('output', {
|
||||
alias: 'o',
|
||||
describe: 'Optional: Path to save the JSON output file',
|
||||
type: 'string',
|
||||
})
|
||||
.help()
|
||||
.argv;
|
||||
|
||||
try {
|
||||
console.log(`Analyzing document: ${argv.file}...`);
|
||||
const result = await analyzeDocument(argv.file);
|
||||
|
||||
if (argv.output) {
|
||||
await fs.writeFile(argv.output, JSON.stringify(result, null, 2));
|
||||
console.log(`Successfully saved analysis to ${argv.output}`);
|
||||
} else {
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error:', error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
@@ -0,0 +1,230 @@
|
||||
require('dotenv').config();
|
||||
const express = require('express');
|
||||
const multer = require('multer');
|
||||
const cors = require('cors');
|
||||
const { DocumentAnalysisClient, AzureKeyCredential } = require('@azure/ai-form-recognizer');
|
||||
|
||||
const app = express();
|
||||
const port = process.env.PORT || 3001;
|
||||
|
||||
// Middleware
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
|
||||
// Multer setup for file uploads
|
||||
const upload = multer({ storage: multer.memoryStorage() });
|
||||
|
||||
// Helper functions
|
||||
const formatDate = (dateStr) => {
|
||||
if (!dateStr) return "yyyy-mm-dd";
|
||||
const date = new Date(dateStr);
|
||||
if (!isNaN(date.getTime())) {
|
||||
return date.toISOString().split('T')[0];
|
||||
}
|
||||
const match = dateStr.match(/(\d{1,2})[-/](\d{1,2})[-/](\d{4})/);
|
||||
if (match) {
|
||||
return `${match[3]}-${match[2].padStart(2, '0')}-${match[1].padStart(2, '0')}`;
|
||||
}
|
||||
return dateStr;
|
||||
};
|
||||
|
||||
const parseNumber = (text, isFloat = false) => {
|
||||
if (!text) return isFloat ? 0.00 : 0;
|
||||
const cleanText = text.replace(/[^\d.,-]/g, '').replace(',', '.');
|
||||
const val = isFloat ? parseFloat(cleanText) : parseInt(cleanText);
|
||||
return isNaN(val) ? (isFloat ? 0.00 : 0) : val;
|
||||
};
|
||||
|
||||
// Route for handling file analysis
|
||||
app.post('/api/analyze', upload.single('document'), async (req, res) => {
|
||||
try {
|
||||
if (!req.file) {
|
||||
return res.status(400).json({ error: 'No file uploaded.' });
|
||||
}
|
||||
|
||||
const endpoint = process.env.AZURE_ENDPOINT;
|
||||
const apiKey = process.env.AZURE_API_KEY;
|
||||
|
||||
if (!endpoint || !apiKey) {
|
||||
return res.status(500).json({ error: 'Azure credentials not configured.' });
|
||||
}
|
||||
|
||||
const client = new DocumentAnalysisClient(endpoint, new AzureKeyCredential(apiKey), {
|
||||
apiVersion: "2024-11-30"
|
||||
});
|
||||
|
||||
console.log(`Starting analysis for file: ${req.file.originalname}, mimetype: ${req.file.mimetype}`);
|
||||
|
||||
// Choose the best model based on file type.
|
||||
// Office files (.docx, .xlsx, .pptx) are ONLY supported by 'prebuilt-layout' and 'prebuilt-read' in many regions.
|
||||
// PDF and images are supported by 'prebuilt-document' which provides richer key-value pair extraction.
|
||||
const isOfficeFile = req.file.mimetype.includes('officedocument') ||
|
||||
req.file.originalname.endsWith('.docx') ||
|
||||
req.file.originalname.endsWith('.xlsx') ||
|
||||
req.file.originalname.endsWith('.pptx');
|
||||
|
||||
const modelId = isOfficeFile ? 'prebuilt-layout' : 'prebuilt-document';
|
||||
console.log(`Using model: ${modelId} for ${isOfficeFile ? 'Office file' : 'Standard file'}`);
|
||||
|
||||
const poller = await client.beginAnalyzeDocument(modelId, req.file.buffer, {
|
||||
contentType: req.file.mimetype
|
||||
});
|
||||
const { content, pages, tables, keyValuePairs = [] } = await poller.pollUntilDone();
|
||||
|
||||
const findValue = (keys) => {
|
||||
const kv = keyValuePairs.find(p =>
|
||||
keys.some(k => p.key.content.toLowerCase().includes(k.toLowerCase())) &&
|
||||
p.value && p.value.content.trim() !== ""
|
||||
);
|
||||
if (kv && kv.value) return kv.value.content;
|
||||
return "";
|
||||
};
|
||||
|
||||
// Improved Naam detection
|
||||
let detectedName = findValue(["Naam", "Name", "Klant", "Customer", "Billed to", "Factuur aan"]);
|
||||
|
||||
// Fallback 1: Check if "Billed to" is in a table
|
||||
if (!detectedName) {
|
||||
for (const table of tables) {
|
||||
const billedToCell = table.cells.find(c => c.content.toLowerCase().includes("billed to") || c.content.toLowerCase().includes("factuur aan"));
|
||||
if (billedToCell) {
|
||||
const belowCell = table.cells.find(c => c.rowIndex === billedToCell.rowIndex + 1 && c.columnIndex === billedToCell.columnIndex);
|
||||
if (belowCell && belowCell.content.trim()) {
|
||||
detectedName = belowCell.content.trim();
|
||||
break;
|
||||
}
|
||||
const rightCell = table.cells.find(c => c.rowIndex === billedToCell.rowIndex && c.columnIndex === billedToCell.columnIndex + 1);
|
||||
if (rightCell && rightCell.content.trim()) {
|
||||
detectedName = rightCell.content.trim();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback 2: Search the raw content for "Billed to" and take the next line
|
||||
if (!detectedName && content) {
|
||||
const lines = content.split('\n');
|
||||
const billedToIndex = lines.findIndex(l => l.toLowerCase().includes("billed to") || l.toLowerCase().includes("factuur aan"));
|
||||
if (billedToIndex !== -1 && billedToIndex + 1 < lines.length) {
|
||||
for (let i = billedToIndex + 1; i < Math.min(billedToIndex + 5, lines.length); i++) {
|
||||
const candidate = lines[i].trim();
|
||||
if (candidate && !candidate.toLowerCase().includes("from") && !candidate.toLowerCase().includes("invoice") && !candidate.toLowerCase().includes("purchase") && !candidate.toLowerCase().includes("billed by")) {
|
||||
detectedName = candidate;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up Naam if it contains multiple lines (just take first line as the name)
|
||||
if (detectedName && detectedName.includes('\n')) {
|
||||
detectedName = detectedName.split('\n')[0].trim();
|
||||
}
|
||||
|
||||
const orderData = {
|
||||
Naam: detectedName,
|
||||
Datum: formatDate(findValue(["Datum", "Date", "Factuurdatum", "Issue Date", "Invoice Date"])),
|
||||
Relatiecode: findValue(["Relatiecode", "Relatie nr", "Klantnummer", "Customer Code", "Customer ID"]),
|
||||
Referentie: findValue(["Referentie", "Reference", "Your reference", "Uw referentie", "Betalingskenmerk", "Kenmerk"]),
|
||||
regels: []
|
||||
};
|
||||
|
||||
// Find the item table
|
||||
let itemTable = null;
|
||||
let maxScore = -1;
|
||||
let columnMap = { productcode: -1, omschrijving: -1, aantal: -1, prijs: -1 };
|
||||
|
||||
for (const table of tables) {
|
||||
let currentColumnMap = { productcode: -1, omschrijving: -1, aantal: -1, prijs: -1 };
|
||||
let score = 0;
|
||||
|
||||
const headerRow = table.cells.filter(c => c.rowIndex === 0);
|
||||
headerRow.forEach(cell => {
|
||||
const text = cell.content.toLowerCase();
|
||||
if (text.includes("description") || text.includes("omschrijving") || text.includes("product") || text.includes("item")) {
|
||||
currentColumnMap.omschrijving = cell.columnIndex;
|
||||
score += 10;
|
||||
} else if (text.includes("code") || text.includes("sku") || text.includes("art")) {
|
||||
currentColumnMap.productcode = cell.columnIndex;
|
||||
score += 5;
|
||||
} else if (text.includes("qty") || text.includes("aantal") || text.includes("quantity") || text.includes("stuks")) {
|
||||
currentColumnMap.aantal = cell.columnIndex;
|
||||
score += 10;
|
||||
} else if (text.includes("price") || text.includes("prijs") || text.includes("unit") || text.includes("rate") || text.includes("cost") || text.includes("kosten")) {
|
||||
currentColumnMap.prijs = cell.columnIndex;
|
||||
score += 10;
|
||||
} else if (text.includes("amount") || text.includes("totaal") || text.includes("subtotal")) {
|
||||
if (currentColumnMap.prijs === -1) currentColumnMap.prijs = cell.columnIndex;
|
||||
score += 5;
|
||||
}
|
||||
});
|
||||
|
||||
if (score > maxScore) {
|
||||
maxScore = score;
|
||||
itemTable = table;
|
||||
columnMap = currentColumnMap;
|
||||
}
|
||||
}
|
||||
|
||||
if (itemTable && maxScore > 5) {
|
||||
for (let r = 1; r < itemTable.rowCount; r++) {
|
||||
const rowCells = itemTable.cells.filter(c => c.rowIndex === r);
|
||||
if (rowCells.length > 0) {
|
||||
const row = {
|
||||
productcode: 0,
|
||||
omschrijving: "",
|
||||
aantal: 0,
|
||||
prijs: 0.00
|
||||
};
|
||||
|
||||
rowCells.forEach(cell => {
|
||||
const content = cell.content.trim();
|
||||
if (cell.columnIndex === columnMap.productcode) {
|
||||
row.productcode = parseNumber(content);
|
||||
} else if (cell.columnIndex === columnMap.omschrijving) {
|
||||
row.omschrijving = content;
|
||||
} else if (cell.columnIndex === columnMap.aantal) {
|
||||
row.aantal = parseNumber(content);
|
||||
} else if (cell.columnIndex === columnMap.prijs) {
|
||||
row.prijs = parseNumber(content, true);
|
||||
}
|
||||
});
|
||||
|
||||
if (row.omschrijving || row.productcode !== 0 || row.prijs !== 0) {
|
||||
orderData.regels.push(row);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (orderData.regels.length === 0) {
|
||||
orderData.regels.push({
|
||||
productcode: 0,
|
||||
omschrijving: "",
|
||||
aantal: 0,
|
||||
prijs: 0.00
|
||||
});
|
||||
}
|
||||
|
||||
const standardizedJson = [{ order: orderData }];
|
||||
res.json(standardizedJson);
|
||||
} catch (error) {
|
||||
console.error('--- Analysis Error ---');
|
||||
console.error('Message:', error.message);
|
||||
if (error.details) {
|
||||
console.error('Details:', JSON.stringify(error.details, null, 2));
|
||||
}
|
||||
if (error.response) {
|
||||
console.error('Response Status:', error.response.status);
|
||||
console.error('Response Data:', JSON.stringify(error.response.data, null, 2));
|
||||
}
|
||||
const errorMessage = error.details?.error?.message || error.message || 'Failed to analyze document.';
|
||||
res.status(error.statusCode || 500).json({ error: errorMessage });
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server listening on port ${port}`);
|
||||
});
|
||||
@@ -0,0 +1,67 @@
|
||||
body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
min-height: 100vh;
|
||||
background-color: lightgray;
|
||||
}
|
||||
|
||||
.header-top,
|
||||
.header-bottom {
|
||||
position: fixed;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 60px;
|
||||
background-color: white;
|
||||
z-index: 10;
|
||||
}
|
||||
|
||||
.header-top {
|
||||
top: 0;
|
||||
}
|
||||
|
||||
.header-bottom {
|
||||
bottom: 0;
|
||||
}
|
||||
|
||||
.container {
|
||||
display: flex;
|
||||
gap: 20px;
|
||||
}
|
||||
|
||||
.box {
|
||||
width: 500px;
|
||||
height: 500px;
|
||||
background-color: #7b8083;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.boxinbox {
|
||||
width: 400px;
|
||||
height: 400px;
|
||||
background-color: white;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
text-align: center;
|
||||
font-size: 28px;
|
||||
}
|
||||
|
||||
.action-button {
|
||||
padding: 16px 28px;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
background-color: #c62828;
|
||||
color: white;
|
||||
font-size: 24px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.action-button:hover {
|
||||
background-color: #a71f1f;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Home Page</title>
|
||||
</head>
|
||||
<body>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link rel="stylesheet" href="home.css" />
|
||||
<header class="header-top"></header>
|
||||
<header class="header-bottom"></header>
|
||||
<div class="container">
|
||||
<div class="box">
|
||||
<div class="boxinbox">
|
||||
<button class="action-button" type="button">
|
||||
Drag file to convert
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="box">
|
||||
<div class="boxinbox">
|
||||
<button class="action-button" type="button">Download</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,18 @@
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from openai import OpenAI
|
||||
|
||||
load_dotenv()
|
||||
|
||||
client = OpenAI(
|
||||
# This is the default and can be omitted
|
||||
api_key=os.environ.get("KEY"),
|
||||
)
|
||||
|
||||
response = client.responses.create(
|
||||
model="gpt-5.2",
|
||||
instructions="You are a coding assistant that talks like a pirate.",
|
||||
input="How do I check if a Python object is an instance of a class?",
|
||||
)
|
||||
|
||||
print(response.output_text)
|
||||
|
After Width: | Height: | Size: 2.6 MiB |
|
After Width: | Height: | Size: 492 KiB |
|
After Width: | Height: | Size: 300 KiB |
|
After Width: | Height: | Size: 364 KiB |
|
After Width: | Height: | Size: 316 KiB |
|
After Width: | Height: | Size: 269 KiB |
|
After Width: | Height: | Size: 419 KiB |