setup1
This commit is contained in:
38
apps/Frontend/src/hooks/use-extractPdfData.ts
Normal file
38
apps/Frontend/src/hooks/use-extractPdfData.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { useToast } from "@/hooks/use-toast";
|
||||
import { apiRequest, queryClient } from "@/lib/queryClient";
|
||||
|
||||
export interface ExtractedData {
|
||||
name: string;
|
||||
memberId: string;
|
||||
dob: string;
|
||||
}
|
||||
|
||||
export default function useExtractPdfData() {
|
||||
const { toast } = useToast();
|
||||
|
||||
return useMutation<ExtractedData, Error, File>({
|
||||
mutationFn: async (pdfFile: File) => {
|
||||
const formData = new FormData();
|
||||
formData.append("pdf", pdfFile);
|
||||
|
||||
const res = await apiRequest("POST", "/api/pdfExtraction/", formData);
|
||||
if (!res.ok) throw new Error("Failed to extract PDF");
|
||||
return res.json();
|
||||
},
|
||||
onSuccess: () => {
|
||||
toast({
|
||||
title: "Success",
|
||||
description: "PDF data extracted!",
|
||||
variant: "default",
|
||||
});
|
||||
},
|
||||
onError: (error) => {
|
||||
toast({
|
||||
title: "Error",
|
||||
description: `Failed to extract PDF: ${error.message}`,
|
||||
variant: "destructive",
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -31,6 +31,7 @@ import {
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import useExtractPdfData from "@/hooks/use-extractPdfData";
|
||||
|
||||
const PatientSchema = (
|
||||
PatientUncheckedCreateInputObjectSchema as unknown as z.ZodObject<any>
|
||||
@@ -85,6 +86,9 @@ export default function PatientsPage() {
|
||||
const [uploadedFile, setUploadedFile] = useState<File | null>(null);
|
||||
const [isUploading, setIsUploading] = useState(false);
|
||||
const [isExtracting, setIsExtracting] = useState(false);
|
||||
const [formData, setFormData] = useState({ PatientName: "", PatientMemberId: "", PatientDob:"" });
|
||||
const { mutate: extractPdf, isPending } = useExtractPdfData();
|
||||
|
||||
|
||||
// Fetch patients
|
||||
const {
|
||||
@@ -240,10 +244,7 @@ export default function PatientsPage() {
|
||||
}
|
||||
};
|
||||
|
||||
const isLoading =
|
||||
isLoadingPatients ||
|
||||
addPatientMutation.isPending ||
|
||||
updatePatientMutation.isPending;
|
||||
const isLoading = isLoadingPatients || addPatientMutation.isPending || updatePatientMutation.isPending;
|
||||
|
||||
// Search handling
|
||||
const handleSearch = (criteria: SearchCriteria) => {
|
||||
@@ -254,18 +255,6 @@ export default function PatientsPage() {
|
||||
setSearchCriteria(null);
|
||||
};
|
||||
|
||||
// File upload handling
|
||||
const handleFileUpload = (file: File) => {
|
||||
setUploadedFile(file);
|
||||
setIsUploading(false); // In a real implementation, this would be set to true during upload
|
||||
|
||||
toast({
|
||||
title: "File Selected",
|
||||
description: `${file.name} is ready for processing.`,
|
||||
variant: "default",
|
||||
});
|
||||
};
|
||||
|
||||
// Filter patients based on search criteria
|
||||
const filteredPatients = useMemo(() => {
|
||||
if (!searchCriteria || !searchCriteria.searchTerm) {
|
||||
@@ -302,6 +291,30 @@ export default function PatientsPage() {
|
||||
});
|
||||
}, [patients, searchCriteria]);
|
||||
|
||||
|
||||
// File upload handling
|
||||
const handleFileUpload = (file: File) => {
|
||||
setUploadedFile(file);
|
||||
setIsUploading(false); // In a real implementation, this would be set to true during upload
|
||||
|
||||
toast({
|
||||
title: "File Selected",
|
||||
description: `${file.name} is ready for processing.`,
|
||||
variant: "default",
|
||||
});
|
||||
};
|
||||
|
||||
// data extraction
|
||||
const handleExtract = () => {
|
||||
if (!uploadedFile) return alert("Please upload a PDF.");
|
||||
extractPdf(uploadedFile, {
|
||||
onSuccess: (data) => {
|
||||
setFormData({ PatientName: data.name || "", PatientMemberId: data.memberId || "", PatientDob: data.dob || ""});
|
||||
},
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="flex h-screen overflow-hidden bg-gray-100">
|
||||
<Sidebar
|
||||
@@ -369,6 +382,7 @@ export default function PatientsPage() {
|
||||
<Button
|
||||
className="w-full h-12 gap-2"
|
||||
disabled={!uploadedFile || isExtracting}
|
||||
onClick={handleExtract}
|
||||
>
|
||||
{isExtracting ? (
|
||||
<>
|
||||
|
||||
23
apps/PdfService/main.py
Normal file
23
apps/PdfService/main.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from flask import Flask, request, jsonify
|
||||
import fitz # PyMuPDF
|
||||
import re
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.route("/extract", methods=["POST"])
|
||||
def extract():
|
||||
file = request.files['pdf']
|
||||
doc = fitz.open(stream=file.read(), filetype="pdf")
|
||||
text = "".join(page.get_text() for page in doc)
|
||||
|
||||
name = re.search(r"Name:\s*(.*)", text)
|
||||
email = re.search(r"Email:\s*(.*)", text)
|
||||
|
||||
return jsonify({
|
||||
"text": text,
|
||||
"name": name.group(1).strip() if name else "",
|
||||
"email": email.group(1).strip() if email else ""
|
||||
})
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(port=5001)
|
||||
Reference in New Issue
Block a user