RichardErkhov commited on
Commit
22dbc6c
1 Parent(s): 9e2b8ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -64
app.py CHANGED
@@ -2,19 +2,20 @@ import gradio as gr
2
  import pandas as pd
3
  import requests
4
 
5
- CHUNK_SIZE = 1000
6
  DATA_URL = "https://erkhov.com/huggingspace_data"
7
  TIME_URL = "https://erkhov.com/huggingspace_time"
8
-
9
- def fetch_time():
10
- response = requests.get(TIME_URL)
11
- return response.text.strip() # assume the endpoint returns a raw timestamp or numeric value as string
12
 
13
  def fetch_data():
14
  response = requests.get(DATA_URL)
15
  data = response.json()
16
  return data
17
 
 
 
 
 
18
  def clickable(x, which_one):
19
  if x in ["Not Found", "Unknown"]:
20
  return "Not Found"
@@ -110,33 +111,8 @@ def filter_authors(authors_df, author_search_query, min_author_downloads, min_au
110
  filtered_authors = apply_author_filters(authors_df, author_search_query, min_author_downloads, min_author_likes)
111
  return filtered_authors
112
 
113
- def refresh_data(last_time, models_df_state, authors_df_state, stats_markdown, model_table, author_table):
114
- # Check if time changed
115
- current_time = fetch_time()
116
- if current_time != last_time and current_time != 0:
117
- # Time changed, re-fetch data
118
- data = fetch_data()
119
- models_df, authors_df = create_dataframes(data)
120
-
121
- total_models_count = data["total_models"]
122
- total_downloads = data["total_downloads"]
123
- total_likes = models_df["Likes"].sum() if "Likes" in models_df.columns else 0
124
-
125
- # Update stats markdown
126
- new_stats_markdown = f"""
127
- # GGUF Models and Authors Leaderboard
128
- **Total Models:** {total_models_count} | **Total Downloads (30d):** {total_downloads} | **Total Likes:** {total_likes}
129
- **Last Updated:** {current_time}
130
- """
131
-
132
- # Update states
133
- return current_time, models_df, authors_df, gr.update(value=new_stats_markdown), gr.update(value=models_df.iloc[:CHUNK_SIZE]), gr.update(value=authors_df)
134
- else:
135
- # No change
136
- return last_time, models_df_state, authors_df_state, stats_markdown, model_table, author_table
137
-
138
- # Initial fetch
139
- initial_time = fetch_time()
140
  data = fetch_data()
141
  all_models_df, authors_df = create_dataframes(data)
142
 
@@ -144,24 +120,22 @@ total_models_count = data["total_models"]
144
  total_downloads = data["total_downloads"]
145
  total_likes = all_models_df["Likes"].sum() if "Likes" in all_models_df.columns else 0
146
 
147
- initial_stats_markdown = f"""
148
- # GGUF Models and Authors Leaderboard
149
- **Total Models:** {total_models_count} | **Total Downloads (30d):** {total_downloads} | **Total Likes:** {total_likes}
150
- **Last Updated:** {initial_time}
151
- """
152
-
153
  with gr.Blocks() as demo:
154
  gr.Markdown(f"""
155
  # 🚀GGUF Tracker🚀
156
  Welcome to 🚀**GGUF Tracker**🚀, a live-updating leaderboard for all things GGUF on 🚀Hugging Face.
157
- Stats refresh every hour on the backend, but this interface checks every 5 minutes for updates.
158
 
159
  By the way, I’m 🚀Richard Erkhov, and you can check out more of what I’m working on at my [🌟**github**](https://github.com/RichardErkhov),
160
  [🌟**huggingface**](https://huggingface.co/RichardErkhov) or [🌟**erkhov.com**](https://erkhov.com). Go take a look—I think you’ll like what you find.
161
  """)
162
 
163
- stats_markdown = gr.Markdown(initial_stats_markdown)
164
-
 
 
 
 
165
  with gr.Tabs():
166
  with gr.TabItem("Models"):
167
  with gr.Row():
@@ -179,7 +153,7 @@ with gr.Blocks() as demo:
179
  )
