Merge branch 'starlette-migration' of http://github.com/gradio-app/gradio into starlette-migration

This commit is contained in:
Ali Abid 2022-01-13 22:13:11 -08:00
commit df8e1c1973
3 changed files with 24 additions and 7 deletions

View File

@ -117,6 +117,14 @@ def static_resource(path: str):
raise HTTPException(status_code=404, detail="Static file not found") raise HTTPException(status_code=404, detail="Static file not found")
@app.get('/favicon.ico')
async def favicon():
if app.favicon_path:
return FileResponse(app.favicon_path)
else:
raise HTTPException(status_code=404, detail="Favicon file not found")
@app.get("/api", response_class=HTMLResponse) # Needed for Spaces @app.get("/api", response_class=HTMLResponse) # Needed for Spaces
@app.get("/api/", response_class=HTMLResponse) @app.get("/api/", response_class=HTMLResponse)
def api_docs(request: Request): def api_docs(request: Request):
@ -307,6 +315,9 @@ if __name__ == '__main__': # Run directly for debugging: python app.py
app.interface.config = app.interface.get_config_file() app.interface.config = app.interface.get_config_file()
app.interface.show_error = True app.interface.show_error = True
app.interface.flagging_callback.setup(app.interface.flagging_dir) app.interface.flagging_callback.setup(app.interface.flagging_dir)
app.favicon_path = None
app.tokens = {}
auth = True auth = True
if auth: if auth:
app.interface.auth = ("a", "b") app.interface.auth = ("a", "b")
@ -314,5 +325,5 @@ if __name__ == '__main__': # Run directly for debugging: python app.py
app.interface.auth_message = None app.interface.auth_message = None
else: else:
app.auth = None app.auth = None
app.tokens = {}
uvicorn.run(app) uvicorn.run(app)

View File

@ -518,7 +518,8 @@ class Interface:
height: int = 500, height: int = 500,
width: int = 900, width: int = 900,
encrypt: bool = False, encrypt: bool = False,
cache_examples: bool = False cache_examples: bool = False,
favicon_path: Optional[str] = None,
) -> Tuple[flask.Flask, str, str]: ) -> Tuple[flask.Flask, str, str]:
""" """
Launches the webserver that serves the UI for the interface. Launches the webserver that serves the UI for the interface.
@ -540,6 +541,7 @@ class Interface:
height (int): The height in pixels of the <iframe> element containing the interface (used if inline=True) height (int): The height in pixels of the <iframe> element containing the interface (used if inline=True)
encrypt (bool): If True, flagged data will be encrypted by key provided by creator at launch encrypt (bool): If True, flagged data will be encrypted by key provided by creator at launch
cache_examples (bool): If True, examples outputs will be processed and cached in a folder, and will be used if a user uses an example input. cache_examples (bool): If True, examples outputs will be processed and cached in a folder, and will be used if a user uses an example input.
favicon_path (str): If a path to an file (.png, .gif, or .ico) is provided, it will be used as the favicon for the web page.
Returns: Returns:
app (flask.Flask): Flask app object app (flask.Flask): Flask app object
path_to_local_server (str): Locally accessible link path_to_local_server (str): Locally accessible link
@ -555,7 +557,8 @@ class Interface:
self.show_tips = show_tips self.show_tips = show_tips
self.show_error = show_error self.show_error = show_error
self.height = self.height or height self.height = self.height or height
self.width = self.width or width self.width = self.width or width
self.favicon_path = favicon_path
if self.encrypt is None: if self.encrypt is None:
self.encrypt = encrypt self.encrypt = encrypt
@ -575,7 +578,7 @@ class Interface:
cache_interface_examples(self) cache_interface_examples(self)
server_port, path_to_local_server, app, server = networking.start_server( server_port, path_to_local_server, app, server = networking.start_server(
self, server_name, server_port, self.auth) self, server_name, server_port)
self.local_url = path_to_local_server self.local_url = path_to_local_server
self.server_port = server_port self.server_port = server_port

View File

@ -100,7 +100,6 @@ def start_server(
interface: Interface, interface: Interface,
server_name: Optional[str] = None, server_name: Optional[str] = None,
server_port: Optional[int] = None, server_port: Optional[int] = None,
auth: Optional[Callable | Tuple[str, str] | List[Tuple[str, str]]] = None,
) -> Tuple[int, str, fastapi.FastAPI, threading.Thread, None]: ) -> Tuple[int, str, fastapi.FastAPI, threading.Thread, None]:
"""Launches a local server running the provided Interface """Launches a local server running the provided Interface
Parameters: Parameters:
@ -126,6 +125,7 @@ def start_server(
url_host_name = "localhost" if server_name == "0.0.0.0" else server_name url_host_name = "localhost" if server_name == "0.0.0.0" else server_name
path_to_local_server = "http://{}:{}/".format(url_host_name, port) path_to_local_server = "http://{}:{}/".format(url_host_name, port)
auth = interface.auth
if auth is not None: if auth is not None:
if not callable(auth): if not callable(auth):
app.auth = {account[0]: account[1] for account in auth} app.auth = {account[0]: account[1] for account in auth}
@ -135,6 +135,9 @@ def start_server(
app.auth = None app.auth = None
app.interface = interface app.interface = interface
app.cwd = os.getcwd() app.cwd = os.getcwd()
app.favicon_path = interface.favicon_path
app.tokens = {}
if app.interface.enable_queue: if app.interface.enable_queue:
if auth is not None or app.interface.encrypt: if auth is not None or app.interface.encrypt:
raise ValueError("Cannot queue with encryption or authentication enabled.") raise ValueError("Cannot queue with encryption or authentication enabled.")
@ -142,8 +145,8 @@ def start_server(
app.queue_thread = threading.Thread(target=queue_thread, args=(path_to_local_server,)) app.queue_thread = threading.Thread(target=queue_thread, args=(path_to_local_server,))
app.queue_thread.start() app.queue_thread.start()
if interface.save_to is not None: # Used for selenium tests if interface.save_to is not None: # Used for selenium tests
interface.save_to["port"] = port interface.save_to["port"] = port
app.tokens = {}
config = uvicorn.Config(app=app, port=port, host=server_name, config = uvicorn.Config(app=app, port=port, host=server_name,
log_level="warning") log_level="warning")
server = Server(config=config) server = Server(config=config)