arunasrivastava commited on
Commit
63b28b4
Β·
1 Parent(s): 1cff442

adding compatability for two spaces

Browse files
Files changed (1) hide show
  1. app.py +45 -44
app.py CHANGED
@@ -5,23 +5,24 @@ from pathlib import Path
5
  from datetime import datetime, timezone
6
  import time
7
  import logging
 
8
 
9
  logging.basicConfig(level=logging.INFO)
10
- BACKEND_URL = "http://127.0.0.1:8000"
11
  LAST_UPDATED = "Dec 4th 2024"
12
 
13
- def load_static_leaderboard():
14
- # read fake_queue/leaderboard.json
15
- return pd.read_json(Path("fake_queue/leaderboard.json"))
16
-
17
  def load_leaderboard_data():
18
  try:
19
- response = requests.get(f"{BACKEND_URL}/leaderboard")
20
  response.raise_for_status()
21
  return pd.DataFrame(response.json())
22
  except requests.RequestException as e:
23
  logging.error(f"Error loading leaderboard: {e}")
24
- return pd.DataFrame()
 
 
 
 
25
 
26
  def format_leaderboard_df(df):
27
  if df.empty:
@@ -37,34 +38,34 @@ def format_leaderboard_df(df):
37
 
38
  return display_df.sort_values("Average PER ⬇️")
39
 
40
- # def submit_evaluation(model_name, submission_name, github_url):
41
- # if not model_name or not submission_name:
42
- # return "⚠️ Please provide both model name and submission name."
43
 
44
- # request_data = {
45
- # "transcription_model": model_name,
46
- # "subset": "test",
47
- # "submission_name": submission_name,
48
- # "github_url": github_url if github_url else None
49
- # }
50
 
51
- # try:
52
- # response = requests.post(f"{BACKEND_URL}/evaluate", json=request_data)
53
- # response.raise_for_status()
54
- # task_id = response.json()["task_id"]
55
- # return f"βœ… Evaluation submitted successfully! Task ID: {task_id}"
56
- # except requests.RequestException as e:
57
- # return f"❌ Error: {str(e)}"
58
 
59
- # def check_task_status(task_id):
60
- # if not task_id:
61
- # return "Please enter a task ID"
62
- # try:
63
- # response = requests.get(f"{BACKEND_URL}/tasks/{task_id}")
64
- # response.raise_for_status()
65
- # return response.json()
66
- # except requests.RequestException as e:
67
- # return f"Error checking status: {str(e)}"
68
 
69
  def create_html_table(df):
70
  return df.to_html(escape=False, index=False, classes="styled-table")
@@ -95,10 +96,10 @@ with gr.Blocks(css="""
95
 
96
  with gr.Tabs():
97
  with gr.TabItem("πŸ† Leaderboard"):
98
- leaderboard_html = gr.HTML(create_html_table(format_leaderboard_df(load_static_leaderboard())))
99
  refresh_btn = gr.Button("πŸ”„ Refresh")
100
  refresh_btn.click(
101
- lambda: create_html_table(format_leaderboard_df(load_static_leaderboard())),
102
  outputs=leaderboard_html
103
  )
104
 
@@ -109,22 +110,22 @@ with gr.Blocks(css="""
109
  submit_btn = gr.Button("Submit")
110
  result = gr.Textbox(label="Submission Status")
111
 
112
- # submit_btn.click(
113
- # submit_evaluation,
114
- # inputs=[model_name, submission_name, github_url],
115
- # outputs=result
116
- # )
117
 
118
  with gr.TabItem("πŸ“Š Task Status"):
119
  task_id = gr.Textbox(label="Task ID")
120
  status_btn = gr.Button("Check Status")
121
  status_output = gr.JSON(label="Status")
122
 
123
- # status_btn.click(
124
- # check_task_status,
125
- # inputs=task_id,
126
- # outputs=status_output
127
- # )
128
 
129
  if __name__ == "__main__":
130
  demo.launch()
 
5
  from datetime import datetime, timezone
6
  import time
7
  import logging
8
+ import os
9
 
10
  logging.basicConfig(level=logging.INFO)
11
+ QUEUE_SPACE_URL = os.getenv('QUEUE_SPACE_URL', 'https://huggingface.co/spaces/KoelLabs/IPA-Transcription-EN-Queue').rstrip('/')
12
  LAST_UPDATED = "Dec 4th 2024"
13
 
 
 
 
 
14
  def load_leaderboard_data():
15
  try:
16
+ response = requests.get(f"{QUEUE_SPACE_URL}/api/leaderboard")
17
  response.raise_for_status()
18
  return pd.DataFrame(response.json())
19
  except requests.RequestException as e:
20
  logging.error(f"Error loading leaderboard: {e}")
21
+ # Fallback to static leaderboard if available
22
+ try:
23
+ return pd.read_json(Path("fake_queue/leaderboard.json"))
24
+ except:
25
+ return pd.DataFrame()
26
 
27
  def format_leaderboard_df(df):
28
  if df.empty:
 
38
 
39
  return display_df.sort_values("Average PER ⬇️")
40
 
41
+ def submit_evaluation(model_name, submission_name, github_url):
42
+ if not model_name or not submission_name:
43
+ return "⚠️ Please provide both model name and submission name."
44
 
45
+ request_data = {
46
+ "transcription_model": model_name,
47
+ "subset": "test",
48
+ "submission_name": submission_name,
49
+ "github_url": github_url if github_url else None
50
+ }
51
 
52
+ try:
53
+ response = requests.post(f"{QUEUE_SPACE_URL}/api/evaluate", json=request_data)
54
+ response.raise_for_status()
55
+ task_id = response.json()["task_id"]
56
+ return f"βœ… Evaluation submitted successfully! Task ID: {task_id}"
57
+ except requests.RequestException as e:
58
+ return f"❌ Error: {str(e)}"
59
 
60
+ def check_task_status(task_id):
61
+ if not task_id:
62
+ return "Please enter a task ID"
63
+ try:
64
+ response = requests.get(f"{QUEUE_SPACE_URL}/api/tasks/{task_id}")
65
+ response.raise_for_status()
66
+ return response.json()
67
+ except requests.RequestException as e:
68
+ return f"Error checking status: {str(e)}"
69
 
70
  def create_html_table(df):
71
  return df.to_html(escape=False, index=False, classes="styled-table")
 
96
 
97
  with gr.Tabs():
98
  with gr.TabItem("πŸ† Leaderboard"):
99
+ leaderboard_html = gr.HTML(create_html_table(format_leaderboard_df(load_leaderboard_data())))
100
  refresh_btn = gr.Button("πŸ”„ Refresh")
101
  refresh_btn.click(
102
+ lambda: create_html_table(format_leaderboard_df(load_leaderboard_data())),
103
  outputs=leaderboard_html
104
  )
105
 
 
110
  submit_btn = gr.Button("Submit")
111
  result = gr.Textbox(label="Submission Status")
112
 
113
+ submit_btn.click(
114
+ submit_evaluation,
115
+ inputs=[model_name, submission_name, github_url],
116
+ outputs=result
117
+ )
118
 
119
  with gr.TabItem("πŸ“Š Task Status"):
120
  task_id = gr.Textbox(label="Task ID")
121
  status_btn = gr.Button("Check Status")
122
  status_output = gr.JSON(label="Status")
123
 
124
+ status_btn.click(
125
+ check_task_status,
126
+ inputs=task_id,
127
+ outputs=status_output
128
+ )
129
 
130
  if __name__ == "__main__":
131
  demo.launch()