MJobe commited on
Commit
8700a34
1 Parent(s): f198fb3

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +16 -11
main.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from fastapi import FastAPI, File, UploadFile, Form
2
  from fastapi.responses import JSONResponse
3
  from transformers import pipeline
@@ -8,6 +9,8 @@ app = FastAPI()
8
 
9
  # Use a pipeline as a high-level helper
10
  nlp_qa = pipeline("document-question-answering", model="impira/layoutlm-document-qa")
 
 
11
 
12
  description = """
13
  ## Image-based Document QA
@@ -52,6 +55,7 @@ async def perform_document_qa(
52
  except Exception as e:
53
  return JSONResponse(content=f"Error processing file: {str(e)}", status_code=500)
54
 
 
55
  @app.post("/pdfUpload/", description=description)
56
  async def load_file(
57
  file: UploadFile = File(...),
@@ -61,25 +65,26 @@ async def load_file(
61
  # Read the uploaded file as bytes
62
  contents = await file.read()
63
 
64
- # Open the image using PIL
65
- image = Image.open(BytesIO(contents))
 
 
 
 
66
 
67
- # Perform document question answering for each question using LayoutLM-based model
68
  answers_dict = {}
69
  for question in questions.split(','):
70
- result = nlp_qa(
71
- image,
72
- question.strip()
73
- )
74
 
75
- # Access the 'answer' key from the first item in the result list
76
- answer = result[0]['answer']
77
 
78
  # Format the question as a string without extra characters
79
  formatted_question = question.strip("[]")
80
 
81
- answers_dict[formatted_question] = answer
82
 
83
  return answers_dict
84
  except Exception as e:
85
- return JSONResponse(content=f"Error processing file: {str(e)}", status_code=500)
 
1
+ import fitz
2
  from fastapi import FastAPI, File, UploadFile, Form
3
  from fastapi.responses import JSONResponse
4
  from transformers import pipeline
 
9
 
10
  # Use a pipeline as a high-level helper
11
  nlp_qa = pipeline("document-question-answering", model="impira/layoutlm-document-qa")
12
+ # Use a pipeline as a high-level helper for NER
13
+ nlp_ner = pipeline("ner", model="microsoft/layoutlm-base-ner")
14
 
15
  description = """
16
  ## Image-based Document QA
 
55
  except Exception as e:
56
  return JSONResponse(content=f"Error processing file: {str(e)}", status_code=500)
57
 
58
+
59
  @app.post("/pdfUpload/", description=description)
60
  async def load_file(
61
  file: UploadFile = File(...),
 
65
  # Read the uploaded file as bytes
66
  contents = await file.read()
67
 
68
+ # Extract text from the PDF using PyMuPDF (fitz)
69
+ pdf_document = fitz.open("file.pdf", pdf_bytes=contents)
70
+ text_content = ""
71
+ for page_num in range(pdf_document.page_count):
72
+ page = pdf_document[page_num]
73
+ text_content += page.get_text()
74
 
75
+ # Perform named entity recognition for each question using LayoutLM-based NER
76
  answers_dict = {}
77
  for question in questions.split(','):
78
+ result = nlp_ner(text_content, question.strip())
 
 
 
79
 
80
+ # Extract the named entity from the result
81
+ named_entity = result[0]['word'] if result else "Not Found"
82
 
83
  # Format the question as a string without extra characters
84
  formatted_question = question.strip("[]")
85
 
86
+ answers_dict[formatted_question] = named_entity
87
 
88
  return answers_dict
89
  except Exception as e:
90
+ return JSONResponse(content=f"Error processing PDF file: {str(e)}", status_code=500)