Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1 +1,106 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import plotly.express as px
|
| 4 |
+
from datetime import datetime, timedelta
|
| 5 |
+
import requests
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
|
| 8 |
+
def load_and_process_data():
|
| 9 |
+
# Hugging Face 데이터셋에서 parquet 파일 다운로드
|
| 10 |
+
url = "https://huggingface.co/datasets/cfahlgren1/hub-stats/resolve/main/spaces.parquet"
|
| 11 |
+
response = requests.get(url)
|
| 12 |
+
df = pd.read_parquet(BytesIO(response.content))
|
| 13 |
+
|
| 14 |
+
# 30일 전 날짜 계산
|
| 15 |
+
thirty_days_ago = datetime.now() - timedelta(days=30)
|
| 16 |
+
|
| 17 |
+
# SQL 쿼리와 동일한 처리
|
| 18 |
+
df['createdAt'] = pd.to_datetime(df['createdAt'])
|
| 19 |
+
filtered_df = df[df['createdAt'] >= thirty_days_ago].copy()
|
| 20 |
+
filtered_df['created'] = filtered_df['createdAt'].dt.date
|
| 21 |
+
filtered_df = filtered_df.sort_values('trendingScore', ascending=False)
|
| 22 |
+
|
| 23 |
+
return filtered_df.head(100)
|
| 24 |
+
|
| 25 |
+
def create_trend_chart(selected_id, df):
|
| 26 |
+
if not selected_id:
|
| 27 |
+
return None
|
| 28 |
+
|
| 29 |
+
space_data = df[df['id'] == selected_id]
|
| 30 |
+
if space_data.empty:
|
| 31 |
+
return None
|
| 32 |
+
|
| 33 |
+
# 선택된 space의 트렌드 차트 생성
|
| 34 |
+
fig = px.line(
|
| 35 |
+
space_data,
|
| 36 |
+
x='created',
|
| 37 |
+
y='trendingScore',
|
| 38 |
+
title=f'Trending Score for {selected_id}',
|
| 39 |
+
labels={'created': 'Date', 'trendingScore': 'Trending Score'}
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
fig.update_layout(
|
| 43 |
+
xaxis_title="Date",
|
| 44 |
+
yaxis_title="Trending Score",
|
| 45 |
+
hovermode='x unified',
|
| 46 |
+
plot_bgcolor='white',
|
| 47 |
+
paper_bgcolor='white'
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
return fig
|
| 51 |
+
|
| 52 |
+
def update_display(selected_id, df):
|
| 53 |
+
if selected_id is None:
|
| 54 |
+
return None, ""
|
| 55 |
+
|
| 56 |
+
space_data = df[df['id'] == selected_id].iloc[0]
|
| 57 |
+
info_text = f"""ID: {space_data['id']}
|
| 58 |
+
Created At: {space_data['createdAt'].strftime('%Y-%m-%d')}
|
| 59 |
+
Trending Score: {space_data['trendingScore']:.2f}"""
|
| 60 |
+
|
| 61 |
+
chart = create_trend_chart(selected_id, df)
|
| 62 |
+
|
| 63 |
+
return chart, info_text
|
| 64 |
+
|
| 65 |
+
# 데이터 로드
|
| 66 |
+
df = load_and_process_data()
|
| 67 |
+
|
| 68 |
+
# Gradio 인터페이스 생성
|
| 69 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 70 |
+
gr.Markdown("# Trending Spaces Dashboard")
|
| 71 |
+
|
| 72 |
+
with gr.Row():
|
| 73 |
+
# 왼쪽 패널 - 스페이스 리스트
|
| 74 |
+
with gr.Column(scale=1):
|
| 75 |
+
space_list = gr.Dropdown(
|
| 76 |
+
choices=[(row['id'], f"{row['id']} (Score: {row['trendingScore']:.2f})")
|
| 77 |
+
for _, row in df.iterrows()],
|
| 78 |
+
label="Select a Space",
|
| 79 |
+
info="Click to select a space and view its trend",
|
| 80 |
+
value=df['id'].iloc[0] if not df.empty else None
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
# 스페이스 정보 표시
|
| 84 |
+
info_box = gr.Textbox(
|
| 85 |
+
label="Space Details",
|
| 86 |
+
value="",
|
| 87 |
+
interactive=False,
|
| 88 |
+
lines=3
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
# 오른쪽 패널 - 트렌드 차트
|
| 92 |
+
with gr.Column(scale=2):
|
| 93 |
+
trend_plot = gr.Plot(
|
| 94 |
+
label="Trending Score Over Time"
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# 이벤트 핸들러
|
| 98 |
+
space_list.change(
|
| 99 |
+
fn=update_display,
|
| 100 |
+
inputs=[space_list],
|
| 101 |
+
outputs=[trend_plot, info_box]
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
# 대시보드 실행
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
demo.launch()
|