File size: 10,681 Bytes
74dd3f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278

import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from datetime import datetime, timedelta
import random  # Only used for demo data, remove in production

def generate_demo_data(model_ids, days=30):
    """Generate demo data for visualization purposes"""
    today = datetime.now()
    data = []
    
    # For each model, generate 30 days of data
    for model_id in model_ids:
        base_downloads = random.randint(10, 1000)
        base_likes = random.randint(5, 200)
        
        # Generate daily data with some randomness and trend
        for i in range(days):
            date = (today - timedelta(days=days-i-1)).strftime("%Y-%m-%d")
            daily_downloads = max(1, int(base_downloads * (1 + 0.1 * i/days) * random.uniform(0.8, 1.2)))
            daily_likes = max(0, int(base_likes * (1 + 0.05 * i/days) * random.uniform(0.7, 1.3)))
            
            data.append({
                "model_id": model_id,
                "date": date,
                "downloads": base_downloads + daily_downloads,
                "likes": base_likes + daily_likes,
                "daily_downloads": daily_downloads,
                "daily_likes": daily_likes
            })
            
            # Update base values for next day (cumulative)
            base_downloads += daily_downloads
            base_likes += daily_likes
    
    return pd.DataFrame(data)

def render_analytics_page():
    st.title("πŸ“Š Model Performance Analytics")
    
    if "models" not in st.session_state or not st.session_state.models:
        st.info("No models found. Please create or import models first.")
        return
    
    # Get model IDs from the session state
    model_ids = [model.modelId for model in st.session_state.models]
    
    # Time period selection
    time_period = st.selectbox(
        "Select Time Period",
        ["Last 7 days", "Last 30 days", "Last 90 days", "All time"],
        index=1
    )
    
    # Convert time period to days
    days_mapping = {
        "Last 7 days": 7,
        "Last 30 days": 30,
        "Last 90 days": 90,
        "All time": 180  # Default to 6 months for demo
    }
    days = days_mapping[time_period]
    
    # In a real implementation, we would fetch this data from the Hugging Face API
    # For now, generate demo data
    df = generate_demo_data(model_ids, days)
    
    # Model selection for detailed view
    selected_models = st.multiselect(
        "Select Models to Compare",
        model_ids,
        default=model_ids[:min(3, len(model_ids))]
    )
    
    if not selected_models:
        st.warning("Please select at least one model to display analytics.")
        return
    
    # Filter data for selected models
    filtered_df = df[df["model_id"].isin(selected_models)]
    
    # Create tabs for different analytics views
    tab1, tab2, tab3, tab4 = st.tabs(["Downloads", "Likes", "Growth Rate", "Comparison"])
    
    with tab1:
        st.subheader("Downloads Over Time")
        
        # Cumulative downloads
        fig_cumulative = px.line(
            filtered_df, 
            x="date", 
            y="downloads", 
            color="model_id",
            title="Cumulative Downloads",
            labels={"downloads": "Total Downloads", "date": "Date", "model_id": "Model"}
        )
        st.plotly_chart(fig_cumulative, use_container_width=True)
        
        # Daily downloads
        fig_daily = px.bar(
            filtered_df, 
            x="date", 
            y="daily_downloads", 
            color="model_id",
            title="Daily Downloads",
            labels={"daily_downloads": "Daily Downloads", "date": "Date", "model_id": "Model"},
            barmode="group"
        )
        st.plotly_chart(fig_daily, use_container_width=True)
    
    with tab2:
        st.subheader("Likes Over Time")
        
        # Cumulative likes
        fig_cumulative = px.line(
            filtered_df, 
            x="date", 
            y="likes", 
            color="model_id",
            title="Cumulative Likes",
            labels={"likes": "Total Likes", "date": "Date", "model_id": "Model"}
        )
        st.plotly_chart(fig_cumulative, use_container_width=True)
        
        # Daily likes
        fig_daily = px.bar(
            filtered_df, 
            x="date", 
            y="daily_likes", 
            color="model_id",
            title="Daily Likes",
            labels={"daily_likes": "Daily Likes", "date": "Date", "model_id": "Model"},
            barmode="group"
        )
        st.plotly_chart(fig_daily, use_container_width=True)
    
    with tab3:
        st.subheader("Growth Metrics")
        
        # Calculate growth rates
        growth_data = []
        for model in selected_models:
            model_data = filtered_df[filtered_df["model_id"] == model]
            if len(model_data) >= 2:
                first_day = model_data.iloc[0]
                last_day = model_data.iloc[-1]
                
                # Calculate download growth
                if first_day["downloads"] > 0:
                    download_growth = (last_day["downloads"] - first_day["downloads"]) / first_day["downloads"] * 100
                else:
                    download_growth = 100 if last_day["downloads"] > 0 else 0
                
                # Calculate like growth
                if first_day["likes"] > 0:
                    like_growth = (last_day["likes"] - first_day["likes"]) / first_day["likes"] * 100
                else:
                    like_growth = 100 if last_day["likes"] > 0 else 0
                
                growth_data.append({
                    "model_id": model,
                    "download_growth": download_growth,
                    "like_growth": like_growth,
                    "downloads": last_day["downloads"],
                    "likes": last_day["likes"]
                })
        
        growth_df = pd.DataFrame(growth_data)
        
        # Show growth rates
        if not growth_df.empty:
            col1, col2 = st.columns(2)
            
            with col1:
                fig = px.bar(
                    growth_df,
                    x="model_id",
                    y="download_growth",
                    title="Download Growth Rate (%)",
                    labels={"download_growth": "Growth (%)", "model_id": "Model"},
                    color="download_growth",
                    color_continuous_scale=px.colors.sequential.Blues,
                )
                st.plotly_chart(fig, use_container_width=True)
            
            with col2:
                fig = px.bar(
                    growth_df,
                    x="model_id",
                    y="like_growth",
                    title="Like Growth Rate (%)",
                    labels={"like_growth": "Growth (%)", "model_id": "Model"},
                    color="like_growth",
                    color_continuous_scale=px.colors.sequential.Reds,
                )
                st.plotly_chart(fig, use_container_width=True)
        else:
            st.info("Not enough data to calculate growth rates.")
    
    with tab4:
        st.subheader("Model Comparison")
        
        # Get the most recent data point for each model
        latest_data = filtered_df.groupby("model_id").last().reset_index()
        
        # Create a radar chart for model comparison
        categories = ["downloads", "likes", "daily_downloads", "daily_likes"]
        fig = go.Figure()
        
        for model in latest_data["model_id"]:
            model_row = latest_data[latest_data["model_id"] == model].iloc[0]
            
            # Normalize values for radar chart (0-1 scale)
            max_vals = latest_data[categories].max()
            normalized_vals = [model_row[cat]/max_vals[cat] if max_vals[cat] > 0 else 0 for cat in categories]
            
            fig.add_trace(go.Scatterpolar(
                r=normalized_vals,
                theta=["Total Downloads", "Total Likes", "Daily Downloads", "Daily Likes"],
                fill='toself',
                name=model
            ))
        
        fig.update_layout(
            polar=dict(
                radialaxis=dict(
                    visible=True,
                    range=[0, 1]
                )),
            showlegend=True
        )
        
        st.plotly_chart(fig, use_container_width=True)
        
        # Comparison table
        st.subheader("Numeric Comparison")
        comparison_df = latest_data[["model_id", "downloads", "likes", "daily_downloads", "daily_likes"]]
        comparison_df.columns = ["Model", "Total Downloads", "Total Likes", "Daily Downloads", "Daily Likes"]
        st.dataframe(comparison_df, use_container_width=True)

    # Analytics insights
    st.subheader("πŸ“ˆ Key Insights")
    
    # Calculate some basic insights
    if not filtered_df.empty:
        # Most downloaded model
        most_downloaded = filtered_df.loc[filtered_df.groupby("model_id")["downloads"].idxmax()]
        # Fastest growing model in terms of downloads
        growth_rates = []
        for model in selected_models:
            model_data = filtered_df[filtered_df["model_id"] == model]
            if len(model_data) >= 2:
                first_downloads = model_data.iloc[0]["downloads"]
                last_downloads = model_data.iloc[-1]["downloads"]
                growth_rate = (last_downloads - first_downloads) / max(1, first_downloads)
                growth_rates.append((model, growth_rate))
        
        col1, col2 = st.columns(2)
        
        with col1:
            st.info(f"πŸ’‘ Most downloaded model: **{most_downloaded['model_id']}** with **{most_downloaded['downloads']}** total downloads")
            
            if growth_rates:
                fastest_growing = max(growth_rates, key=lambda x: x[1])
                st.info(f"πŸ’‘ Fastest growing model: **{fastest_growing[0]}** with a growth rate of **{fastest_growing[1]*100:.2f}%**")
        
        with col2:
            # Most liked model
            most_liked = filtered_df.loc[filtered_df.groupby("model_id")["likes"].idxmax()]
            st.info(f"πŸ’‘ Most liked model: **{most_liked['model_id']}** with **{most_liked['likes']}** total likes")
            
            # Average daily downloads
            avg_daily = filtered_df.groupby("model_id")["daily_downloads"].mean().reset_index()
            highest_avg = avg_daily.loc[avg_daily["daily_downloads"].idxmax()]
            st.info(f"πŸ’‘ Highest avg daily downloads: **{highest_avg['model_id']}** with **{highest_avg['daily_downloads']:.1f}** downloads/day")