Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,19 +1,29 @@
|
|
1 |
import fitz
|
2 |
from fastapi import FastAPI, File, UploadFile, Form
|
3 |
from fastapi.responses import JSONResponse
|
|
|
4 |
from PIL import Image
|
5 |
from io import BytesIO
|
6 |
from starlette.middleware import Middleware
|
7 |
from starlette.middleware.cors import CORSMiddleware
|
8 |
-
from transformers import pipeline, DistilBertTokenizer, DistilBertForQuestionAnswering
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
nlp_qa = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
13 |
|
14 |
description = """
|
15 |
## Image-based Document QA
|
16 |
-
This API performs document question answering using a
|
17 |
|
18 |
### Endpoints:
|
19 |
- **POST /uploadfile/:** Upload an image file to extract text and answer provided questions.
|
@@ -34,16 +44,16 @@ async def perform_document_qa(
|
|
34 |
# Open the image using PIL
|
35 |
image = Image.open(BytesIO(contents))
|
36 |
|
37 |
-
# Perform document question answering for each question using
|
38 |
answers_dict = {}
|
39 |
for question in questions.split(','):
|
40 |
result = nlp_qa(
|
41 |
-
|
42 |
-
|
43 |
)
|
44 |
|
45 |
-
# Access the 'answer' key from the result
|
46 |
-
answer = result['answer']
|
47 |
|
48 |
# Format the question as a string without extra characters
|
49 |
formatted_question = question.strip("[]")
|
|
|
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 |
from starlette.middleware import Middleware
|
8 |
from starlette.middleware.cors import CORSMiddleware
|
|
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
12 |
+
# Set up CORS middleware
|
13 |
+
origins = ["*"] # or specify your list of allowed origins
|
14 |
+
app.add_middleware(
|
15 |
+
CORSMiddleware,
|
16 |
+
allow_origins=origins,
|
17 |
+
allow_credentials=True,
|
18 |
+
allow_methods=["*"],
|
19 |
+
allow_headers=["*"],
|
20 |
+
)
|
21 |
+
|
22 |
nlp_qa = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
23 |
|
24 |
description = """
|
25 |
## Image-based Document QA
|
26 |
+
This API performs document question answering using a LayoutLMv2-based model.
|
27 |
|
28 |
### Endpoints:
|
29 |
- **POST /uploadfile/:** Upload an image file to extract text and answer provided questions.
|
|
|
44 |
# Open the image using PIL
|
45 |
image = Image.open(BytesIO(contents))
|
46 |
|
47 |
+
# Perform document question answering for each question using LayoutLMv2-based model
|
48 |
answers_dict = {}
|
49 |
for question in questions.split(','):
|
50 |
result = nlp_qa(
|
51 |
+
image,
|
52 |
+
question.strip()
|
53 |
)
|
54 |
|
55 |
+
# Access the 'answer' key from the first item in the result list
|
56 |
+
answer = result[0]['answer']
|
57 |
|
58 |
# Format the question as a string without extra characters
|
59 |
formatted_question = question.strip("[]")
|