MJobe commited on
Commit
836458e
1 Parent(s): 1fb76bb

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +14 -21
main.py CHANGED
@@ -1,21 +1,23 @@
1
  import fitz
2
- from fastapi import FastAPI, File, UploadFile, Form
3
  from fastapi.responses import JSONResponse
4
  from transformers import pipeline
5
  from PIL import Image
6
  from io import BytesIO
7
- import pytesseract
8
  from starlette.middleware import Middleware
9
  from starlette.middleware.cors import CORSMiddleware
10
 
11
  app = FastAPI()
12
 
13
- # Use a pipeline as a high-level helper for question answering
14
- nlp_qa = pipeline('question-answering', model='bert-large-uncased-whole-word-masking-finetuned-squad', tokenizer='bert-large-uncased-whole-word-masking-finetuned-squad')
 
 
 
15
 
16
  description = """
17
  ## Image-based Document QA
18
- This API performs document question answering using a BERT-based model.
19
 
20
  ### Endpoints:
21
  - **POST /uploadfile/:** Upload an image file to extract text and answer provided questions.
@@ -36,19 +38,16 @@ async def perform_document_qa(
36
  # Open the image using PIL
37
  image = Image.open(BytesIO(contents))
38
 
39
- # Perform OCR to extract text from the image
40
- text = extract_text_from_image(image)
41
-
42
- # Perform document question answering for each question using BERT-based model
43
  answers_dict = {}
44
  for question in questions.split(','):
45
- result = nlp_qa({
46
- 'question': question.strip(),
47
- 'context': text
48
- })
49
 
50
- # Access the 'answer' key from the result
51
- answer = result['answer']
52
 
53
  # Format the question as a string without extra characters
54
  formatted_question = question.strip("[]")
@@ -59,12 +58,6 @@ async def perform_document_qa(
59
  except Exception as e:
60
  return JSONResponse(content=f"Error processing file: {str(e)}", status_code=500)
61
 
62
- def extract_text_from_image(image):
63
- # Perform OCR to extract text from the image using Tesseract
64
- text = pytesseract.image_to_string(image, lang='eng')
65
-
66
- return text
67
-
68
  @app.post("/pdfQA/", description=description)
69
  async def pdf_question_answering(
70
  file: UploadFile = File(...),
 
1
  import fitz
2
+ from fastapi import FastAPI, File, UploadFile, Form, Request, Response
3
  from fastapi.responses import JSONResponse
4
  from transformers import pipeline
5
  from PIL import Image
6
  from io import BytesIO
 
7
  from starlette.middleware import Middleware
8
  from starlette.middleware.cors import CORSMiddleware
9
 
10
  app = FastAPI()
11
 
12
+ # Use a pipeline as a high-level helper
13
+ nlp_qa = pipeline("document-question-answering", model="impira/layoutlm-invoices")
14
+ # Use a pipeline as a high-level helper
15
+ nlp_ner = pipeline('question-answering', model='deepset/roberta-base-squad2', tokenizer='deepset/roberta-base-squad2')
16
+
17
 
18
  description = """
19
  ## Image-based Document QA
20
+ This API performs document question answering using a LayoutLM-based model.
21
 
22
  ### Endpoints:
23
  - **POST /uploadfile/:** Upload an image file to extract text and answer provided questions.
 
38
  # Open the image using PIL
39
  image = Image.open(BytesIO(contents))
40
 
41
+ # Perform document question answering for each question using LayoutLM-based model
 
 
 
42
  answers_dict = {}
43
  for question in questions.split(','):
44
+ result = nlp_qa(
45
+ image,
46
+ question.strip()
47
+ )
48
 
49
+ # Access the 'answer' key from the first item in the result list
50
+ answer = result[0]['answer']
51
 
52
  # Format the question as a string without extra characters
53
  formatted_question = question.strip("[]")
 
58
  except Exception as e:
59
  return JSONResponse(content=f"Error processing file: {str(e)}", status_code=500)
60
 
 
 
 
 
 
 
61
  @app.post("/pdfQA/", description=description)
62
  async def pdf_question_answering(
63
  file: UploadFile = File(...),