MJobe commited on
Commit
c39e604
1 Parent(s): 78eefe7

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +10 -2
main.py CHANGED
@@ -1,6 +1,8 @@
1
  from fastapi import FastAPI, File, UploadFile, Form
2
  from fastapi.responses import JSONResponse
3
  from transformers import pipeline
 
 
4
 
5
  app = FastAPI()
6
 
@@ -27,11 +29,14 @@ async def perform_document_qa(
27
  # Read the uploaded file as bytes
28
  contents = await file.read()
29
 
 
 
 
30
  # Perform document question answering for each question using LayoutLM-based model
31
  answers_dict = {}
32
  for question in questions.split(','):
33
  result = nlp_qa(
34
- contents,
35
  question.strip()
36
  )
37
  answers_dict[question] = result['answer']
@@ -49,11 +54,14 @@ async def load_file(
49
  # Read the uploaded file as bytes
50
  contents = await file.read()
51
 
 
 
 
52
  # Perform document question answering for each question using LayoutLM-based model
53
  answers_dict = {}
54
  for question in questions.split(','):
55
  result = nlp_qa(
56
- contents,
57
  question.strip()
58
  )
59
  answers_dict[question] = result['answer']
 
1
  from fastapi import FastAPI, File, UploadFile, Form
2
  from fastapi.responses import JSONResponse
3
  from transformers import pipeline
4
+ from PIL import Image
5
+ from io import BytesIO
6
 
7
  app = FastAPI()
8
 
 
29
  # Read the uploaded file as bytes
30
  contents = await file.read()
31
 
32
+ # Open the image using PIL
33
+ image = Image.open(BytesIO(contents))
34
+
35
  # Perform document question answering for each question using LayoutLM-based model
36
  answers_dict = {}
37
  for question in questions.split(','):
38
  result = nlp_qa(
39
+ image,
40
  question.strip()
41
  )
42
  answers_dict[question] = result['answer']
 
54
  # Read the uploaded file as bytes
55
  contents = await file.read()
56
 
57
+ # Open the image using PIL
58
+ image = Image.open(BytesIO(contents))
59
+
60
  # Perform document question answering for each question using LayoutLM-based model
61
  answers_dict = {}
62
  for question in questions.split(','):
63
  result = nlp_qa(
64
+ image,
65
  question.strip()
66
  )
67
  answers_dict[question] = result['answer']