Skip to content

Commit

Permalink
Extract report data from the PDF document using AI
Browse files Browse the repository at this point in the history
  • Loading branch information
jrivals committed Sep 24, 2024
1 parent 89550f3 commit 2640ef8
Show file tree
Hide file tree
Showing 8 changed files with 354 additions and 4 deletions.
12 changes: 12 additions & 0 deletions frontend/src/services/analysis.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {
PartialAnalysis,
} from 'shared/schema/Analysis/Analysis';
import { api } from 'src/services/api.service';
import { authParams } from 'src/services/auth-headers';
import config from 'src/utils/config';
import { getURLQuery } from 'src/utils/fetchUtils';

export const analysisApi = api.injectEndpoints({
endpoints: (builder) => ({
Expand Down Expand Up @@ -47,10 +50,19 @@ export const analysisApi = api.injectEndpoints({
}),
});

const analysisExtractURL = (analysis: PartialAnalysis) => {
const params = getURLQuery({
...authParams(),
});
return `${config.apiEndpoint}/api/analysis/${analysis.id}/extract${params}`;
};

export const {
useGetSampleAnalysisQuery,
useCreateAnalysisMutation,
useUpdateAnalysisMutation,
getAnalysisExtractURL,
} = {
...analysisApi,
getAnalysisExtractURL: analysisExtractURL,
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import Button from '@codegouvfr/react-dsfr/Button';
import { cx } from '@codegouvfr/react-dsfr/fr/cx';
import Stepper from '@codegouvfr/react-dsfr/Stepper';
import { useEffect, useState } from 'react';
import { useSearchParams } from 'react-router-dom';
import { PartialAnalysis } from 'shared/schema/Analysis/Analysis';
import { AnalysisStatus } from 'shared/schema/Analysis/AnalysisStatus';
import { Sample } from 'shared/schema/Sample/Sample';
import { useGetSampleAnalysisQuery } from 'src/services/analysis.service';
import {
getAnalysisExtractURL,
useGetSampleAnalysisQuery,
} from 'src/services/analysis.service';
import AnalysisComplianceStep from 'src/views/SampleView/SampleAnalysis/SampleDraftAnalysis/AnalysisComplianceStep/AnalysisComplianceStep';
import AnalysisReportStep from 'src/views/SampleView/SampleAnalysis/SampleDraftAnalysis/AnalysisReportStep/AnalysisReportStep';
import AnalysisResiduesStep from 'src/views/SampleView/SampleAnalysis/SampleDraftAnalysis/AnalysisResiduesStep/AnalysisResiduesStep';
Expand Down Expand Up @@ -46,6 +50,17 @@ const SampleDraftAnalysis = ({ sample }: Props) => {

return (
<>
{partialAnalysis?.id && (
<Button
className={cx('fr-mr-2w')}
linkProps={{
to: getAnalysisExtractURL(partialAnalysis as PartialAnalysis),
target: '_blank',
}}
>
Test extraction
</Button>
)}
<h4 className={cx('fr-mb-0')}>
Saisie des résultats d’analyse
<div className={cx('fr-text--md', 'fr-text--regular')}>
Expand Down
154 changes: 151 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
"lodash": "^4.17.21",
"node-fetch": "^2.6.7",
"nodemailer": "^6.9.13",
"openai": "^4.61.1",
"pdf-parse": "^1.1.1",
"pg": "^8.7.3",
"puppeteer": "^22.11.2",
"randomstring": "^1.2.1",
Expand All @@ -82,6 +84,7 @@
"@types/node": "^20.11.30",
"@types/node-fetch": "^2.5.12",
"@types/nodemailer": "^6.4.15",
"@types/pdf-parse": "^1.1.4",
"@types/randomstring": "^1.1.8",
"@types/supertest": "^2.0.12",
"@types/uuid": "^8.3.4",
Expand Down
15 changes: 15 additions & 0 deletions server/controllers/analysisController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '../../shared/schema/Analysis/Analysis';
import analysisRepository from '../repositories/analysisRepository';
import sampleRepository from '../repositories/sampleRepository';
import { extractFromReport } from '../services/analysisService/AnalysisService';

const getAnalysis = async (request: Request, response: Response) => {
const { sampleId } = request.query as { sampleId: string };
Expand Down Expand Up @@ -86,8 +87,22 @@ const updateAnalysis = async (request: Request, response: Response) => {
response.send(updatedAnalysis);
};

const extractAnalysis = async (request: Request, response: Response) => {
const { analysisId } = request.params;
const analysis = await analysisRepository.findUnique(analysisId);

if (!analysis) {
return response.sendStatus(constants.HTTP_STATUS_NOT_FOUND);
}

const extraction = await extractFromReport(analysis.reportDocumentId);

response.send(extraction);
};

export default {
getAnalysis,
createAnalysis,
updateAnalysis,
extractAnalysis,
};
2 changes: 2 additions & 0 deletions server/routers/analysis.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ router.put(
analysisController.updateAnalysis
);

router.get('/:analysisId/extract', analysisController.extractAnalysis);

export default router;
Loading

0 comments on commit 2640ef8

Please sign in to comment.