Delete app.py
Browse files
app.py
DELETED
@@ -1,284 +0,0 @@
|
|
1 |
-
"""
|
2 |
-
Main application for Dynamic Highscores system.
|
3 |
-
|
4 |
-
This file integrates all components into a unified application.
|
5 |
-
"""
|
6 |
-
|
7 |
-
import os
|
8 |
-
import gradio as gr
|
9 |
-
import threading
|
10 |
-
import time
|
11 |
-
from database_schema import DynamicHighscoresDB
|
12 |
-
from auth import HuggingFaceAuth
|
13 |
-
from benchmark_selection import BenchmarkSelector, create_benchmark_selection_ui
|
14 |
-
from evaluation_queue import EvaluationQueue, create_model_submission_ui
|
15 |
-
from leaderboard import Leaderboard, create_leaderboard_ui
|
16 |
-
from sample_benchmarks import add_sample_benchmarks
|
17 |
-
|
18 |
-
# Initialize components in main thread
|
19 |
-
db = DynamicHighscoresDB()
|
20 |
-
auth_manager = HuggingFaceAuth(db)
|
21 |
-
benchmark_selector = BenchmarkSelector(db, auth_manager)
|
22 |
-
evaluation_queue = EvaluationQueue(db, auth_manager)
|
23 |
-
leaderboard = Leaderboard(db)
|
24 |
-
|
25 |
-
# Initialize sample benchmarks if none exist
|
26 |
-
print("Checking for existing benchmarks...")
|
27 |
-
benchmarks = db.get_benchmarks()
|
28 |
-
if not benchmarks or len(benchmarks) == 0:
|
29 |
-
print("No benchmarks found. Adding sample benchmarks...")
|
30 |
-
try:
|
31 |
-
# Make sure the database path is clear
|
32 |
-
print(f"Database path: {db.db_path}")
|
33 |
-
|
34 |
-
# Import and call the function directly
|
35 |
-
num_added = add_sample_benchmarks()
|
36 |
-
print(f"Added {num_added} sample benchmarks.")
|
37 |
-
except Exception as e:
|
38 |
-
print(f"Error adding sample benchmarks: {str(e)}")
|
39 |
-
# Try direct DB insertion as fallback
|
40 |
-
try:
|
41 |
-
print("Attempting direct benchmark insertion...")
|
42 |
-
db.add_benchmark(
|
43 |
-
name="MMLU (Massive Multitask Language Understanding)",
|
44 |
-
dataset_id="cais/mmlu",
|
45 |
-
description="Tests knowledge across 57 subjects"
|
46 |
-
)
|
47 |
-
print("Added fallback benchmark.")
|
48 |
-
except Exception as inner_e:
|
49 |
-
print(f"Fallback insertion failed: {str(inner_e)}")
|
50 |
-
else:
|
51 |
-
print(f"Found {len(benchmarks)} existing benchmarks.")
|
52 |
-
|
53 |
-
# Custom CSS with theme awareness
|
54 |
-
css = """
|
55 |
-
/* Theme-adaptive colored info box */
|
56 |
-
.info-text {
|
57 |
-
background-color: rgba(53, 130, 220, 0.1);
|
58 |
-
padding: 12px;
|
59 |
-
border-radius: 8px;
|
60 |
-
border-left: 4px solid #3498db;
|
61 |
-
margin: 12px 0;
|
62 |
-
}
|
63 |
-
|
64 |
-
/* High-contrast text for elements - works in light and dark themes */
|
65 |
-
.info-text, .header, .footer, .tab-content,
|
66 |
-
button, input, textarea, select, option,
|
67 |
-
.gradio-container *, .markdown-text {
|
68 |
-
color: var(--text-color, inherit) !important;
|
69 |
-
}
|
70 |
-
|
71 |
-
/* Container styling */
|
72 |
-
.container {
|
73 |
-
max-width: 1200px;
|
74 |
-
margin: 0 auto;
|
75 |
-
}
|
76 |
-
|
77 |
-
/* Header styling */
|
78 |
-
.header {
|
79 |
-
text-align: center;
|
80 |
-
margin-bottom: 20px;
|
81 |
-
font-weight: bold;
|
82 |
-
font-size: 24px;
|
83 |
-
}
|
84 |
-
|
85 |
-
/* Footer styling */
|
86 |
-
.footer {
|
87 |
-
text-align: center;
|
88 |
-
margin-top: 40px;
|
89 |
-
padding: 20px;
|
90 |
-
border-top: 1px solid var(--border-color-primary, #eee);
|
91 |
-
}
|
92 |
-
|
93 |
-
/* Login section styling */
|
94 |
-
.login-section {
|
95 |
-
padding: 10px;
|
96 |
-
margin-bottom: 15px;
|
97 |
-
border-radius: 8px;
|
98 |
-
background-color: rgba(250, 250, 250, 0.1);
|
99 |
-
text-align: center;
|
100 |
-
}
|
101 |
-
|
102 |
-
/* Login button styling */
|
103 |
-
.login-button {
|
104 |
-
background-color: #4CAF50 !important;
|
105 |
-
color: white !important;
|
106 |
-
font-weight: bold;
|
107 |
-
}
|
108 |
-
|
109 |
-
/* Force high contrast on specific input areas */
|
110 |
-
input[type="text"], input[type="password"], textarea {
|
111 |
-
background-color: var(--background-fill-primary) !important;
|
112 |
-
color: var(--body-text-color) !important;
|
113 |
-
}
|
114 |
-
|
115 |
-
/* Force text visibility in multiple contexts */
|
116 |
-
.gradio-markdown p, .gradio-markdown h1, .gradio-markdown h2,
|
117 |
-
.gradio-markdown h3, .gradio-markdown h4, .gradio-markdown li {
|
118 |
-
color: var(--body-text-color) !important;
|
119 |
-
}
|
120 |
-
|
121 |
-
/* Fix dark mode text visibility */
|
122 |
-
@media (prefers-color-scheme: dark) {
|
123 |
-
input, textarea, select {
|
124 |
-
color: #ffffff !important;
|
125 |
-
}
|
126 |
-
|
127 |
-
::placeholder {
|
128 |
-
color: rgba(255, 255, 255, 0.5) !important;
|
129 |
-
}
|
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 |
-
|
194 |
-
# Simple manual authentication check
|
195 |
-
def check_user(request: gr.Request):
|
196 |
-
if request:
|
197 |
-
username = request.headers.get("HF-User")
|
198 |
-
if username:
|
199 |
-
# User is logged in via HF Spaces
|
200 |
-
print(f"User logged in: {username}")
|
201 |
-
user = db.get_user_by_username(username)
|
202 |
-
if not user:
|
203 |
-
# Create user if they don't exist
|
204 |
-
print(f"Creating new user: {username}")
|
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():
|
213 |
-
# Wait a moment to ensure app is initialized
|
214 |
-
time.sleep(2)
|
215 |
-
try:
|
216 |
-
print("Starting evaluation queue worker...")
|
217 |
-
evaluation_queue.start_worker()
|
218 |
-
except Exception as e:
|
219 |
-
print(f"Error starting queue worker: {e}")
|
220 |
-
|
221 |
-
# Create Gradio app
|
222 |
-
with gr.Blocks(css=css, title="Dynamic Highscores") as app:
|
223 |
-
# State to track user
|
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("""
|
241 |
-
*Not Active yet, Check back soon!* Welcome to Dynamic Highscores - a community benchmark platform for evaluating and comparing language models.
|
242 |
-
|
243 |
-
- **Add your own benchmarks** from HuggingFace datasets
|
244 |
-
- **Submit your models** for CPU-only evaluation
|
245 |
-
- **Compare performance** across different models and benchmarks
|
246 |
-
- **Filter results** by model type (Merge, Agent, Reasoning, Coding, etc.)
|
247 |
-
""", elem_classes=["info-text"])
|
248 |
-
|
249 |
-
# Main tabs
|
250 |
-
with gr.Tabs() as tabs:
|
251 |
-
with gr.TabItem("📊 Leaderboard", id=0):
|
252 |
-
leaderboard_ui = create_leaderboard_ui(leaderboard, db)
|
253 |
-
|
254 |
-
with gr.TabItem("🚀 Submit Model", id=1):
|
255 |
-
submission_ui = create_model_submission_ui(evaluation_queue, auth_manager, db)
|
256 |
-
|
257 |
-
with gr.TabItem("🔍 Benchmarks", id=2):
|
258 |
-
benchmark_ui = create_benchmark_selection_ui(benchmark_selector, auth_manager)
|
259 |
-
|
260 |
-
gr.Markdown("""
|
261 |
-
### About Dynamic Highscores
|
262 |
-
|
263 |
-
This platform allows users to select benchmarks from HuggingFace datasets and evaluate models against them.
|
264 |
-
Each user can submit one benchmark per day (admin users are exempt from this limit).
|
265 |
-
All evaluations run on CPU only to ensure fair comparisons.
|
266 |
-
|
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
|
278 |
-
if __name__ == "__main__":
|
279 |
-
# Start queue worker in a separate thread
|
280 |
-
queue_thread = threading.Thread(target=start_queue_worker)
|
281 |
-
queue_thread.daemon = True
|
282 |
-
queue_thread.start()
|
283 |
-
|
284 |
-
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|