MJobe commited on
Commit
d3f8141
1 Parent(s): 2181fee

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +13 -19
main.py CHANGED
@@ -1,23 +1,17 @@
1
  from io import BytesIO
2
-
3
  from PIL import Image
4
  from fastapi import FastAPI, File, UploadFile, Form
5
  from fastapi.responses import JSONResponse
6
- import fitz
7
  from transformers import pipeline
8
- import requests
9
- from typing import List
10
- from pytesseract import pytesseract
11
-
12
 
13
  app = FastAPI()
14
 
15
- # Load a BERT-based question answering pipeline
16
- nlp_qa = pipeline('question-answering', model='bert-large-uncased-whole-word-masking-finetuned-squad')
17
 
18
  description = """
19
  ## Image-based Document QA
20
- This API extracts text from an uploaded image using OCR and performs document question answering using a BERT-based model.
21
 
22
  ### Endpoints:
23
  - **POST /uploadfile/:** Upload an image file to extract text and answer provided questions.
@@ -44,13 +38,13 @@ async def perform_document_qa(
44
  # Split the questions string into a list
45
  question_list = [q.strip() for q in questions.split(',')]
46
 
47
- # Perform document question answering for each question using BERT-based model
48
  answers_dict = {}
49
  for question in question_list:
50
- result = nlp_qa({
51
- 'question': question,
52
- 'context': text_content
53
- })
54
  answers_dict[question] = result['answer']
55
 
56
  return answers_dict
@@ -66,13 +60,13 @@ async def load_file(
66
  # Read the uploaded file as bytes
67
  contents = await file.read()
68
 
69
- # Perform document question answering for each question using BERT-based model
70
  answers_dict = {}
71
  for question in questions.split(','):
72
- result = nlp_qa({
73
- 'question': question.strip(),
74
- 'context': contents.decode('utf-8') # Assuming the content is text, adjust as needed
75
- })
76
  answers_dict[question] = result['answer']
77
 
78
  return answers_dict
 
1
  from io import BytesIO
 
2
  from PIL import Image
3
  from fastapi import FastAPI, File, UploadFile, Form
4
  from fastapi.responses import JSONResponse
 
5
  from transformers import pipeline
 
 
 
 
6
 
7
  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
14
+ This API extracts text from an uploaded image using OCR and performs document question answering using a LayoutLM-based model.
15
 
16
  ### Endpoints:
17
  - **POST /uploadfile/:** Upload an image file to extract text and answer provided questions.
 
38
  # Split the questions string into a list
39
  question_list = [q.strip() for q in questions.split(',')]
40
 
41
+ # Perform document question answering for each question using LayoutLM-based model
42
  answers_dict = {}
43
  for question in question_list:
44
+ result = nlp_qa(
45
+ text_content,
46
+ question
47
+ )
48
  answers_dict[question] = result['answer']
49
 
50
  return answers_dict
 
60
  # Read the uploaded file as bytes
61
  contents = await file.read()
62
 
63
+ # Perform document question answering for each question using LayoutLM-based model
64
  answers_dict = {}
65
  for question in questions.split(','):
66
+ result = nlp_qa(
67
+ contents.decode('utf-8'), # Assuming the content is text, adjust as needed
68
+ question.strip()
69
+ )
70
  answers_dict[question] = result['answer']
71
 
72
  return answers_dict