lamhieu commited on
Commit
b0cb394
·
1 Parent(s): b8fd9fb

chore: update something

Browse files
lightweight_embeddings/__init__.py CHANGED
@@ -23,6 +23,9 @@ import gradio as gr
23
  import requests
24
  import json
25
  import logging
 
 
 
26
  from fastapi import FastAPI
27
  from fastapi.middleware.cors import CORSMiddleware
28
  from gradio.routes import mount_gradio_app
@@ -131,16 +134,55 @@ def call_embeddings_api(user_input: str, selected_model: str) -> str:
131
  return "❌ Failed to parse JSON from API response."
132
 
133
 
134
- def call_stats_api() -> str:
135
  """
136
  Calls the /v1/stats endpoint to retrieve analytics data.
137
- Returns the JSON response as a formatted string.
138
  """
139
  url = "https://lamhieu-lightweight-embeddings.hf.space/v1/stats"
 
 
140
  response = requests.get(url)
141
  if response.status_code != 200:
142
  raise ValueError(f"Failed to fetch stats: {response.text}")
143
- return json.dumps(response.json(), indent=2, ensure_ascii=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
 
145
 
146
  def create_main_interface():
@@ -224,13 +266,22 @@ def create_main_interface():
224
  """
225
  )
226
 
227
- # NEW STATS SECTION
228
  with gr.Accordion("Analytics Stats"):
229
  stats_btn = gr.Button("Get Stats")
230
- stats_json = gr.Textbox(
231
- label="Stats API Response", lines=10, interactive=False
 
 
 
 
 
 
 
 
 
 
232
  )
233
- stats_btn.click(fn=call_stats_api, inputs=[], outputs=stats_json)
234
 
235
  return demo
236
 
 
23
  import requests
24
  import json
25
  import logging
26
+ import pandas as pd
27
+ from typing import Tuple
28
+
29
  from fastapi import FastAPI
30
  from fastapi.middleware.cors import CORSMiddleware
31
  from gradio.routes import mount_gradio_app
 
134
  return "❌ Failed to parse JSON from API response."
135
 
136
 
137
+ def call_stats_api_df() -> Tuple[pd.DataFrame, pd.DataFrame]:
138
  """
139
  Calls the /v1/stats endpoint to retrieve analytics data.
140
+ Returns two DataFrames (access_df, tokens_df) constructed from the JSON response.
141
  """
142
  url = "https://lamhieu-lightweight-embeddings.hf.space/v1/stats"
143
+
144
+ # Fetch stats
145
  response = requests.get(url)
146
  if response.status_code != 200:
147
  raise ValueError(f"Failed to fetch stats: {response.text}")
148
+
149
+ data = response.json()
150
+ access_data = data["access"]
151
+ tokens_data = data["tokens"]
152
+
153
+ def build_stats_df(bucket: dict) -> pd.DataFrame:
154
+ """
155
+ Helper to build a DataFrame with columns: Model, total, daily, weekly, monthly, yearly.
156
+ bucket is a dictionary like data["access"] or data["tokens"] in the stats response.
157
+ """
158
+ all_models = set()
159
+ for time_range in ["total", "daily", "weekly", "monthly", "yearly"]:
160
+ all_models.update(bucket[time_range].keys())
161
+
162
+ # Prepare a data structure for DataFrame creation
163
+ result_dict = {
164
+ "Model": [],
165
+ "Total": [],
166
+ "Daily": [],
167
+ "Weekly": [],
168
+ "Monthly": [],
169
+ "Yearly": [],
170
+ }
171
+
172
+ for model in sorted(all_models):
173
+ result_dict["Model"].append(model)
174
+ result_dict["Total"].append(bucket["total"].get(model, 0))
175
+ result_dict["Daily"].append(bucket["daily"].get(model, 0))
176
+ result_dict["Weekly"].append(bucket["weekly"].get(model, 0))
177
+ result_dict["Monthly"].append(bucket["monthly"].get(model, 0))
178
+ result_dict["Yearly"].append(bucket["yearly"].get(model, 0))
179
+
180
+ df = pd.DataFrame(result_dict)
181
+ return df
182
+
183
+ access_df = build_stats_df(access_data)
184
+ tokens_df = build_stats_df(tokens_data)
185
+ return access_df, tokens_df
186
 
187
 
188
  def create_main_interface():
 
266
  """
267
  )
268
 
269
+ # STATS SECTION: display stats in tables
270
  with gr.Accordion("Analytics Stats"):
271
  stats_btn = gr.Button("Get Stats")
272
+ access_df = gr.DataFrame(
273
+ label="Access Stats",
274
+ headers=["Model", "Total", "Daily", "Weekly", "Monthly", "Yearly"],
275
+ interactive=False,
276
+ )
277
+ tokens_df = gr.DataFrame(
278
+ label="Token Stats",
279
+ headers=["Model", "Total", "Daily", "Weekly", "Monthly", "Yearly"],
280
+ interactive=False,
281
+ )
282
+ stats_btn.click(
283
+ fn=call_stats_api_df, inputs=[], outputs=[access_df, tokens_df]
284
  )
 
285
 
286
  return demo
287
 
requirements.txt CHANGED
@@ -4,6 +4,7 @@ uvicorn
4
  requests
5
  pydantic
6
  cachetools
 
7
  sentence-transformers[onnx]==3.3.1
8
  sentencepiece==0.2.0
9
  torch==2.4.0
 
4
  requests
5
  pydantic
6
  cachetools
7
+ pandas
8
  sentence-transformers[onnx]==3.3.1
9
  sentencepiece==0.2.0
10
  torch==2.4.0