Spaces:
Sleeping
Sleeping
zsolnai commited on
Commit Β·
5a78858
1
Parent(s): 525c082
ui: fix data missing issue
Browse files
app.py
CHANGED
|
@@ -6,20 +6,43 @@ import pandas as pd
|
|
| 6 |
import plotly.express as px
|
| 7 |
|
| 8 |
|
| 9 |
-
def get_data():
|
| 10 |
file_path = "predictions.json"
|
|
|
|
|
|
|
| 11 |
if not os.path.exists(file_path):
|
| 12 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
try:
|
| 15 |
with open(file_path, "r") as f:
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
predictions_list = raw_data.get("predictions", [])
|
| 19 |
date = raw_data.get("prediction_date", "Unknown Date")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
df = pd.DataFrame(predictions_list)
|
| 21 |
|
| 22 |
-
if not df.empty:
|
| 23 |
# Sort for Chart (Ascending for horizontal Plotly layout)
|
| 24 |
chart_df = df.sort_values(by="probability", ascending=True)
|
| 25 |
|
|
@@ -53,12 +76,22 @@ def get_data():
|
|
| 53 |
lambda x: f"{x:.1%}"
|
| 54 |
)
|
| 55 |
table_df = table_df[["repo_name", "language", "probability"]]
|
| 56 |
-
table_df.columns =
|
| 57 |
|
| 58 |
return table_df, f"### π
Last Prediction Run: {date}", fig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
except Exception as e:
|
| 61 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
|
| 64 |
# CSS to center the markdown text since 'style' is no longer allowed in components
|
|
|
|
| 6 |
import plotly.express as px
|
| 7 |
|
| 8 |
|
| 9 |
+
def get_data(*args, **kwargs):
|
| 10 |
file_path = "predictions.json"
|
| 11 |
+
table_columns = ["Repository", "Language", "Trend Probability"]
|
| 12 |
+
|
| 13 |
if not os.path.exists(file_path):
|
| 14 |
+
return (
|
| 15 |
+
pd.DataFrame(columns=table_columns),
|
| 16 |
+
"π
Status: Waiting for data sync...",
|
| 17 |
+
None,
|
| 18 |
+
)
|
| 19 |
|
| 20 |
try:
|
| 21 |
with open(file_path, "r") as f:
|
| 22 |
+
# Handle empty file case
|
| 23 |
+
content = f.read()
|
| 24 |
+
if not content:
|
| 25 |
+
return (
|
| 26 |
+
pd.DataFrame(columns=table_columns),
|
| 27 |
+
"π
Status: predictions.json is empty.",
|
| 28 |
+
None,
|
| 29 |
+
)
|
| 30 |
+
raw_data = json.loads(content)
|
| 31 |
|
| 32 |
predictions_list = raw_data.get("predictions", [])
|
| 33 |
date = raw_data.get("prediction_date", "Unknown Date")
|
| 34 |
+
|
| 35 |
+
# Handle case where 'predictions' key is missing or not a list
|
| 36 |
+
if not isinstance(predictions_list, list):
|
| 37 |
+
return (
|
| 38 |
+
pd.DataFrame(columns=table_columns),
|
| 39 |
+
f"### β οΈ Error: 'predictions' format is invalid.",
|
| 40 |
+
None,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
df = pd.DataFrame(predictions_list)
|
| 44 |
|
| 45 |
+
if not df.empty and all(col in df.columns for col in ['repo_name', 'language', 'probability']):
|
| 46 |
# Sort for Chart (Ascending for horizontal Plotly layout)
|
| 47 |
chart_df = df.sort_values(by="probability", ascending=True)
|
| 48 |
|
|
|
|
| 76 |
lambda x: f"{x:.1%}"
|
| 77 |
)
|
| 78 |
table_df = table_df[["repo_name", "language", "probability"]]
|
| 79 |
+
table_df.columns = table_columns
|
| 80 |
|
| 81 |
return table_df, f"### π
Last Prediction Run: {date}", fig
|
| 82 |
+
else:
|
| 83 |
+
return (
|
| 84 |
+
pd.DataFrame(columns=table_columns),
|
| 85 |
+
f"### π
Last Prediction Run: {date} (No valid data)",
|
| 86 |
+
None,
|
| 87 |
+
)
|
| 88 |
|
| 89 |
+
except (json.JSONDecodeError, Exception) as e:
|
| 90 |
+
return (
|
| 91 |
+
pd.DataFrame(columns=table_columns),
|
| 92 |
+
f"### β οΈ Error loading data: {e}",
|
| 93 |
+
None,
|
| 94 |
+
)
|
| 95 |
|
| 96 |
|
| 97 |
# CSS to center the markdown text since 'style' is no longer allowed in components
|