com3dian commited on
Commit
326967b
Β·
verified Β·
1 Parent(s): 5c24b19

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +38 -45
src/streamlit_app.py CHANGED
@@ -1,64 +1,57 @@
1
- import panel as pn
2
  import pandas as pd
3
  import os
4
  import datetime
5
- import io
6
 
7
  from google_sheet import fetch_leaderboard
8
  from google_drive import upload_to_drive
9
 
10
- pn.extension()
11
 
12
- # File upload widget
13
- file_input = pn.widgets.FileInput(accept='.zip', multiple=False)
14
-
15
- # Status message
16
- status = pn.pane.Markdown("")
17
-
18
- # Leaderboard display
19
- leaderboard = pn.pane.DataFrame(pd.DataFrame(), width=600)
 
20
 
21
- def submit_file(event):
22
- if file_input.value is None:
23
- status.object = "⚠️ Please upload a .zip file before submitting."
24
- return
25
 
26
- # Save uploaded file
27
  timestamp = datetime.datetime.now().isoformat().replace(":", "_")
28
- filename = f"{timestamp}_{file_input.filename}"
29
- submission_path = os.path.join("submissions", filename)
30
- os.makedirs("submissions", exist_ok=True)
31
  with open(submission_path, "wb") as f:
32
- f.write(file_input.value)
33
 
34
  try:
35
- drive_file_id = upload_to_drive(submission_path, filename)
36
- status.object = f"βœ… Uploaded to Google Drive [File ID: {drive_file_id}]"
37
  except Exception as e:
38
- status.object = f"❌ Failed to upload to Google Drive: {e}"
39
 
40
- # Update leaderboard
41
- try:
42
- df = fetch_leaderboard()
43
- if not df.empty:
44
- df_sorted = df.sort_values(by="score", ascending=False)
45
- leaderboard.object = df_sorted
46
- else:
47
- leaderboard.object = pd.DataFrame()
48
- except Exception as e:
49
- status.object += f"\n⚠️ Could not load leaderboard: {e}"
50
 
51
- submit_button = pn.widgets.Button(name="Submit", button_type="primary")
52
- submit_button.on_click(submit_file)
53
 
54
- # Layout
55
- app = pn.Column(
56
- "## πŸ† Hackathon Leaderboard",
57
- file_input,
58
- submit_button,
59
- status,
60
- "### Leaderboard",
61
- leaderboard
62
- )
63
 
64
- app.servable()
 
1
+ import gradio as gr
2
  import pandas as pd
3
  import os
4
  import datetime
 
5
 
6
  from google_sheet import fetch_leaderboard
7
  from google_drive import upload_to_drive
8
 
9
+ os.makedirs("submissions", exist_ok=True)
10
 
11
+ def get_leaderboard_html():
12
+ try:
13
+ df = fetch_leaderboard()
14
+ if df.empty:
15
+ return "<p>No submissions yet.</p>"
16
+ df_sorted = df.sort_values(by="score", ascending=False)
17
+ return df_sorted.to_html(index=False)
18
+ except Exception as e:
19
+ return f"<p>Could not load leaderboard: {e}</p>"
20
 
21
+ def handle_submission(file):
22
+ if file is None:
23
+ return "❌ No file uploaded.", "<p>No leaderboard to show.</p>"
 
24
 
 
25
  timestamp = datetime.datetime.now().isoformat().replace(":", "_")
26
+ submission_filename = f"{timestamp}_{file.name}"
27
+ submission_path = os.path.join("submissions", submission_filename)
28
+
29
  with open(submission_path, "wb") as f:
30
+ f.write(file.read())
31
 
32
  try:
33
+ drive_file_id = upload_to_drive(submission_path, submission_filename)
34
+ status = f"βœ… Uploaded to Google Drive [File ID: {drive_file_id}]"
35
  except Exception as e:
36
+ status = f"⚠️ Failed to upload to Google Drive: {e}"
37
 
38
+ leaderboard_html = get_leaderboard_html()
39
+ return status, leaderboard_html
40
+
41
+ with gr.Blocks(title="πŸ† Hackathon Leaderboard") as demo:
42
+ gr.Markdown("## πŸ† Hackathon Leaderboard")
43
+
44
+ with gr.Row():
45
+ file_input = gr.File(label="Upload your submission (.zip)", file_types=[".zip"])
46
+ submit_btn = gr.Button("Submit")
 
47
 
48
+ status_output = gr.Markdown()
49
+ leaderboard_output = gr.HTML(get_leaderboard_html())
50
 
51
+ submit_btn.click(
52
+ fn=handle_submission,
53
+ inputs=file_input,
54
+ outputs=[status_output, leaderboard_output]
55
+ )
 
 
 
 
56
 
57
+ demo.launch(share=True)