Spaces:
Running
Running
| import gradio as gr | |
| import pandas as pd | |
| import plotly.express as px | |
| from datetime import datetime, timedelta | |
| import requests | |
| from io import BytesIO | |
| def create_trend_chart(space_id, daily_ranks_df): | |
| if space_id is None or daily_ranks_df.empty: | |
| return None | |
| try: | |
| # ํน์ space์ ๋ฐ์ดํฐ๋ง ํํฐ๋ง | |
| space_data = daily_ranks_df[daily_ranks_df['id'] == space_id].copy() | |
| if space_data.empty: | |
| return None | |
| # ๋ฐ์ดํฐ ์ ๋ ฌ | |
| space_data = space_data.sort_values('date') | |
| fig = px.line( | |
| space_data, | |
| x='date', | |
| y='rank', | |
| title=f'Daily Rank Trend for {space_id}', | |
| labels={'date': 'Date', 'rank': 'Rank'}, | |
| markers=True | |
| ) | |
| fig.update_layout( | |
| xaxis_title="Date", | |
| yaxis_title="Rank", | |
| yaxis=dict( | |
| range=[100, 1], # 100์๋ถํฐ 1์๊น์ง (์ญ์์ผ๋ก ์ค์ ) | |
| tickmode='linear', # ์ ํ ๊ฐ๊ฒฉ์ผ๋ก ๋๊ธ ํ์ | |
| tick0=1, # ์ฒซ ๋๊ธ | |
| dtick=10 # ๋๊ธ ๊ฐ๊ฒฉ (10๋จ์๋ก ํ์) | |
| ), | |
| hovermode='x unified', | |
| plot_bgcolor='white', | |
| paper_bgcolor='white', | |
| showlegend=False | |
| ) | |
| # ๊ฒฉ์ ์ถ๊ฐ | |
| fig.update_xaxes(showgrid=True, gridwidth=1, gridcolor='lightgray') | |
| fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='lightgray') | |
| # ๋ผ์ธ ์คํ์ผ ์์ | |
| fig.update_traces( | |
| line_color='#2563eb', | |
| line_width=2, | |
| marker=dict(size=8, color='#2563eb') | |
| ) | |
| return fig | |
| except Exception as e: | |
| print(f"Error creating chart: {e}") | |
| return None | |
| def load_and_process_data(): | |
| try: | |
| url = "https://huggingface.co/datasets/cfahlgren1/hub-stats/resolve/main/spaces.parquet" | |
| response = requests.get(url) | |
| df = pd.read_parquet(BytesIO(response.content)) | |
| # 30์ผ์น ๋ฐ์ดํฐ ์ค๋น | |
| thirty_days_ago = datetime.now() - timedelta(days=30) | |
| df['createdAt'] = pd.to_datetime(df['createdAt']) | |
| df = df[df['createdAt'] >= thirty_days_ago].copy() | |
| # ๋ ์ง๋ณ ๋ฐ์ดํฐ ์ฒ๋ฆฌ | |
| dates = pd.date_range(start=thirty_days_ago, end=datetime.now(), freq='D') | |
| daily_ranks = [] | |
| for date in dates: | |
| # ํด๋น ๋ ์ง์ ๋ฐ์ดํฐ ์ถ์ถ | |
| date_data = df[df['createdAt'].dt.date <= date.date()].copy() | |
| # trendingScore๊ฐ ๊ฐ์ ๊ฒฝ์ฐ id๋ก ์ ๋ ฌํ์ฌ ์ ๋ํฌํ ์์ ๋ณด์ฅ | |
| date_data = date_data.sort_values(['trendingScore', 'id'], ascending=[False, True]) | |
| # ์์ ๊ณ์ฐ | |
| date_data['rank'] = range(1, len(date_data) + 1) | |
| date_data['date'] = date.date() | |
| # ํ์ํ ์ปฌ๋ผ๋ง ์ ํ | |
| daily_ranks.append( | |
| date_data[['id', 'date', 'rank', 'trendingScore', 'createdAt']] | |
| ) | |
| # ์ ์ฒด ๋ฐ์ดํฐ ๋ณํฉ | |
| daily_ranks_df = pd.concat(daily_ranks, ignore_index=True) | |
| # ์ต์ ๋ ์ง์ top 100 ์ถ์ถ | |
| latest_date = daily_ranks_df['date'].max() | |
| top_100_spaces = daily_ranks_df[ | |
| daily_ranks_df['date'] == latest_date | |
| ].sort_values('rank').head(100).copy() | |
| return daily_ranks_df, top_100_spaces | |
| except Exception as e: | |
| print(f"Error loading data: {e}") | |
| return pd.DataFrame(), pd.DataFrame() | |
| def update_display(selection): | |
| global daily_ranks_df | |
| if not selection: | |
| return None, "Please select a space" | |
| try: | |
| # ์ ํ๋ ํญ๋ชฉ์์ space ID ์ถ์ถ | |
| space_id = selection.split(': ')[1].split(' (Score')[0] | |
| # ์ต์ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ | |
| latest_data = daily_ranks_df[ | |
| daily_ranks_df['id'] == space_id | |
| ].sort_values('date').iloc[-1] | |
| info_text = f"""ID: {space_id} | |
| Current Rank: {int(latest_data['rank'])} | |
| Trending Score: {latest_data['trendingScore']:.2f} | |
| Created At: {latest_data['createdAt'].strftime('%Y-%m-%d')}""" | |
| chart = create_trend_chart(space_id, daily_ranks_df) | |
| return chart, info_text | |
| except Exception as e: | |
| print(f"Error in update_display: {e}") | |
| return None, f"Error processing data: {str(e)}" | |
| # ๋ฐ์ดํฐ ๋ก๋ | |
| print("Loading initial data...") | |
| daily_ranks_df, top_100_spaces = load_and_process_data() | |
| print("Data loaded.") | |
| # Gradio ์ธํฐํ์ด์ค ์์ฑ | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# Trending Spaces Dashboard") | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| # ์์๊ฐ ํฌํจ๋ ๋ฆฌ์คํธ๋ก ํ์ | |
| space_choices = [ | |
| f"Rank {row['rank']}: {row['id']} (Score: {row['trendingScore']:.2f})" | |
| for _, row in top_100_spaces.iterrows() | |
| ] | |
| space_list = gr.Radio( | |
| choices=space_choices, | |
| label="Top 100 Trending Spaces", | |
| info="Select a space to view its rank trend", | |
| value=space_choices[0] if space_choices else None | |
| ) | |
| info_box = gr.Textbox( | |
| label="Space Details", | |
| value="", | |
| interactive=False, | |
| lines=4 | |
| ) | |
| with gr.Column(scale=2): | |
| trend_plot = gr.Plot( | |
| label="Daily Rank Trend" | |
| ) | |
| space_list.change( | |
| fn=update_display, | |
| inputs=[space_list], | |
| outputs=[trend_plot, info_box] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(share=True) |