Update app.py
Browse files
app.py
CHANGED
@@ -130,22 +130,64 @@ input[type="text"], input[type="password"], textarea {
|
|
130 |
}
|
131 |
"""
|
132 |
|
133 |
-
#
|
134 |
-
def
|
135 |
-
# Get the space host from environment variable
|
136 |
space_host = os.environ.get("SPACE_HOST", "localhost:7860")
|
137 |
-
|
138 |
|
139 |
-
# Construct the full redirect URL
|
140 |
-
redirect_uri = f"https://{space_host}/auth/callback"
|
141 |
-
|
142 |
-
# Create the complete authorization URL
|
143 |
-
auth_url = f"https://huggingface.co/oauth/authorize?client_id=hf&redirect_uri={redirect_uri}&scope=openid%20profile&response_type=code"
|
144 |
-
|
145 |
-
# Return JavaScript to open the login page
|
146 |
return f"""
|
|
|
147 |
<script>
|
148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
</script>
|
150 |
"""
|
151 |
|
@@ -163,8 +205,8 @@ def check_user(request: gr.Request):
|
|
163 |
is_admin = (username == "Quazim0t0")
|
164 |
db.add_user(username, username, is_admin)
|
165 |
user = db.get_user_by_username(username)
|
166 |
-
return
|
167 |
-
return
|
168 |
|
169 |
# Start evaluation queue worker
|
170 |
def start_queue_worker():
|
@@ -182,15 +224,17 @@ with gr.Blocks(css=css, title="Dynamic Highscores") as app:
|
|
182 |
user_state = gr.State(None)
|
183 |
|
184 |
# Login section
|
185 |
-
with gr.Row(
|
186 |
with gr.Column():
|
187 |
-
|
188 |
-
|
|
|
|
|
|
|
|
|
189 |
|
190 |
-
#
|
191 |
-
|
192 |
-
with gr.Column():
|
193 |
-
user_info = gr.Markdown("User information")
|
194 |
|
195 |
gr.Markdown("# 🏆 Dynamic Highscores", elem_classes=["header"])
|
196 |
gr.Markdown("""
|
@@ -223,14 +267,11 @@ with gr.Blocks(css=css, title="Dynamic Highscores") as app:
|
|
223 |
Created by Quazim0t0
|
224 |
""", elem_classes=["footer"])
|
225 |
|
226 |
-
# Connect login handler
|
227 |
-
login_button.click(fn=get_login_url, inputs=[], outputs=[gr.HTML()])
|
228 |
-
|
229 |
# Check login on page load
|
230 |
app.load(
|
231 |
fn=check_user,
|
232 |
inputs=[],
|
233 |
-
outputs=[
|
234 |
)
|
235 |
|
236 |
# Launch the app
|
|
|
130 |
}
|
131 |
"""
|
132 |
|
133 |
+
# JavaScript login implementation
|
134 |
+
def js_login_script():
|
|
|
135 |
space_host = os.environ.get("SPACE_HOST", "localhost:7860")
|
136 |
+
redirect_uri = f"https://{space_host}"
|
137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
return f"""
|
139 |
+
<script src="https://unpkg.com/@huggingface/[email protected]/dist/index.umd.min.js"></script>
|
140 |
<script>
|
141 |
+
(async function() {{
|
142 |
+
const HfHub = window.HfHub;
|
143 |
+
try {{
|
144 |
+
// Check if we're returning from OAuth redirect
|
145 |
+
const oauthResult = await HfHub.oauthHandleRedirectIfPresent();
|
146 |
+
|
147 |
+
if (oauthResult) {{
|
148 |
+
console.log("User logged in:", oauthResult);
|
149 |
+
|
150 |
+
// Store the user info in localStorage
|
151 |
+
localStorage.setItem("hf_user", JSON.stringify(oauthResult.userInfo));
|
152 |
+
localStorage.setItem("hf_token", oauthResult.accessToken);
|
153 |
+
|
154 |
+
// Update the UI to show logged in state
|
155 |
+
document.getElementById("login-status").textContent = "Logged in as: " + oauthResult.userInfo.name;
|
156 |
+
document.getElementById("login-button").style.display = "none";
|
157 |
+
|
158 |
+
// Refresh the page to update server-side state
|
159 |
+
setTimeout(() => window.location.reload(), 1000);
|
160 |
+
}}
|
161 |
+
}} catch (error) {{
|
162 |
+
console.error("OAuth error:", error);
|
163 |
+
}}
|
164 |
+
|
165 |
+
// Check if user is already logged in from localStorage
|
166 |
+
const storedUser = localStorage.getItem("hf_user");
|
167 |
+
if (storedUser) {{
|
168 |
+
try {{
|
169 |
+
const userInfo = JSON.parse(storedUser);
|
170 |
+
document.getElementById("login-status").textContent = "Logged in as: " + userInfo.name;
|
171 |
+
document.getElementById("login-button").style.display = "none";
|
172 |
+
}} catch (e) {{
|
173 |
+
console.error("Error parsing stored user:", e);
|
174 |
+
}}
|
175 |
+
}}
|
176 |
+
|
177 |
+
// Setup login button
|
178 |
+
document.getElementById("login-button").addEventListener("click", async function() {{
|
179 |
+
try {{
|
180 |
+
const loginUrl = await HfHub.oauthLoginUrl({{
|
181 |
+
redirectUrl: "{redirect_uri}",
|
182 |
+
scopes: ["openid", "profile"]
|
183 |
+
}});
|
184 |
+
window.location.href = loginUrl;
|
185 |
+
}} catch (error) {{
|
186 |
+
console.error("Error generating login URL:", error);
|
187 |
+
alert("Error starting login process. Please try again.");
|
188 |
+
}}
|
189 |
+
}});
|
190 |
+
}})();
|
191 |
</script>
|
192 |
"""
|
193 |
|
|
|
205 |
is_admin = (username == "Quazim0t0")
|
206 |
db.add_user(username, username, is_admin)
|
207 |
user = db.get_user_by_username(username)
|
208 |
+
return username
|
209 |
+
return None
|
210 |
|
211 |
# Start evaluation queue worker
|
212 |
def start_queue_worker():
|
|
|
224 |
user_state = gr.State(None)
|
225 |
|
226 |
# Login section
|
227 |
+
with gr.Row(elem_classes=["login-section"]):
|
228 |
with gr.Column():
|
229 |
+
gr.HTML("""
|
230 |
+
<div style="display: flex; justify-content: space-between; align-items: center;">
|
231 |
+
<div id="login-status">Not logged in</div>
|
232 |
+
<button id="login-button" style="padding: 8px 16px; background-color: #4CAF50; color: white; border: none; border-radius: 4px; cursor: pointer;">Login with HuggingFace</button>
|
233 |
+
</div>
|
234 |
+
""")
|
235 |
|
236 |
+
# Add the JS login script
|
237 |
+
gr.HTML(js_login_script())
|
|
|
|
|
238 |
|
239 |
gr.Markdown("# 🏆 Dynamic Highscores", elem_classes=["header"])
|
240 |
gr.Markdown("""
|
|
|
267 |
Created by Quazim0t0
|
268 |
""", elem_classes=["footer"])
|
269 |
|
|
|
|
|
|
|
270 |
# Check login on page load
|
271 |
app.load(
|
272 |
fn=check_user,
|
273 |
inputs=[],
|
274 |
+
outputs=[user_state]
|
275 |
)
|
276 |
|
277 |
# Launch the app
|