Spaces:
Running
Running
File size: 3,193 Bytes
1597f7e |
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 |
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 load_and_process_data():
# Hugging Face ๋ฐ์ดํฐ์
์์ parquet ํ์ผ ๋ค์ด๋ก๋
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)
# SQL ์ฟผ๋ฆฌ์ ๋์ผํ ์ฒ๋ฆฌ
df['createdAt'] = pd.to_datetime(df['createdAt'])
filtered_df = df[df['createdAt'] >= thirty_days_ago].copy()
filtered_df['created'] = filtered_df['createdAt'].dt.date
filtered_df = filtered_df.sort_values('trendingScore', ascending=False)
return filtered_df.head(100)
def create_trend_chart(selected_id, df):
if not selected_id:
return None
space_data = df[df['id'] == selected_id]
if space_data.empty:
return None
# ์ ํ๋ space์ ํธ๋ ๋ ์ฐจํธ ์์ฑ
fig = px.line(
space_data,
x='created',
y='trendingScore',
title=f'Trending Score for {selected_id}',
labels={'created': 'Date', 'trendingScore': 'Trending Score'}
)
fig.update_layout(
xaxis_title="Date",
yaxis_title="Trending Score",
hovermode='x unified',
plot_bgcolor='white',
paper_bgcolor='white'
)
return fig
def update_display(selected_id, df):
if selected_id is None:
return None, ""
space_data = df[df['id'] == selected_id].iloc[0]
info_text = f"""ID: {space_data['id']}
Created At: {space_data['createdAt'].strftime('%Y-%m-%d')}
Trending Score: {space_data['trendingScore']:.2f}"""
chart = create_trend_chart(selected_id, df)
return chart, info_text
# ๋ฐ์ดํฐ ๋ก๋
df = load_and_process_data()
# Gradio ์ธํฐํ์ด์ค ์์ฑ
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# Trending Spaces Dashboard")
with gr.Row():
# ์ผ์ชฝ ํจ๋ - ์คํ์ด์ค ๋ฆฌ์คํธ
with gr.Column(scale=1):
space_list = gr.Dropdown(
choices=[(row['id'], f"{row['id']} (Score: {row['trendingScore']:.2f})")
for _, row in df.iterrows()],
label="Select a Space",
info="Click to select a space and view its trend",
value=df['id'].iloc[0] if not df.empty else None
)
# ์คํ์ด์ค ์ ๋ณด ํ์
info_box = gr.Textbox(
label="Space Details",
value="",
interactive=False,
lines=3
)
# ์ค๋ฅธ์ชฝ ํจ๋ - ํธ๋ ๋ ์ฐจํธ
with gr.Column(scale=2):
trend_plot = gr.Plot(
label="Trending Score Over Time"
)
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
space_list.change(
fn=update_display,
inputs=[space_list],
outputs=[trend_plot, info_box]
)
# ๋์๋ณด๋ ์คํ
if __name__ == "__main__":
demo.launch() |