Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,81 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from home import build_home_page
|
| 4 |
-
from
|
| 5 |
-
from ui import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import json
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from huggingface_hub import whoami
|
| 5 |
+
|
| 6 |
+
from common import get_db
|
| 7 |
from home import build_home_page
|
| 8 |
+
from modules.models import SheamiUser
|
| 9 |
+
from ui import (
|
| 10 |
+
get_app_theme,
|
| 11 |
+
get_app_title,
|
| 12 |
+
get_css,
|
| 13 |
+
render_logo,
|
| 14 |
+
) # Optional: to show retrieving org info
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def get_logged_in_user_name(profile: gr.OAuthProfile | None) -> str:
|
| 18 |
+
if profile is None:
|
| 19 |
+
return None
|
| 20 |
+
return f"{profile.name}"
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def get_sheami_user(profile: gr.OAuthProfile | None):
|
| 24 |
+
if profile is None:
|
| 25 |
+
return None
|
| 26 |
+
return SheamiUser(
|
| 27 |
+
email=f"{profile.username}",
|
| 28 |
+
name=f"{profile.name}",
|
| 29 |
+
picture_url=f"{profile.picture}",
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def list_organizations(oauth_token: gr.OAuthToken | None) -> str:
|
| 34 |
+
if oauth_token is None:
|
| 35 |
+
return "Please log in to list organizations."
|
| 36 |
+
org_names = [org["name"] for org in whoami(oauth_token.token)["orgs"]]
|
| 37 |
+
return f"You belong to {', '.join(org_names)}."
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def get_user_from_request(profile: gr.OAuthProfile):
|
| 41 |
+
print("profile.username = ", profile.username)
|
| 42 |
+
return profile.username or None
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
async def build_securely():
|
| 46 |
+
with gr.Blocks(
|
| 47 |
+
title=get_app_title(), theme=get_app_theme(), css=get_css(), fill_height=True
|
| 48 |
+
) as demo:
|
| 49 |
+
# Top menu bar with logo and login/logout buttons
|
| 50 |
+
with gr.Row():
|
| 51 |
+
with gr.Column(scale=5):
|
| 52 |
+
render_logo()
|
| 53 |
+
with gr.Column(scale=1):
|
| 54 |
+
gr.LoginButton()
|
| 55 |
+
logged_in_user_name = gr.Markdown()
|
| 56 |
+
logged_in_user_id = gr.State()
|
| 57 |
+
m2 = gr.Markdown()
|
| 58 |
+
|
| 59 |
+
@gr.render(inputs=logged_in_user_id)
|
| 60 |
+
def render_home_page(user: SheamiUser | None):
|
| 61 |
+
if user:
|
| 62 |
+
asyncio.run(register_user(logged_in_user=user))
|
| 63 |
+
build_home_page(logged_in_user=user)
|
| 64 |
+
else:
|
| 65 |
+
pass
|
| 66 |
+
|
| 67 |
+
demo.load(get_logged_in_user_name, inputs=None, outputs=logged_in_user_name)
|
| 68 |
+
demo.load(get_sheami_user, inputs=None, outputs=logged_in_user_id)
|
| 69 |
+
# demo.load(list_organizations, inputs=None, outputs=m2)
|
| 70 |
+
|
| 71 |
+
return demo
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
async def register_user(logged_in_user: SheamiUser):
|
| 75 |
+
user = await get_db().get_user_by_email(email=logged_in_user.email)
|
| 76 |
+
if not user:
|
| 77 |
+
# register the user
|
| 78 |
+
await get_db().add_user(email=logged_in_user.email, name=logged_in_user.name)
|
| 79 |
+
|
| 80 |
+
demo = asyncio.run(build_securely())
|
| 81 |
+
demo.queue().launch()
|