Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,10 +1,11 @@
|
|
1 |
-
|
2 |
-
|
|
|
|
|
|
|
3 |
from fastapi import FastAPI, File, UploadFile, Form
|
4 |
from fastapi.responses import JSONResponse
|
5 |
from transformers import pipeline
|
6 |
-
from pytesseract import pytesseract
|
7 |
-
import base64
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
@@ -13,7 +14,7 @@ nlp_qa = pipeline("document-question-answering", model="impira/layoutlm-document
|
|
13 |
|
14 |
description = """
|
15 |
## Image-based Document QA
|
16 |
-
This API
|
17 |
|
18 |
### Endpoints:
|
19 |
- **POST /uploadfile/:** Upload an image file to extract text and answer provided questions.
|
@@ -22,12 +23,8 @@ This API extracts text from an uploaded image using OCR and performs document qu
|
|
22 |
|
23 |
app = FastAPI(docs_url="/", description=description)
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
image = Image.open(BytesIO(contents))
|
28 |
-
# Perform OCR to extract text from the image
|
29 |
-
text_content = pytesseract.image_to_string(image)
|
30 |
-
return text_content
|
31 |
|
32 |
@app.post("/uploadfile/", description=description)
|
33 |
async def perform_document_qa(
|
@@ -35,20 +32,15 @@ async def perform_document_qa(
|
|
35 |
questions: str = Form(...),
|
36 |
):
|
37 |
try:
|
38 |
-
# Read the uploaded file
|
39 |
contents = await file.read()
|
40 |
|
41 |
-
text_content = get_image_content(contents)
|
42 |
-
|
43 |
-
# Split the questions string into a list
|
44 |
-
question_list = [q.strip() for q in questions.split(',')]
|
45 |
-
|
46 |
# Perform document question answering for each question using LayoutLM-based model
|
47 |
answers_dict = {}
|
48 |
-
for question in
|
49 |
result = nlp_qa(
|
50 |
-
|
51 |
-
question
|
52 |
)
|
53 |
answers_dict[question] = result['answer']
|
54 |
|
|
|
1 |
+
import os
|
2 |
+
import shutil
|
3 |
+
import requests
|
4 |
+
from tempfile import NamedTemporaryFile
|
5 |
+
|
6 |
from fastapi import FastAPI, File, UploadFile, Form
|
7 |
from fastapi.responses import JSONResponse
|
8 |
from transformers import pipeline
|
|
|
|
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
|
|
14 |
|
15 |
description = """
|
16 |
## Image-based Document QA
|
17 |
+
This API performs document question answering using a LayoutLM-based model.
|
18 |
|
19 |
### Endpoints:
|
20 |
- **POST /uploadfile/:** Upload an image file to extract text and answer provided questions.
|
|
|
23 |
|
24 |
app = FastAPI(docs_url="/", description=description)
|
25 |
|
26 |
+
# Define a temporary folder to store downloaded files
|
27 |
+
TEMP_FOLDER = "/path/to/temp/folder" # Replace with the actual path
|
|
|
|
|
|
|
|
|
28 |
|
29 |
@app.post("/uploadfile/", description=description)
|
30 |
async def perform_document_qa(
|
|
|
32 |
questions: str = Form(...),
|
33 |
):
|
34 |
try:
|
35 |
+
# Read the uploaded file as bytes
|
36 |
contents = await file.read()
|
37 |
|
|
|
|
|
|
|
|
|
|
|
38 |
# Perform document question answering for each question using LayoutLM-based model
|
39 |
answers_dict = {}
|
40 |
+
for question in questions.split(','):
|
41 |
result = nlp_qa(
|
42 |
+
contents.decode('utf-8'), # Assuming the content is text, adjust as needed
|
43 |
+
question.strip()
|
44 |
)
|
45 |
answers_dict[question] = result['answer']
|
46 |
|