Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,49 +9,55 @@ HF_CLIENT_SECRET = os.environ.get("HF_CLIENT_SECRET")
|
|
| 9 |
#print(f" space details: {SPACE_HOST}")
|
| 10 |
|
| 11 |
shost = os.getenv("SPACE_HOST")
|
| 12 |
-
print(
|
|
|
|
| 13 |
|
| 14 |
HF_REDIRECT_URI = f"https://{shost}"
|
| 15 |
#HF_REDIRECT_URI = f"https://{shost}/login/callback"
|
| 16 |
|
| 17 |
AUTH_URL = f"https://huggingface.co/oauth/authorize?client_id={HF_CLIENT_ID}&redirect_uri={HF_REDIRECT_URI}&response_type=code&scope=read"
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
token_url = "https://huggingface.co/oauth/token"
|
| 21 |
payload = {
|
| 22 |
"grant_type": "authorization_code",
|
| 23 |
"code": code,
|
| 24 |
-
"client_id":
|
| 25 |
-
"client_secret":
|
| 26 |
-
"redirect_uri":
|
| 27 |
}
|
|
|
|
| 28 |
response = requests.post(token_url, data=payload)
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
st.title("🔐 Hugging Face OAuth Login")
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
-
if "code" not in query_params:
|
| 43 |
-
st.info("Please log in with your Hugging Face account:")
|
| 44 |
-
st.markdown(f"[Login via Hugging Face]({AUTH_URL})")
|
| 45 |
-
else:
|
| 46 |
-
code = query_params["code"][0]
|
| 47 |
-
token_response = exchange_code_for_token(code)
|
| 48 |
-
access_token = token_response.get("access_token")
|
| 49 |
-
|
| 50 |
-
if not access_token:
|
| 51 |
-
st.error("OAuth error: Could not get access token.")
|
| 52 |
-
else:
|
| 53 |
-
user_info = get_user_info(access_token)
|
| 54 |
-
st.success(f"✅ Logged in as: **{user_info.get('name')}**")
|
| 55 |
|
| 56 |
# Check role in orgs
|
| 57 |
orgs = user_info.get("orgs", [])
|
|
@@ -68,4 +74,15 @@ else:
|
|
| 68 |
st.warning("🔒 Limited access. You're not an admin.")
|
| 69 |
else:
|
| 70 |
st.info("Not part of any org.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
|
|
|
| 9 |
#print(f" space details: {SPACE_HOST}")
|
| 10 |
|
| 11 |
shost = os.getenv("SPACE_HOST")
|
| 12 |
+
print("==============space host uri====================")
|
| 13 |
+
print(f"{shost}")
|
| 14 |
|
| 15 |
HF_REDIRECT_URI = f"https://{shost}"
|
| 16 |
#HF_REDIRECT_URI = f"https://{shost}/login/callback"
|
| 17 |
|
| 18 |
AUTH_URL = f"https://huggingface.co/oauth/authorize?client_id={HF_CLIENT_ID}&redirect_uri={HF_REDIRECT_URI}&response_type=code&scope=read"
|
| 19 |
|
| 20 |
+
# ---- Store Code if Present ----
|
| 21 |
+
query_params = st.experimental_get_query_params()
|
| 22 |
+
|
| 23 |
+
if "code" in query_params and "code" not in st.session_state:
|
| 24 |
+
st.session_state["code"] = query_params["code"][0]
|
| 25 |
+
|
| 26 |
+
st.title("🤖 Hugging Face OAuth Login")
|
| 27 |
+
|
| 28 |
+
# ---- OAuth Flow ----
|
| 29 |
+
if "code" not in st.session_state:
|
| 30 |
+
st.info("Please log in with your Hugging Face account:")
|
| 31 |
+
st.markdown(f"[Login via Hugging Face]({AUTH_URL})")
|
| 32 |
+
else:
|
| 33 |
+
st.success("Authorization code received!")
|
| 34 |
+
code = st.session_state["code"]
|
| 35 |
+
|
| 36 |
+
# Exchange for token
|
| 37 |
token_url = "https://huggingface.co/oauth/token"
|
| 38 |
payload = {
|
| 39 |
"grant_type": "authorization_code",
|
| 40 |
"code": code,
|
| 41 |
+
"client_id": CLIENT_ID,
|
| 42 |
+
"client_secret": CLIENT_SECRET,
|
| 43 |
+
"redirect_uri": REDIRECT_URI,
|
| 44 |
}
|
| 45 |
+
|
| 46 |
response = requests.post(token_url, data=payload)
|
| 47 |
+
if response.status_code == 200:
|
| 48 |
+
access_token = response.json().get("access_token")
|
| 49 |
+
st.success("✅ Access token received!")
|
| 50 |
|
| 51 |
+
# Display user info
|
| 52 |
+
user_info = requests.get(
|
| 53 |
+
"https://huggingface.co/api/whoami-v2",
|
| 54 |
+
headers={"Authorization": f"Bearer {access_token}"}
|
| 55 |
+
).json()
|
| 56 |
|
|
|
|
| 57 |
|
| 58 |
+
st.subheader("👤 Logged in as:")
|
| 59 |
+
st.json(user_info)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
# Check role in orgs
|
| 63 |
orgs = user_info.get("orgs", [])
|
|
|
|
| 74 |
st.warning("🔒 Limited access. You're not an admin.")
|
| 75 |
else:
|
| 76 |
st.info("Not part of any org.")
|
| 77 |
+
else:
|
| 78 |
+
st.error(f"Failed to get token: {response.text}")
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
|