Sybghat commited on
Commit
87c0c6d
1 Parent(s): dc47801

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +94 -0
  2. start.py +3 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = "687634210515-r86aagm0mse8qeq2fhi0t6atd0o1gln5.apps.googleusercontent.com" #os.environ.get("GOOGLE_CLIENT_ID")
15
+ GOOGLE_CLIENT_SECRET = "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" #os.environ.get("GOOGLE_CLIENT_SECRET")
16
+ SECRET_KEY = "3fc4jb0ohuBuFGWxohXspsCuxXaF" #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(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
+ redirect_uri = "https://www.google.com"
55
+
56
+ print("Redirecting to", redirect_uri)
57
+ return await oauth.google.authorize_redirect(request, redirect_uri)
58
+
59
+ @app.route('/auth')
60
+ async def auth(request: Request):
61
+ try:
62
+ access_token = await oauth.google.authorize_access_token(request)
63
+ except OAuthError:
64
+ print("Error getting access token", str(OAuthError))
65
+ return RedirectResponse(url='/')
66
+ request.session['user'] = dict(access_token)["userinfo"]
67
+ print("Redirecting to /gradio")
68
+ return RedirectResponse(url='/gradio')
69
+
70
+ with gr.Blocks() as login_demo:
71
+ btn = gr.Button("Login with Google")
72
+ _js_redirect = """
73
+ () => {
74
+ url = '/login' + window.location.search;
75
+ window.open(url);
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
+ return f"Welcome to Gradio, {request.username}"
84
+
85
+ with gr.Blocks() as main_demo:
86
+ m = gr.Markdown("Welcome to Gradio!")
87
+ gr.Button("Logout", link="/logout")
88
+ main_demo.load(greet, None, m)
89
+
90
+ app = gr.mount_gradio_app(app, main_demo, path="/gradio", auth_dependency=get_user)
91
+
92
+
93
+ if __name__ == '__main__':
94
+ uvicorn.run(app)
start.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ import subprocess
2
+
3
+ subprocess.run("uvicorn app:app --host 0.0.0.0 --port 7860", shell=True)