Merge pull request #710 from gradio-app/json-fix

Switched from `json` to `orjson`
This commit is contained in:
Abubakar Abid 2022-02-22 15:56:56 -05:00 committed by GitHub
commit 93f372d988
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 5 deletions

View File

@ -5,6 +5,7 @@ ffmpy
markdown-it-py[linkify,plugins] markdown-it-py[linkify,plugins]
matplotlib matplotlib
numpy numpy
orjson
pandas pandas
paramiko paramiko
pillow pillow

View File

@ -9,8 +9,9 @@ import posixpath
import secrets import secrets
import traceback import traceback
import urllib import urllib
from typing import List, Optional, Type from typing import Any, List, Optional, Type
import orjson
import pkg_resources import pkg_resources
import uvicorn import uvicorn
from fastapi import Depends, FastAPI, HTTPException, Request, status from fastapi import Depends, FastAPI, HTTPException, Request, status
@ -19,6 +20,7 @@ from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
from fastapi.security import OAuth2PasswordRequestForm from fastapi.security import OAuth2PasswordRequestForm
from fastapi.templating import Jinja2Templates from fastapi.templating import Jinja2Templates
from jinja2.exceptions import TemplateNotFound
from starlette.responses import RedirectResponse from starlette.responses import RedirectResponse
from gradio import encryptor, queueing, utils from gradio import encryptor, queueing, utils
@ -37,7 +39,15 @@ GRADIO_BUILD_ROOT = "https://gradio.s3-us-west-2.amazonaws.com/{}/assets/".forma
VERSION VERSION
) )
app = FastAPI()
class ORJSONResponse(JSONResponse):
media_type = "application/json"
def render(self, content: Any) -> bytes:
return orjson.dumps(content)
app = FastAPI(default_response_class=ORJSONResponse)
app.add_middleware( app.add_middleware(
CORSMiddleware, CORSMiddleware,
allow_origins=["*"], allow_origins=["*"],
@ -108,9 +118,15 @@ def main(request: Request, user: str = Depends(get_current_user)):
else: else:
config = {"auth_required": True, "auth_message": app.interface.auth_message} config = {"auth_required": True, "auth_message": app.interface.auth_message}
try:
return templates.TemplateResponse( return templates.TemplateResponse(
"frontend/index.html", {"request": request, "config": config} "frontend/index.html", {"request": request, "config": config}
) )
except TemplateNotFound:
raise ValueError(
"Did you install Gradio from source files? You need to build "
"the frontend by running /scripts/build_frontend.sh"
)
@app.get("/config/", dependencies=[Depends(login_check)]) @app.get("/config/", dependencies=[Depends(login_check)])

View File

@ -22,6 +22,7 @@ setup(
"markdown-it-py[linkify,plugins]", "markdown-it-py[linkify,plugins]",
"matplotlib", "matplotlib",
"numpy", "numpy",
"orjson",
"pandas", "pandas",
"paramiko", "paramiko",
"pillow", "pillow",