mirror of
https://github.com/gradio-app/gradio.git
synced 2024-11-21 01:01:05 +08:00
migrating auth
This commit is contained in:
parent
89382d2107
commit
6a9cd383d9
@ -6,8 +6,8 @@ Next, to install the local development version of Gradio:
|
||||
|
||||
When installing locally, you may also need to build the front end:
|
||||
* Navigate to the `/frontend` subfolder and run `npm install`.
|
||||
* Then run `npm run build`.
|
||||
* Then you can run `npm run start` to start a local development server (on port 3000) that responds to any changes in the frontend
|
||||
* Then run `npm run build` (or `npm run build:win` on Windows).
|
||||
* Then you can run `npm run start` to start a local development server (on port 3000 by default) that responds to any changes in the frontend
|
||||
|
||||
### Structure of the Repository
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
from gradio.interface import * # This makes it possible to import `Interface` as `gradio.Interface`.
|
||||
from gradio.networking import get_state, set_state
|
||||
from gradio.app import get_state, set_state
|
||||
from gradio.mix import *
|
||||
from gradio.flagging import *
|
||||
import pkg_resources
|
||||
|
@ -1,8 +1,11 @@
|
||||
"""Implements a FastAPI server to run the gradio interface."""
|
||||
|
||||
from __future__ import annotations
|
||||
from fastapi import FastAPI, Form, Request, HTTPException
|
||||
from fastapi.responses import JSONResponse, HTMLResponse, FileResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
import inspect
|
||||
import os
|
||||
import posixpath
|
||||
@ -14,6 +17,7 @@ import uvicorn
|
||||
|
||||
from gradio import utils
|
||||
|
||||
|
||||
STATIC_TEMPLATE_LIB = pkg_resources.resource_filename("gradio", "templates/")
|
||||
STATIC_PATH_LIB = pkg_resources.resource_filename(
|
||||
"gradio", "templates/frontend/static")
|
||||
@ -23,6 +27,13 @@ with open(VERSION_FILE) as version_file:
|
||||
GRADIO_STATIC_ROOT = "https://gradio.s3-us-west-2.amazonaws.com/{}/static/".format(version)
|
||||
|
||||
app = FastAPI()
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
templates = Jinja2Templates(directory=STATIC_TEMPLATE_LIB)
|
||||
|
||||
|
||||
@ -159,6 +170,34 @@ async def interpret(request: Request):
|
||||
}
|
||||
|
||||
|
||||
# @app.route("/shutdown", methods=['GET'])
|
||||
# def shutdown():
|
||||
# shutdown_func = request.environ.get('werkzeug.server.shutdown')
|
||||
# if shutdown_func is None:
|
||||
# raise RuntimeError('Not running werkzeug')
|
||||
# shutdown_func()
|
||||
# return "Shutting down..."
|
||||
|
||||
|
||||
# @app.route("/api/queue/push/", methods=["POST"])
|
||||
# #@login_check
|
||||
# def queue_push():
|
||||
# data = request.json["data"]
|
||||
# action = request.json["action"]
|
||||
# job_hash, queue_position = queueing.push({"data": data}, action)
|
||||
# return {"hash": job_hash, "queue_position": queue_position}
|
||||
|
||||
|
||||
# @app.route("/api/queue/status/", methods=["POST"])
|
||||
# #@login_check
|
||||
# def queue_status():
|
||||
# hash = request.json['hash']
|
||||
# status, data = queueing.get_status(hash)
|
||||
# return {"status": status, "data": data}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
########
|
||||
# Helper functions
|
||||
@ -203,6 +242,16 @@ def get_types(cls_set, component):
|
||||
types.append(doc_lines[-1].split(")")[0].split("(")[-1])
|
||||
return docset, types
|
||||
|
||||
|
||||
def get_state():
|
||||
pass
|
||||
# return session.get("state")
|
||||
|
||||
|
||||
def set_state(value):
|
||||
pass
|
||||
# session["state"] = value
|
||||
|
||||
|
||||
if __name__ == '__main__': # Run directly for debugging: python app.py
|
||||
from gradio import Interface
|
||||
|
@ -252,7 +252,6 @@ class Interface:
|
||||
self.flagging_callback = flagging_callback
|
||||
self.flagging_dir = flagging_dir
|
||||
|
||||
self.save_to = None
|
||||
self.share = None
|
||||
self.share_url = None
|
||||
self.local_url = None
|
||||
|
@ -5,12 +5,9 @@ creating tunnels.
|
||||
from __future__ import annotations
|
||||
import fastapi
|
||||
from flask import Flask, request, session, jsonify, abort, send_file, render_template, redirect
|
||||
from flask_cachebuster import CacheBuster
|
||||
from flask_login import LoginManager, login_user, current_user, login_required
|
||||
from flask_cors import CORS
|
||||
from functools import wraps
|
||||
import http
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import requests
|
||||
@ -39,25 +36,12 @@ LOCALHOST_NAME = os.getenv('GRADIO_SERVER_NAME', "127.0.0.1")
|
||||
GRADIO_API_SERVER = "https://api.gradio.app/v1/tunnel-request"
|
||||
|
||||
# # TODO: all of this needs to be migrated
|
||||
# app = Flask(__name__,
|
||||
# template_folder=STATIC_TEMPLATE_LIB,
|
||||
# static_folder="",
|
||||
# static_url_path="/none/")
|
||||
# app.url_map.strict_slashes = False # TODO: go back to discussion with Charles
|
||||
|
||||
# CORS(app)
|
||||
# cache_buster = CacheBuster(
|
||||
# config={'extensions': ['.js', '.css'], 'hash_size': 5})
|
||||
# cache_buster.init_app(app)
|
||||
# app.secret_key = os.getenv("GRADIO_KEY", "secret")
|
||||
# login_manager = LoginManager()
|
||||
# login_manager.login_view = 'login'
|
||||
# login_manager.init_app(app)
|
||||
|
||||
# # Hide Flask default message
|
||||
# cli = sys.modules['flask.cli']
|
||||
# cli.show_server_banner = lambda *x: None
|
||||
|
||||
|
||||
# class User:
|
||||
# def __init__(self, id):
|
||||
@ -130,32 +114,6 @@ def get_first_available_port(
|
||||
# return abort(401)
|
||||
|
||||
|
||||
@app.route("/shutdown", methods=['GET'])
|
||||
def shutdown():
|
||||
shutdown_func = request.environ.get('werkzeug.server.shutdown')
|
||||
if shutdown_func is None:
|
||||
raise RuntimeError('Not running werkzeug')
|
||||
shutdown_func()
|
||||
return "Shutting down..."
|
||||
|
||||
|
||||
@app.route("/api/queue/push/", methods=["POST"])
|
||||
#@login_check
|
||||
def queue_push():
|
||||
data = request.json["data"]
|
||||
action = request.json["action"]
|
||||
job_hash, queue_position = queueing.push({"data": data}, action)
|
||||
return {"hash": job_hash, "queue_position": queue_position}
|
||||
|
||||
|
||||
@app.route("/api/queue/status/", methods=["POST"])
|
||||
#@login_check
|
||||
def queue_status():
|
||||
hash = request.json['hash']
|
||||
status, data = queueing.get_status(hash)
|
||||
return {"status": status, "data": data}
|
||||
|
||||
|
||||
def queue_thread(path_to_local_server, test_mode=False):
|
||||
while True:
|
||||
try:
|
||||
@ -215,15 +173,13 @@ def start_server(
|
||||
else:
|
||||
app.auth = None
|
||||
app.interface = interface
|
||||
# app.cwd = os.getcwd()
|
||||
app.cwd = os.getcwd()
|
||||
# if app.interface.enable_queue:
|
||||
# if auth is not None or app.interface.encrypt:
|
||||
# raise ValueError("Cannot queue with encryption or authentication enabled.")
|
||||
# queueing.init()
|
||||
# app.queue_thread = threading.Thread(target=queue_thread, args=(path_to_local_server,))
|
||||
# app.queue_thread.start()
|
||||
# if interface.save_to is not None:
|
||||
# interface.save_to["port"] = port
|
||||
app_kwargs = {"app": app, "port": port, "host": server_name,
|
||||
"log_level": "warning"}
|
||||
thread = threading.Thread(target=uvicorn.run, kwargs=app_kwargs)
|
||||
@ -231,14 +187,6 @@ def start_server(
|
||||
return port, path_to_local_server, app, thread, None
|
||||
|
||||
|
||||
def get_state():
|
||||
return session.get("state")
|
||||
|
||||
|
||||
def set_state(value):
|
||||
session["state"] = value
|
||||
|
||||
|
||||
def setup_tunnel(local_server_port: int, endpoint: str) -> str:
|
||||
response = url_request(
|
||||
endpoint + '/v1/tunnel-request' if endpoint is not None else GRADIO_API_SERVER)
|
||||
|
@ -1 +1 @@
|
||||
<!doctype html><html lang="en" style="height:100%;margin:0;padding:0"><head><meta charset="utf-8"/><meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"><title>{{ config['title'] or 'Gradio' }}</title><meta property="og:url" content="https://gradio.app/"/><meta property="og:type" content="website"/><meta property="og:image" content="{{ config['thumbnail'] or '' }}"/><meta property="og:title" content="{{ config['title'] or '' }}"/><meta property="og:description" content="{{ config['description'] or '' }}"/><meta name="twitter:card" content="summary_large_image"><meta name="twitter:creator" content="@teamGradio"><meta name="twitter:title" content="{{ config['title'] or '' }}"><meta name="twitter:description" content="{{ config['description'] or '' }}"><meta name="twitter:image" content="{{ config['thumbnail'] or '' }}"><script async src="https://www.googletagmanager.com/gtag/js?id=UA-156449732-1"></script><script>function gtag(){dataLayer.push(arguments)}window.dataLayer=window.dataLayer||[],gtag("js",new Date),gtag("config","UA-156449732-1"),window.gradio_mode="app";try{let a="";window.gradio_config=JSON.parse("{{ config|tojson }}"+a)}catch(a){}</script><script src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.1/iframeResizer.contentWindow.min.js"></script><title>Gradio</title><link href="static/bundle.css" rel="stylesheet"><link href="static/css/main.9666dfea.css" rel="stylesheet"></head><body style="height:100%;margin:0;padding:0"><div id="root" style="height:100%"></div><script src="static/bundle.js"></script></body></html>
|
||||
<!doctype html><html lang="en" style="height:100%;margin:0;padding:0"><head><meta charset="utf-8"/><meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"><title>{{ config['title'] or 'Gradio' }}</title><meta property="og:url" content="https://gradio.app/"/><meta property="og:type" content="website"/><meta property="og:image" content="{{ config['thumbnail'] or '' }}"/><meta property="og:title" content="{{ config['title'] or '' }}"/><meta property="og:description" content="{{ config['description'] or '' }}"/><meta name="twitter:card" content="summary_large_image"><meta name="twitter:creator" content="@teamGradio"><meta name="twitter:title" content="{{ config['title'] or '' }}"><meta name="twitter:description" content="{{ config['description'] or '' }}"><meta name="twitter:image" content="{{ config['thumbnail'] or '' }}"><script async src="https://www.googletagmanager.com/gtag/js?id=UA-156449732-1"></script><script>function gtag(){dataLayer.push(arguments)}window.dataLayer=window.dataLayer||[],gtag("js",new Date),gtag("config","UA-156449732-1"),window.gradio_mode="app"</script><script>window.gradio_config = {{ config|tojson }};</script><script src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.1/iframeResizer.contentWindow.min.js"></script><title>Gradio</title><link href="static/bundle.css" rel="stylesheet"><link href="static/css/main.9666dfea.css" rel="stylesheet"></head><body style="height:100%;margin:0;padding:0"><div id="root" style="height:100%"></div><script src="static/bundle.js"></script></body></html>
|
Loading…
Reference in New Issue
Block a user