MJobe commited on
Commit
af17670
1 Parent(s): d505ac6

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +30 -17
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.cors import CORSMiddleware # Turns out it doesn't matter where you import this from
8
- from .my_router import router
9
-
10
-
11
- def create_app() -> CORSMiddleware:
12
- fastapi_app = FastAPI()
13
- fastapi_app.include_router(router)
14
- return CORSMiddleware(
15
- fastapi_app,
16
- allow_origins=["*"],
17
- allow_credentials=True,
18
- allow_methods=["*"],
19
- allow_headers=["*"],
20
- )
21
-
22
-
23
- app = create_app()
 
 
 
 
 
 
 
 
 
 
 
 
 
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")