zsolnai commited on
Commit
1c33314
Β·
1 Parent(s): 9cda128

feat(ui): add handling complex json

Browse files
Files changed (1) hide show
  1. app.py +43 -17
app.py CHANGED
@@ -7,30 +7,56 @@ import pandas as pd
7
 
8
  def get_data():
9
  file_path = "predictions.json"
10
- if os.path.exists(file_path):
11
- try:
12
- with open(file_path, "r") as f:
13
- data = json.load(f)
14
- # Assuming predictions.json is a list of dicts
15
- return pd.DataFrame(data)
16
- except Exception as e:
17
- return pd.DataFrame({"Error": [f"Failed to parse JSON: {e}"]})
18
- return pd.DataFrame(
19
- {"Status": ["predictions.json not found. Run your GitHub Action to push data!"]}
20
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
 
23
  # Create the Gradio Interface
24
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
25
  gr.Markdown("# πŸš€ GitHub Trend Predictor")
26
- gr.Markdown("Visualizing trending repositories pushed from GitHub Actions.")
27
 
28
  with gr.Row():
29
- # This will automatically load the data when the page opens
30
- table = gr.Dataframe(value=get_data, interactive=False)
 
 
 
 
 
 
 
 
31
 
32
- refresh_btn = gr.Button("πŸ”„ Refresh Table")
33
- refresh_btn.click(fn=get_data, outputs=table)
34
 
35
  if __name__ == "__main__":
36
  demo.launch()
 
7
 
8
  def get_data():
9
  file_path = "predictions.json"
10
+ if not os.path.exists(file_path):
11
+ return (
12
+ pd.DataFrame({"Status": ["File not found yet. Run GitHub Action."]}),
13
+ "N/A",
14
+ )
15
+
16
+ try:
17
+ with open(file_path, "r") as f:
18
+ raw_data = json.load(f)
19
+
20
+ # 1. Extract the nested list
21
+ predictions_list = raw_data.get("predictions", [])
22
+ date = raw_data.get("prediction_date", "Unknown Date")
23
+
24
+ # 2. Convert to DataFrame
25
+ df = pd.DataFrame(predictions_list)
26
+
27
+ # 3. Clean up the data (Optional but recommended)
28
+ if not df.empty:
29
+ # Sort by highest probability
30
+ df = df.sort_values(by="probability", ascending=False)
31
+ # Format probability to 2 decimal percentage (e.g., 0.953 -> 95.39%)
32
+ df["probability"] = df["probability"].apply(lambda x: f"{x:.2%}")
33
+ # Rename columns for a nicer UI
34
+ df.columns = ["Repository", "Main Language", "Trend Probability"]
35
+
36
+ return df, date
37
+
38
+ except Exception as e:
39
+ return pd.DataFrame({"Error": [str(e)]}), "Error"
40
 
41
 
42
  # Create the Gradio Interface
43
+ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
44
  gr.Markdown("# πŸš€ GitHub Trend Predictor")
 
45
 
46
  with gr.Row():
47
+ date_display = gr.Textbox(label="Prediction Date", interactive=False)
48
+
49
+ with gr.Row():
50
+ # interactive=False removes the editing capability
51
+ table = gr.Dataframe(interactive=False)
52
+
53
+ refresh_btn = gr.Button("πŸ”„ Refresh Data")
54
+
55
+ # Load data on startup
56
+ demo.load(fn=get_data, outputs=[table, date_display])
57
 
58
+ # Reload on button click
59
+ refresh_btn.click(fn=get_data, outputs=[table, date_display])
60
 
61
  if __name__ == "__main__":
62
  demo.launch()