Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
Main application for Dynamic Highscores system.
|
|
|
|
| 3 |
This file integrates all components into a unified application.
|
| 4 |
"""
|
| 5 |
|
|
@@ -7,6 +8,15 @@ import os
|
|
| 7 |
import gradio as gr
|
| 8 |
import threading
|
| 9 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
from database_schema import DynamicHighscoresDB
|
| 11 |
from auth import HuggingFaceAuth, create_login_ui, setup_auth_handlers
|
| 12 |
from benchmark_selection import BenchmarkSelector, create_benchmark_selection_ui
|
|
@@ -27,13 +37,31 @@ benchmarks = db.get_benchmarks()
|
|
| 27 |
if not benchmarks or len(benchmarks) == 0:
|
| 28 |
print("No benchmarks found. Adding sample benchmarks...")
|
| 29 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
num_added = add_sample_benchmarks()
|
| 31 |
print(f"Added {num_added} sample benchmarks.")
|
| 32 |
except Exception as e:
|
| 33 |
print(f"Error adding sample benchmarks: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
# Custom CSS
|
| 36 |
css = """
|
|
|
|
| 37 |
.info-text {
|
| 38 |
background-color: rgba(53, 130, 220, 0.1);
|
| 39 |
padding: 12px;
|
|
@@ -41,10 +69,109 @@ css = """
|
|
| 41 |
border-left: 4px solid #3498db;
|
| 42 |
margin: 12px 0;
|
| 43 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
"""
|
| 45 |
|
| 46 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
with gr.Blocks(css=css, title="Dynamic Highscores") as app:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
gr.Markdown("# π Dynamic Highscores", elem_classes=["header"])
|
| 49 |
gr.Markdown("""
|
| 50 |
Welcome to Dynamic Highscores - a community benchmark platform for evaluating and comparing language models.
|
|
@@ -65,7 +192,57 @@ with gr.Blocks(css=css, title="Dynamic Highscores") as app:
|
|
| 65 |
|
| 66 |
with gr.TabItem("π Benchmarks", id=2):
|
| 67 |
benchmark_ui = create_benchmark_selection_ui(benchmark_selector, auth_manager)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# Launch the app
|
| 70 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
app.launch()
|
|
|
|
| 1 |
"""
|
| 2 |
Main application for Dynamic Highscores system.
|
| 3 |
+
|
| 4 |
This file integrates all components into a unified application.
|
| 5 |
"""
|
| 6 |
|
|
|
|
| 8 |
import gradio as gr
|
| 9 |
import threading
|
| 10 |
import time
|
| 11 |
+
|
| 12 |
+
# Set essential OAuth environment variables
|
| 13 |
+
os.environ["OAUTH_CLIENT_ID"] = "hf"
|
| 14 |
+
os.environ["OAUTH_CLIENT_SECRET"] = "hf"
|
| 15 |
+
os.environ["OAUTH_AUTHORIZATION_URL"] = "https://huggingface.co/oauth/authorize"
|
| 16 |
+
os.environ["OAUTH_TOKEN_URL"] = "https://huggingface.co/oauth/token"
|
| 17 |
+
os.environ["OAUTH_SCOPES"] = "inference"
|
| 18 |
+
os.environ["OPENID_PROVIDER_URL"] = "https://huggingface.co"
|
| 19 |
+
|
| 20 |
from database_schema import DynamicHighscoresDB
|
| 21 |
from auth import HuggingFaceAuth, create_login_ui, setup_auth_handlers
|
| 22 |
from benchmark_selection import BenchmarkSelector, create_benchmark_selection_ui
|
|
|
|
| 37 |
if not benchmarks or len(benchmarks) == 0:
|
| 38 |
print("No benchmarks found. Adding sample benchmarks...")
|
| 39 |
try:
|
| 40 |
+
# Make sure the database path is clear
|
| 41 |
+
print(f"Database path: {db.db_path}")
|
| 42 |
+
|
| 43 |
+
# Import and call the function directly
|
| 44 |
num_added = add_sample_benchmarks()
|
| 45 |
print(f"Added {num_added} sample benchmarks.")
|
| 46 |
except Exception as e:
|
| 47 |
print(f"Error adding sample benchmarks: {str(e)}")
|
| 48 |
+
# Try direct DB insertion as fallback
|
| 49 |
+
try:
|
| 50 |
+
print("Attempting direct benchmark insertion...")
|
| 51 |
+
db.add_benchmark(
|
| 52 |
+
name="MMLU (Massive Multitask Language Understanding)",
|
| 53 |
+
dataset_id="cais/mmlu",
|
| 54 |
+
description="Tests knowledge across 57 subjects"
|
| 55 |
+
)
|
| 56 |
+
print("Added fallback benchmark.")
|
| 57 |
+
except Exception as inner_e:
|
| 58 |
+
print(f"Fallback insertion failed: {str(inner_e)}")
|
| 59 |
+
else:
|
| 60 |
+
print(f"Found {len(benchmarks)} existing benchmarks.")
|
| 61 |
|
| 62 |
+
# Custom CSS with theme awareness
|
| 63 |
css = """
|
| 64 |
+
/* Theme-adaptive colored info box */
|
| 65 |
.info-text {
|
| 66 |
background-color: rgba(53, 130, 220, 0.1);
|
| 67 |
padding: 12px;
|
|
|
|
| 69 |
border-left: 4px solid #3498db;
|
| 70 |
margin: 12px 0;
|
| 71 |
}
|
| 72 |
+
|
| 73 |
+
/* High-contrast text for elements - works in light and dark themes */
|
| 74 |
+
.info-text, .header, .footer, .tab-content,
|
| 75 |
+
button, input, textarea, select, option,
|
| 76 |
+
.gradio-container *, .markdown-text {
|
| 77 |
+
color: var(--text-color, inherit) !important;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
/* Container styling */
|
| 81 |
+
.container {
|
| 82 |
+
max-width: 1200px;
|
| 83 |
+
margin: 0 auto;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
/* Header styling */
|
| 87 |
+
.header {
|
| 88 |
+
text-align: center;
|
| 89 |
+
margin-bottom: 20px;
|
| 90 |
+
font-weight: bold;
|
| 91 |
+
font-size: 24px;
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
/* Footer styling */
|
| 95 |
+
.footer {
|
| 96 |
+
text-align: center;
|
| 97 |
+
margin-top: 40px;
|
| 98 |
+
padding: 20px;
|
| 99 |
+
border-top: 1px solid var(--border-color-primary, #eee);
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
/* Login section styling */
|
| 103 |
+
.login-section {
|
| 104 |
+
padding: 10px;
|
| 105 |
+
margin-bottom: 15px;
|
| 106 |
+
border-radius: 8px;
|
| 107 |
+
background-color: rgba(250, 250, 250, 0.1);
|
| 108 |
+
text-align: center;
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
/* Login button styling */
|
| 112 |
+
.login-button {
|
| 113 |
+
background-color: #4CAF50 !important;
|
| 114 |
+
color: white !important;
|
| 115 |
+
font-weight: bold;
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
/* Force high contrast on specific input areas */
|
| 119 |
+
input[type="text"], input[type="password"], textarea {
|
| 120 |
+
background-color: var(--background-fill-primary) !important;
|
| 121 |
+
color: var(--body-text-color) !important;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
/* Force text visibility in multiple contexts */
|
| 125 |
+
.gradio-markdown p, .gradio-markdown h1, .gradio-markdown h2,
|
| 126 |
+
.gradio-markdown h3, .gradio-markdown h4, .gradio-markdown li {
|
| 127 |
+
color: var(--body-text-color) !important;
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
/* Fix dark mode text visibility */
|
| 131 |
+
@media (prefers-color-scheme: dark) {
|
| 132 |
+
input, textarea, select {
|
| 133 |
+
color: #ffffff !important;
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
::placeholder {
|
| 137 |
+
color: rgba(255, 255, 255, 0.5) !important;
|
| 138 |
+
}
|
| 139 |
+
}
|
| 140 |
"""
|
| 141 |
|
| 142 |
+
# Check if the server is running in a HuggingFace Space
|
| 143 |
+
def is_running_in_hf_space():
|
| 144 |
+
return 'SPACE_ID' in os.environ
|
| 145 |
+
|
| 146 |
+
# Function to trigger HuggingFace OAuth login
|
| 147 |
+
def trigger_hf_oauth():
|
| 148 |
+
return """
|
| 149 |
+
<script>
|
| 150 |
+
// Redirect to HuggingFace Space login
|
| 151 |
+
window.location.href = window.location.origin + "?__space_auth_callback=true";
|
| 152 |
+
</script>
|
| 153 |
+
"""
|
| 154 |
+
|
| 155 |
+
# Start evaluation queue worker
|
| 156 |
+
def start_queue_worker():
|
| 157 |
+
# Wait a moment to ensure app is initialized
|
| 158 |
+
time.sleep(2)
|
| 159 |
+
try:
|
| 160 |
+
print("Starting evaluation queue worker...")
|
| 161 |
+
evaluation_queue.start_worker()
|
| 162 |
+
except Exception as e:
|
| 163 |
+
print(f"Error starting queue worker: {e}")
|
| 164 |
+
|
| 165 |
+
# Create Gradio app - without theme to avoid OAuth issues
|
| 166 |
with gr.Blocks(css=css, title="Dynamic Highscores") as app:
|
| 167 |
+
auth_status = gr.State(None) # Store user auth state
|
| 168 |
+
|
| 169 |
+
# Add a prominent login section at the top
|
| 170 |
+
with gr.Row(visible=True, elem_classes=["login-section"]) as login_section:
|
| 171 |
+
with gr.Column():
|
| 172 |
+
login_status = gr.Markdown("### π Not logged in", elem_id="login-status")
|
| 173 |
+
login_button = gr.Button("Login with HuggingFace", size="lg", variant="primary", elem_classes=["login-button"])
|
| 174 |
+
|
| 175 |
gr.Markdown("# π Dynamic Highscores", elem_classes=["header"])
|
| 176 |
gr.Markdown("""
|
| 177 |
Welcome to Dynamic Highscores - a community benchmark platform for evaluating and comparing language models.
|
|
|
|
| 192 |
|
| 193 |
with gr.TabItem("π Benchmarks", id=2):
|
| 194 |
benchmark_ui = create_benchmark_selection_ui(benchmark_selector, auth_manager)
|
| 195 |
+
|
| 196 |
+
gr.Markdown("""
|
| 197 |
+
### About Dynamic Highscores
|
| 198 |
+
|
| 199 |
+
This platform allows users to select benchmarks from HuggingFace datasets and evaluate models against them.
|
| 200 |
+
Each user can submit one benchmark per day (admin users are exempt from this limit).
|
| 201 |
+
All evaluations run on CPU only to ensure fair comparisons.
|
| 202 |
+
|
| 203 |
+
Created by Quazim0t0
|
| 204 |
+
""", elem_classes=["footer"])
|
| 205 |
+
|
| 206 |
+
# Auth status check on page load
|
| 207 |
+
def check_auth_on_load(request: gr.Request):
|
| 208 |
+
# Check if running in HF Space with OAuth
|
| 209 |
+
if is_running_in_hf_space():
|
| 210 |
+
username = request.headers.get("HF-User")
|
| 211 |
+
if username:
|
| 212 |
+
print(f"Detected logged-in user via Space OAuth: {username}")
|
| 213 |
+
# Get or create user
|
| 214 |
+
user = db.get_user_by_username(username)
|
| 215 |
+
if not user:
|
| 216 |
+
print(f"Creating new user: {username}")
|
| 217 |
+
is_admin = (username == "Quazim0t0") # Replace with your admin username
|
| 218 |
+
db.add_user(username, username, is_admin)
|
| 219 |
+
user = db.get_user_by_username(username)
|
| 220 |
+
|
| 221 |
+
# Update UI for logged in state
|
| 222 |
+
return user, f"### β
Logged in as {username}", gr.update(visible=False)
|
| 223 |
+
|
| 224 |
+
# Not logged in - make sure login section is visible
|
| 225 |
+
return None, "### π Not logged in", gr.update(visible=True)
|
| 226 |
+
|
| 227 |
+
# Connect event handlers
|
| 228 |
+
login_button.click(
|
| 229 |
+
fn=trigger_hf_oauth,
|
| 230 |
+
inputs=[],
|
| 231 |
+
outputs=[gr.HTML()]
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
# Check auth on load
|
| 235 |
+
app.load(
|
| 236 |
+
fn=check_auth_on_load,
|
| 237 |
+
inputs=[],
|
| 238 |
+
outputs=[auth_status, login_status, login_section]
|
| 239 |
+
)
|
| 240 |
|
| 241 |
# Launch the app
|
| 242 |
if __name__ == "__main__":
|
| 243 |
+
# Start queue worker in a separate thread
|
| 244 |
+
queue_thread = threading.Thread(target=start_queue_worker)
|
| 245 |
+
queue_thread.daemon = True
|
| 246 |
+
queue_thread.start()
|
| 247 |
+
|
| 248 |
app.launch()
|