louiecerv commited on
Commit
c2c6992
·
1 Parent(s): d011eb6

sync with remote

Browse files
Files changed (3) hide show
  1. .env +3 -0
  2. app.py +84 -0
  3. requirements.txt +2 -0
.env ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ HUGGINGFACE_CLIENT_ID=235a7dd9-fbc7-45f8-ab3c-3bf171470af5
2
+ HUGGINGFACE_CLIENT_SECRET=d5a36042-ed4e-4a89-8146-e6f4de6d4b12
3
+ HUGGINGFACE_REDIRECT_URI=http://localhost:8501
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import os
4
+ import json
5
+ import urllib.parse
6
+ from dotenv import load_dotenv
7
+
8
+ # Load environment variables (if using a .env file)
9
+ load_dotenv()
10
+
11
+ # Hugging Face OAuth Credentials
12
+ CLIENT_ID = os.getenv("HUGGINGFACE_CLIENT_ID")
13
+ CLIENT_SECRET = os.getenv("HUGGINGFACE_CLIENT_SECRET")
14
+ REDIRECT_URI = os.getenv("HUGGINGFACE_REDIRECT_URI")
15
+
16
+ # Hugging Face OAuth URLs
17
+ AUTHORIZATION_URL = "https://huggingface.co/oauth/authorize"
18
+ TOKEN_URL = "https://huggingface.co/oauth/token"
19
+ USER_INFO_URL = "https://huggingface.co/api/whoami-v2"
20
+
21
+ # Streamlit Session State to Track User
22
+ if "user" not in st.session_state:
23
+ st.session_state.user = None
24
+
25
+ def get_authorization_url():
26
+ """Generate the Hugging Face authorization URL."""
27
+ params = {
28
+ "response_type": "code",
29
+ "client_id": CLIENT_ID,
30
+ "redirect_uri": REDIRECT_URI,
31
+ }
32
+ return f"{AUTHORIZATION_URL}?{urllib.parse.urlencode(params)}"
33
+
34
+ def exchange_code_for_token(auth_code):
35
+ """Exchange authorization code for an access token."""
36
+ data = {
37
+ "grant_type": "authorization_code",
38
+ "code": auth_code,
39
+ "client_id": CLIENT_ID,
40
+ "client_secret": CLIENT_SECRET,
41
+ "redirect_uri": REDIRECT_URI,
42
+ }
43
+ response = requests.post(TOKEN_URL, data=data)
44
+ return response.json()
45
+
46
+ def get_user_info(access_token):
47
+ """Fetch user details from Hugging Face API."""
48
+ headers = {"Authorization": f"Bearer {access_token}"}
49
+ response = requests.get(USER_INFO_URL, headers=headers)
50
+ return response.json()
51
+
52
+ def main():
53
+ # Display App Title
54
+ st.title("Sign In with Hugging Face")
55
+
56
+ # Check if user is logged in
57
+ if st.session_state.user:
58
+ st.success(f"Logged in as **{st.session_state.user['name']}**")
59
+ st.image(st.session_state.user['avatar'], width=100)
60
+ if st.button("Logout"):
61
+ st.session_state.user = None
62
+ st.experimental_rerun()
63
+ else:
64
+ auth_url = get_authorization_url()
65
+ st.markdown(f"[![Sign in with Hugging Face](https://huggingface.co/front/assets/huggingface_logo-noborder.svg)]({auth_url})")
66
+
67
+ # Capture OAuth Redirect URL with Auth Code
68
+ query_params = st.query_params
69
+ if "code" in query_params:
70
+ auth_code = query_params["code"][0]
71
+ token_data = exchange_code_for_token(auth_code)
72
+
73
+ if "access_token" in token_data:
74
+ user_info = get_user_info(token_data["access_token"])
75
+ st.session_state.user = {
76
+ "name": user_info.get("name", "Unknown User"),
77
+ "avatar": user_info.get("avatar", ""),
78
+ }
79
+ st.rerun()
80
+ else:
81
+ st.error("Failed to authenticate with Hugging Face.")
82
+
83
+ if __name__ == "__main__":
84
+ main()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ streamlit
2
+ requests