Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -4,23 +4,36 @@ from fastapi.responses import JSONResponse
|
|
4 |
from transformers import pipeline
|
5 |
from PIL import Image
|
6 |
from io import BytesIO
|
7 |
-
from starlette.middleware
|
8 |
-
from .
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
# Use a pipeline as a high-level helper
|
26 |
nlp_qa = pipeline("document-question-answering", model="impira/layoutlm-document-qa")
|
|
|
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 |
+
# Salt to your taste
|
13 |
+
ALLOWED_ORIGINS = [
|
14 |
+
"http://localhost",
|
15 |
+
"http://localhost:8080",
|
16 |
+
"http://127.0.0.1:5500",
|
17 |
+
"http://127.0.0.1:5500/DataExtractAI.html",
|
18 |
+
]
|
19 |
+
|
20 |
+
# Handle CORS preflight requests
|
21 |
+
@app.options('/{rest_of_path:path}')
|
22 |
+
async def preflight_handler(request: Request, rest_of_path: str) -> Response:
|
23 |
+
response = Response()
|
24 |
+
response.headers['Access-Control-Allow-Origin'] = ', '.join(ALLOWED_ORIGINS)
|
25 |
+
response.headers['Access-Control-Allow-Methods'] = 'POST, GET, DELETE, OPTIONS'
|
26 |
+
response.headers['Access-Control-Allow-Headers'] = 'Authorization, Content-Type'
|
27 |
+
return response
|
28 |
+
|
29 |
+
# Set CORS headers
|
30 |
+
@app.middleware("http")
|
31 |
+
async def add_CORS_header(request: Request, call_next):
|
32 |
+
response = await call_next(request)
|
33 |
+
response.headers['Access-Control-Allow-Origin'] = ', '.join(ALLOWED_ORIGINS)
|
34 |
+
response.headers['Access-Control-Allow-Methods'] = 'POST, GET, DELETE, OPTIONS'
|
35 |
+
response.headers['Access-Control-Allow-Headers'] = 'Authorization, Content-Type'
|
36 |
+
return response
|
37 |
|
38 |
# Use a pipeline as a high-level helper
|
39 |
nlp_qa = pipeline("document-question-answering", model="impira/layoutlm-document-qa")
|