Spaces:
Sleeping
Sleeping
Create modules/app.py
Browse files- modules/app.py +97 -0
modules/app.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from authlib.integrations.starlette_client import OAuth, OAuthError
|
3 |
+
from fastapi import FastAPI, Depends, Request
|
4 |
+
from starlette.config import Config
|
5 |
+
from starlette.responses import RedirectResponse
|
6 |
+
from starlette.middleware.sessions import SessionMiddleware
|
7 |
+
import uvicorn
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
app = FastAPI()
|
11 |
+
|
12 |
+
# OAuth settings
|
13 |
+
GOOGLE_CLIENT_ID = os.environ.get("GOOGLE_CLIENT_ID")
|
14 |
+
GOOGLE_CLIENT_SECRET = os.environ.get("GOOGLE_CLIENT_SECRET")
|
15 |
+
SECRET_KEY = os.environ.get("SECRET_KEY")
|
16 |
+
|
17 |
+
# Set up OAuth
|
18 |
+
config_data = {'GOOGLE_CLIENT_ID': GOOGLE_CLIENT_ID, 'GOOGLE_CLIENT_SECRET': GOOGLE_CLIENT_SECRET}
|
19 |
+
starlette_config = Config(environ=config_data)
|
20 |
+
oauth = OAuth(starlette_config)
|
21 |
+
oauth.register(
|
22 |
+
name='google',
|
23 |
+
server_metadata_url='https://accounts.google.com/.well-known/openid-configuration',
|
24 |
+
client_kwargs={'scope': 'openid email profile'},
|
25 |
+
)
|
26 |
+
|
27 |
+
app.add_middleware(SessionMiddleware, secret_key=SECRET_KEY)
|
28 |
+
|
29 |
+
# Dependency to get the current user
|
30 |
+
def get_user(request: Request):
|
31 |
+
user = request.session.get('user')
|
32 |
+
return user
|
33 |
+
# if user:
|
34 |
+
# return user['name']
|
35 |
+
# return None
|
36 |
+
|
37 |
+
@app.get('/')
|
38 |
+
def public(request: Request, user = Depends(get_user)):
|
39 |
+
root_url = gr.route_utils.get_root_url(request, "/", None)
|
40 |
+
if user:
|
41 |
+
return RedirectResponse(url=f'{root_url}/gradio')
|
42 |
+
else:
|
43 |
+
return RedirectResponse(url=f'{root_url}/main')
|
44 |
+
|
45 |
+
@app.route('/logout')
|
46 |
+
async def logout(request: Request):
|
47 |
+
request.session.pop('user', None)
|
48 |
+
return RedirectResponse(url='/')
|
49 |
+
|
50 |
+
@app.route('/login')
|
51 |
+
async def login(request: Request):
|
52 |
+
root_url = gr.route_utils.get_root_url(request, "/login", None)
|
53 |
+
redirect_uri = f"{root_url}/auth"
|
54 |
+
print("Redirecting to", redirect_uri)
|
55 |
+
return await oauth.google.authorize_redirect(request, redirect_uri)
|
56 |
+
|
57 |
+
@app.route('/auth')
|
58 |
+
async def auth(request: Request):
|
59 |
+
try:
|
60 |
+
access_token = await oauth.google.authorize_access_token(request)
|
61 |
+
except OAuthError:
|
62 |
+
print("Error getting access token", str(OAuthError))
|
63 |
+
return RedirectResponse(url='/')
|
64 |
+
request.session['user'] = dict(access_token)["userinfo"]
|
65 |
+
print("Redirecting to /gradio")
|
66 |
+
return RedirectResponse(url='/gradio')
|
67 |
+
|
68 |
+
with gr.Blocks() as login_demo:
|
69 |
+
gr.Button("Login", link="/login")
|
70 |
+
# btn = gr.Button("Login")
|
71 |
+
# _js_redirect = """
|
72 |
+
# () => {
|
73 |
+
# url = '/login' + window.location.search;
|
74 |
+
# window.open(url, "mozillaWindow", "popup");
|
75 |
+
# // window.open(url, '_blank');
|
76 |
+
# }
|
77 |
+
# """
|
78 |
+
# btn.click(None, js=_js_redirect)
|
79 |
+
|
80 |
+
app = gr.mount_gradio_app(app, login_demo, path="/main")
|
81 |
+
|
82 |
+
def greet(request: gr.Request):
|
83 |
+
user = request.session.get('user')
|
84 |
+
# if request:
|
85 |
+
# print("Request headers dictionary:", request.headers)
|
86 |
+
# print("IP address:", request.client.host)
|
87 |
+
# print("Query parameters:", dict(request.query_params))
|
88 |
+
# print("Session hash:", request.session_hash)
|
89 |
+
return f"Welcome to Gradio, {request.username['name']}", request.username
|
90 |
+
|
91 |
+
with gr.Blocks() as main_demo:
|
92 |
+
m = gr.Markdown("Welcome to Gradio!")
|
93 |
+
databox = gr.Textbox(interactive=False)
|
94 |
+
gr.Button("Logout", link="/logout")
|
95 |
+
main_demo.load(greet, None, [m, databox])
|
96 |
+
|
97 |
+
app = gr.mount_gradio_app(app, main_demo, path="/gradio", auth_dependency=get_user)
|