Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -3,20 +3,23 @@ from io import BytesIO
|
|
3 |
from PIL import Image
|
4 |
from fastapi import FastAPI, File, UploadFile, Form
|
5 |
from fastapi.responses import JSONResponse
|
6 |
-
|
7 |
from transformers import pipeline
|
|
|
|
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
11 |
# Load a BERT-based question answering pipeline
|
12 |
-
|
13 |
|
14 |
description = """
|
15 |
## Image-based Document QA
|
16 |
This API extracts text from an uploaded image using OCR and performs document question answering using a BERT-based model.
|
17 |
|
18 |
-
###
|
19 |
- **POST /uploadfile/:** Upload an image file to extract text and answer provided questions.
|
|
|
20 |
"""
|
21 |
|
22 |
app = FastAPI(docs_url="/", description=description)
|
@@ -42,7 +45,35 @@ async def perform_document_qa(
|
|
42 |
# Perform document question answering for each question using BERT-based model
|
43 |
answers_dict = {}
|
44 |
for question in question_list:
|
45 |
-
result =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
'question': question,
|
47 |
'context': text_content
|
48 |
})
|
|
|
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 |
|
11 |
app = FastAPI()
|
12 |
|
13 |
# Load a BERT-based question answering pipeline
|
14 |
+
nlp_qa = pipeline('question-answering', model='bert-large-uncased-whole-word-masking-finetuned-squad')
|
15 |
|
16 |
description = """
|
17 |
## Image-based Document QA
|
18 |
This API extracts text from an uploaded image using OCR and 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.
|
22 |
+
- **POST /pdfUpload/:** Provide a file to extract text and answer provided questions.
|
23 |
"""
|
24 |
|
25 |
app = FastAPI(docs_url="/", description=description)
|
|
|
45 |
# Perform document question answering for each question using BERT-based model
|
46 |
answers_dict = {}
|
47 |
for question in question_list:
|
48 |
+
result = nlp_qa({
|
49 |
+
'question': question,
|
50 |
+
'context': text_content
|
51 |
+
})
|
52 |
+
answers_dict[question] = result['answer']
|
53 |
+
|
54 |
+
return answers_dict
|
55 |
+
except Exception as e:
|
56 |
+
return JSONResponse(content=f"Error processing file: {str(e)}", status_code=500)
|
57 |
+
|
58 |
+
@app.post("/pdfUpload/", description=description)
|
59 |
+
async def load_file(
|
60 |
+
file: UploadFile = File(...),
|
61 |
+
questions: str = Form(...),
|
62 |
+
):
|
63 |
+
try:
|
64 |
+
# Read the uploaded file
|
65 |
+
contents = await file.read()
|
66 |
+
|
67 |
+
# Convert binary content to text
|
68 |
+
text_content = contents.decode('utf-8')
|
69 |
+
|
70 |
+
# Split the questions string into a list
|
71 |
+
question_list = [q.strip() for q in questions.split(',')]
|
72 |
+
|
73 |
+
# Perform document question answering for each question using BERT-based model
|
74 |
+
answers_dict = {}
|
75 |
+
for question in question_list:
|
76 |
+
result = nlp_qa({
|
77 |
'question': question,
|
78 |
'context': text_content
|
79 |
})
|