180
  load_more_button = gr.Button("Load More Models")
181
 
182
- # States for models
183
  start_idx = gr.State(value=CHUNK_SIZE)
184
  filtered_df_state = gr.State(value=all_models_df)
185
 
@@ -211,26 +185,4 @@ with gr.Blocks() as demo:
211
  outputs=author_table
212
  )
213
 
214
- # States for refresh
215
- last_hf_time_state = gr.State(value=initial_time)
216
- models_df_state = gr.State(value=all_models_df)
217
- authors_df_state = gr.State(value=authors_df)
218
-
219
- # Timer to check every 5 minutes = 300 seconds
220
- timer = gr.Timer(interval=300, fn=refresh_data, inputs=[
221
- last_hf_time_state,
222
- models_df_state,
223
- authors_df_state,
224
- stats_markdown,
225
- model_table,
226
- author_table
227
- ], outputs=[
228
- last_hf_time_state,
229
- models_df_state,
230
- authors_df_state,
231
- stats_markdown,
232
- model_table,
233
- author_table
234
- ])
235
-
236
  demo.launch()
 
2
  import pandas as pd
3
  import requests
4
 
5
+ # Endpoints
6
  DATA_URL = "https://erkhov.com/huggingspace_data"
7
  TIME_URL = "https://erkhov.com/huggingspace_time"
8
+ CHUNK_SIZE = 1000
 
 
 
9
 
10
  def fetch_data():
11
  response = requests.get(DATA_URL)
12
  data = response.json()
13
  return data
14
 
15
+ def fetch_time():
16
+ response = requests.get(TIME_URL)
17
+ return response.text.strip()
18
+
19
  def clickable(x, which_one):
20
  if x in ["Not Found", "Unknown"]:
21
  return "Not Found"
 
111
  filtered_authors = apply_author_filters(authors_df, author_search_query, min_author_downloads, min_author_likes)
112
  return filtered_authors
113
 
114
+ # Fetch data once at start
115
+ last_updated = fetch_time()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  data = fetch_data()
117
  all_models_df, authors_df = create_dataframes(data)
118
 
 
120
  total_downloads = data["total_downloads"]
121
  total_likes = all_models_df["Likes"].sum() if "Likes" in all_models_df.columns else 0
122
 
 
 
 
 
 
 
123
  with gr.Blocks() as demo:
124
  gr.Markdown(f"""
125
  # 🚀GGUF Tracker🚀
126
  Welcome to 🚀**GGUF Tracker**🚀, a live-updating leaderboard for all things GGUF on 🚀Hugging Face.
127
+ Data is fetched once upon startup. To get new data, please restart the space.
128
 
129
  By the way, I’m 🚀Richard Erkhov, and you can check out more of what I’m working on at my [🌟**github**](https://github.com/RichardErkhov),
130
  [🌟**huggingface**](https://huggingface.co/RichardErkhov) or [🌟**erkhov.com**](https://erkhov.com). Go take a look—I think you’ll like what you find.
131
  """)
132
 
133
+ gr.Markdown(f"""
134
+ # GGUF Models and Authors Leaderboard
135
+ **Total Models:** {total_models_count} | **Total Downloads (30d):** {total_downloads} | **Total Likes:** {total_likes}
136
+ **Last Updated:** {last_updated}
137
+ """)
138
+
139
  with gr.Tabs():
140
  with gr.TabItem("Models"):
141
  with gr.Row():
 
153
  )
154
  load_more_button = gr.Button("Load More Models")
155
 
156
+ # States
157
  start_idx = gr.State(value=CHUNK_SIZE)
158
  filtered_df_state = gr.State(value=all_models_df)
159
 
 
185
  outputs=author_table
186
  )
187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
  demo.launch()