Spaces:
Runtime error
Runtime error
Create app-wip.py
Browse files- app-wip.py +61 -0
app-wip.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Replace with your OAuth App settings
|
| 6 |
+
HF_CLIENT_ID = os.environ.get("HF_CLIENT_ID")
|
| 7 |
+
HF_CLIENT_SECRET = os.environ.get("HF_CLIENT_SECRET")
|
| 8 |
+
HF_REDIRECT_URI = os.environ.get("HF_REDIRECT_URI", "https://<your-space-username>.hf.space")
|
| 9 |
+
|
| 10 |
+
AUTH_URL = f"https://huggingface.co/oauth/authorize?client_id={HF_CLIENT_ID}&redirect_uri={HF_REDIRECT_URI}&response_type=code&scope=read"
|
| 11 |
+
|
| 12 |
+
def exchange_code_for_token(code):
|
| 13 |
+
token_url = "https://huggingface.co/oauth/token"
|
| 14 |
+
payload = {
|
| 15 |
+
"grant_type": "authorization_code",
|
| 16 |
+
"code": code,
|
| 17 |
+
"client_id": HF_CLIENT_ID,
|
| 18 |
+
"client_secret": HF_CLIENT_SECRET,
|
| 19 |
+
"redirect_uri": HF_REDIRECT_URI,
|
| 20 |
+
}
|
| 21 |
+
response = requests.post(token_url, data=payload)
|
| 22 |
+
return response.json()
|
| 23 |
+
|
| 24 |
+
def get_user_info(access_token):
|
| 25 |
+
headers = {"Authorization": f"Bearer {access_token}"}
|
| 26 |
+
r = requests.get("https://huggingface.co/api/whoami-v2", headers=headers)
|
| 27 |
+
return r.json()
|
| 28 |
+
|
| 29 |
+
st.title("🔐 Hugging Face OAuth Login")
|
| 30 |
+
|
| 31 |
+
query_params = st.experimental_get_query_params()
|
| 32 |
+
|
| 33 |
+
if "code" not in query_params:
|
| 34 |
+
st.info("Please log in with your Hugging Face account:")
|
| 35 |
+
st.markdown(f"[Login via Hugging Face]({AUTH_URL})")
|
| 36 |
+
else:
|
| 37 |
+
code = query_params["code"][0]
|
| 38 |
+
token_response = exchange_code_for_token(code)
|
| 39 |
+
access_token = token_response.get("access_token")
|
| 40 |
+
|
| 41 |
+
if not access_token:
|
| 42 |
+
st.error("OAuth error: Could not get access token.")
|
| 43 |
+
else:
|
| 44 |
+
user_info = get_user_info(access_token)
|
| 45 |
+
st.success(f"✅ Logged in as: **{user_info.get('name')}**")
|
| 46 |
+
|
| 47 |
+
# Check role in orgs
|
| 48 |
+
orgs = user_info.get("orgs", [])
|
| 49 |
+
if orgs:
|
| 50 |
+
role = orgs[0].get("roleInOrg", "unknown")
|
| 51 |
+
org_name = orgs[0].get("name")
|
| 52 |
+
st.write(f"Org: `{org_name}` | Role: `{role}`")
|
| 53 |
+
|
| 54 |
+
if role == "admin":
|
| 55 |
+
st.success("🎉 You have ADMIN access! Special features unlocked.")
|
| 56 |
+
# Add admin features here
|
| 57 |
+
else:
|
| 58 |
+
st.warning("🔒 Limited access. You're not an admin.")
|
| 59 |
+
else:
|
| 60 |
+
st.info("Not part of any org.")
|
| 61 |
+
|