Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update modules/app.py
Browse files- modules/app.py +72 -12
modules/app.py
CHANGED
|
@@ -1,20 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
from io import BytesIO
|
| 5 |
|
| 6 |
-
|
| 7 |
-
from fastapi.staticfiles import StaticFiles
|
| 8 |
-
from fastapi.responses import FileResponse, StreamingResponse
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
app.
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
@app.
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import uvicorn
|
| 2 |
+
from fastapi import FastAPI, Depends
|
| 3 |
+
from starlette.responses import RedirectResponse
|
| 4 |
+
from starlette.middleware.sessions import SessionMiddleware
|
| 5 |
+
from authlib.integrations.starlette_client import OAuth, OAuthError
|
| 6 |
+
from fastapi import Request
|
| 7 |
import os
|
| 8 |
+
from starlette.config import Config
|
| 9 |
+
import gradio as gr
|
|
|
|
| 10 |
|
| 11 |
+
app = FastAPI()
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# OAuth settings
|
| 14 |
+
GOOGLE_CLIENT_ID = os.environ.get("GOOGLE_CLIENT_ID")
|
| 15 |
+
GOOGLE_CLIENT_SECRET = os.environ.get("GOOGLE_CLIENT_SECRET")
|
| 16 |
+
SECRET_KEY = os.environ.get("SECRET_KEY")
|
| 17 |
|
| 18 |
+
# Set up OAuth
|
| 19 |
+
config_data = {'GOOGLE_CLIENT_ID': GOOGLE_CLIENT_ID, 'GOOGLE_CLIENT_SECRET': GOOGLE_CLIENT_SECRET}
|
| 20 |
+
starlette_config = Config(environ=config_data)
|
| 21 |
+
oauth = OAuth(starlette_config)
|
| 22 |
+
oauth.register(
|
| 23 |
+
name='google',
|
| 24 |
+
server_metadata_url='https://accounts.google.com/.well-known/openid-configuration',
|
| 25 |
+
client_kwargs={'scope': 'openid email profile'},
|
| 26 |
+
)
|
| 27 |
|
| 28 |
+
app.add_middleware(SessionMiddleware, secret_key=SECRET_KEY)
|
| 29 |
|
| 30 |
+
# Dependency to get the current user
|
| 31 |
+
def get_user(request: Request):
|
| 32 |
+
user = request.session.get('user')
|
| 33 |
+
if user:
|
| 34 |
+
return user['name']
|
| 35 |
+
return None
|
| 36 |
|
| 37 |
+
@app.get('/')
|
| 38 |
+
def public(user: dict = Depends(get_user)):
|
| 39 |
+
if user:
|
| 40 |
+
return RedirectResponse(url='/gradio')
|
| 41 |
+
else:
|
| 42 |
+
return RedirectResponse(url='/login-demo')
|
| 43 |
|
| 44 |
+
@app.route('/logout')
|
| 45 |
+
async def logout(request: Request):
|
| 46 |
+
request.session.pop('user', None)
|
| 47 |
+
return RedirectResponse(url='/')
|
| 48 |
+
|
| 49 |
+
@app.route('/login')
|
| 50 |
+
async def login(request: Request):
|
| 51 |
+
redirect_uri = request.url_for('auth')
|
| 52 |
+
return await oauth.google.authorize_redirect(request, redirect_uri)
|
| 53 |
+
|
| 54 |
+
@app.route('/auth')
|
| 55 |
+
async def auth(request: Request):
|
| 56 |
+
try:
|
| 57 |
+
access_token = await oauth.google.authorize_access_token(request)
|
| 58 |
+
except OAuthError:
|
| 59 |
+
return RedirectResponse(url='/')
|
| 60 |
+
request.session['user'] = dict(access_token)["userinfo"]
|
| 61 |
+
return RedirectResponse(url='/')
|
| 62 |
+
|
| 63 |
+
with gr.Blocks() as login_demo:
|
| 64 |
+
gr.Button("Login", link="/login")
|
| 65 |
+
|
| 66 |
+
app = gr.mount_gradio_app(app, login_demo, path="/login-demo")
|
| 67 |
+
|
| 68 |
+
def greet(request: gr.Request):
|
| 69 |
+
return f"Welcome to Gradio, {request.username}"
|
| 70 |
+
|
| 71 |
+
with gr.Blocks() as main_demo:
|
| 72 |
+
m = gr.Markdown("Welcome to Gradio!")
|
| 73 |
+
gr.Button("Logout", link="/logout")
|
| 74 |
+
main_demo.load(greet, None, m)
|
| 75 |
+
|
| 76 |
+
app = gr.mount_gradio_app(app, main_demo, path="/gradio", auth_dependency=get_user)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
if __name__ == '__main__':
|
| 80 |
+
uvicorn.run(app)
|