Spaces:
Sleeping
Sleeping
zsolnai commited on
Commit Β·
1c33314
1
Parent(s): 9cda128
feat(ui): add handling complex json
Browse files
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 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
# Create the Gradio Interface
|
| 24 |
-
with gr.Blocks(theme=gr.themes.
|
| 25 |
gr.Markdown("# π GitHub Trend Predictor")
|
| 26 |
-
gr.Markdown("Visualizing trending repositories pushed from GitHub Actions.")
|
| 27 |
|
| 28 |
with gr.Row():
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 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()
|