nathanael-fijalkow commited on
Commit
b8f7c69
·
1 Parent(s): b460b06

add leaderboard

Browse files
Files changed (3) hide show
  1. README.md +1 -0
  2. app.py +96 -24
  3. requirements.txt +4 -0
README.md CHANGED
@@ -8,6 +8,7 @@ sdk_version: 6.2.0
8
  app_file: app.py
9
  pinned: false
10
  short_description: Two exercises (La Disparition and Toulouse cancelling)
 
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
8
  app_file: app.py
9
  pinned: false
10
  short_description: Two exercises (La Disparition and Toulouse cancelling)
11
+ hf_oauth: true
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -1,45 +1,117 @@
1
  import gradio as gr
2
  from gradio_client import Client, handle_file
 
3
  import os
 
 
 
4
 
5
- # 1. Configuration
6
  PRIVATE_SPACE_ID = "LLM-course/lipogram_private"
 
7
  HF_TOKEN = os.environ.get("HF_TOKEN")
 
8
 
9
- # 2. Connect to the Private Evaluator
10
- client = Client(PRIVATE_SPACE_ID, token=HF_TOKEN)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- def submit_challenge(file):
13
- if file is None:
14
- return "Please upload your 'challenge.py' file first."
15
 
16
  try:
17
- result = client.predict(
18
- file_obj=handle_file(file.name),
19
- api_name="/predict"
20
- )
21
- return result
 
 
 
 
 
 
 
 
 
 
 
22
  except Exception as e:
23
- return f"Error contacting evaluation server: {str(e)}"
24
 
25
- # 3. Simple User Interface
26
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
27
- gr.Markdown("# LLM Lipogram Submission")
28
- gr.Markdown("""
29
- Upload your `challenge.py` script below. It will be sent to a private
30
- evaluation server and tested against 20 hidden prompts.
31
- """)
32
 
33
- with gr.Row():
34
- file_input = gr.File(label="Upload challenge.py", file_types=[".py"])
35
- output_text = gr.Markdown(label="Evaluation Report")
36
-
37
- submit_btn = gr.Button("Submit for Grading", variant="primary")
38
 
 
 
 
 
 
 
 
 
 
 
39
  submit_btn.click(
40
  fn=submit_challenge,
41
  inputs=file_input,
42
- outputs=output_text
43
  )
44
 
45
  demo.launch()
 
1
  import gradio as gr
2
  from gradio_client import Client, handle_file
3
+ from huggingface_hub import hf_hub_download, upload_file
4
  import os
5
+ import time
6
+ import pandas as pd
7
+ from datetime import datetime
8
 
9
+ # --- CONFIGURATION ---
10
  PRIVATE_SPACE_ID = "LLM-course/lipogram_private"
11
+ DATASET_REPO_ID = "LLM-course/leaderboard-lipogram"
12
  HF_TOKEN = os.environ.get("HF_TOKEN")
13
+ LOCAL_CSV = "leaderboard.csv"
14
 
15
+ # --- DATABASE HELPERS ---
16
+ def sync_leaderboard():
17
+ """Download the latest leaderboard from the Private Dataset"""
18
+ try:
19
+ path = hf_hub_download(
20
+ repo_id=DATASET_REPO_ID,
21
+ filename="leaderboard.csv",
22
+ repo_type="dataset",
23
+ token=HF_TOKEN
24
+ )
25
+ return pd.read_csv(path)
26
+ except Exception:
27
+ # If file doesn't exist yet, return empty structure
28
+ return pd.DataFrame(columns=["Timestamp", "User", "Score", "Ex 1", "Ex 2"])
29
+
30
+ def save_score(user, score, ex1, ex2):
31
+ """Append new score and push back to Dataset"""
32
+ df = sync_leaderboard()
33
+ new_entry = {
34
+ "Timestamp": datetime.now().strftime("%Y-%m-%d %H:%M"),
35
+ "User": user,
36
+ "Score": score,
37
+ "Ex 1": ex1,
38
+ "Ex 2": ex2
39
+ }
40
+ df = pd.concat([df, pd.DataFrame([new_entry])], ignore_index=True)
41
+ df.to_csv(LOCAL_CSV, index=False)
42
+
43
+ # Push the updated file back to the private dataset
44
+ upload_file(
45
+ path_or_fileobj=LOCAL_CSV,
46
+ path_in_repo="leaderboard.csv",
47
+ repo_id=DATASET_REPO_ID,
48
+ repo_type="dataset",
49
+ token=HF_TOKEN
50
+ )
51
+ return df
52
+
53
+ # --- MAIN LOGIC ---
54
+ user_last_submission = {}
55
+
56
+ # ... (Keep all your previous imports and helper functions) ...
57
+
58
+ def submit_challenge(file, request: gr.Request):
59
+ # Check if the user is logged in via OAuth
60
+ if request.username is None:
61
+ raise gr.Error("Please 'Sign in with Hugging Face' at the top of the page to submit.")
62
+
63
+ user_name = request.username
64
+ user_key = f"{user_name}" # Use username as the unique key
65
+
66
+ # Rate Limiting (10 mins)
67
+ if user_key in user_last_submission and (time.time() - user_last_submission[user_key]) < 600:
68
+ raise gr.Error("One submission every 10 minutes allowed.")
69
 
70
+ if file is None: raise gr.Error("Please upload a file.")
71
+
72
+ gr.Info(f"Hello {user_name}, sending your code to the evaluator...")
73
 
74
  try:
75
+ client = Client(PRIVATE_SPACE_ID, token=HF_TOKEN)
76
+ result_text = client.predict(file_obj=handle_file(file.name), api_name="/predict")
77
+
78
+ # Parse the result from your Private Space output
79
+ try:
80
+ # Assumes format "Final Grade: XX.X%"
81
+ total_score = float(result_text.split('%')[0].split(':')[-1].strip())
82
+ except:
83
+ total_score = 0.0
84
+
85
+ # Save to your Private Dataset "DB"
86
+ updated_df = save_score(user_name, total_score, "Passed Check", "Passed Check")
87
+ user_last_submission[user_key] = time.time()
88
+
89
+ return result_text, updated_df.sort_values(by="Score", ascending=False)
90
+
91
  except Exception as e:
92
+ return f"Error: {str(e)}", sync_leaderboard()
93
 
94
+ # --- UPDATED UI ---
95
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
96
+ gr.Markdown("# LLM Lipogram Challenge Portal")
 
 
 
 
97
 
98
+ # This displays a login button if the user is not authenticated
99
+ gr.LoginButton()
 
 
 
100
 
101
+ with gr.Tabs():
102
+ with gr.TabItem("Submit"):
103
+ gr.Markdown("### 1. Sign in above\n### 2. Upload challenge.py below")
104
+ file_input = gr.File(label="challenge.py")
105
+ submit_btn = gr.Button("Evaluate My Code", variant="primary")
106
+ output_text = gr.Markdown()
107
+
108
+ with gr.TabItem("Leaderboard"):
109
+ leaderboard_df = gr.DataFrame(value=sync_leaderboard, interactive=False)
110
+
111
  submit_btn.click(
112
  fn=submit_challenge,
113
  inputs=file_input,
114
+ outputs=[output_text, leaderboard_df]
115
  )
116
 
117
  demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ gradio-client
3
+ huggingface-hub
4
+ pandas