File size: 5,361 Bytes
c26f143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from datetime import datetime, timedelta
import numpy as np
import streamlit as st
import plotly.graph_objects as go
import plotly.express as px
from PIL import Image
import io
import base64

# νŽ˜μ΄μ§€ μ„€μ •
st.set_page_config(layout="wide", page_title="HuggingFace Spaces Trending Analysis")

# μŠ€νƒ€μΌ 적용
st.markdown("""
    <style>
    .main {
        background-color: #f5f5f5;
    }
    .stButton>button {
        background-color: #ff4b4b;
        color: white;
        border-radius: 5px;
    }
    .trending-card {
        padding: 20px;
        border-radius: 10px;
        background-color: white;
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        margin: 10px 0;
    }
    </style>
    """, unsafe_allow_html=True)

# 타이틀
st.title("πŸ€— HuggingFace Spaces Trending Analysis")

# 관심 슀페이슀 URL λ¦¬μŠ€νŠΈμ™€ 정보
target_spaces = {
    "ginipick/FLUXllama": "https://huggingface.co/spaces/ginipick/FLUXllama",
    "ginipick/SORA-3D": "https://huggingface.co/spaces/ginipick/SORA-3D",
    "fantaxy/Sound-AI-SFX": "https://huggingface.co/spaces/fantaxy/Sound-AI-SFX",
    # ... [λ‚˜λ¨Έμ§€ μŠ€νŽ˜μ΄μŠ€λ“€λ„ λ™μΌν•œ ν˜•μ‹μœΌλ‘œ μΆ”κ°€]
    "NCSOFT/VARCO_Arena": "https://huggingface.co/spaces/NCSOFT/VARCO_Arena"
}

def get_trending_spaces(date):
    url = f"https://huggingface.co/api/spaces/trending?date={date}&limit=300"
    response = requests.get(url)
    if response.status_code == 200:
        return response.json()
    return None

def get_space_rank(spaces, space_id):
    for idx, space in enumerate(spaces, 1):
        if space.get('id', '') == space_id:
            return idx
    return None

# 데이터 μˆ˜μ§‘
@st.cache_data
def fetch_trending_data():
    start_date = datetime(2023, 12, 1)
    end_date = datetime(2023, 12, 31)
    dates = [(start_date + timedelta(days=x)).strftime('%Y-%m-%d') 
             for x in range((end_date - start_date).days + 1)]
    
    trending_data = {}
    target_space_ranks = {space: [] for space in target_spaces.keys()}
    
    with st.spinner('데이터λ₯Ό λΆˆλŸ¬μ˜€λŠ” 쀑...'):
        for date in dates:
            spaces = get_trending_spaces(date)
            if spaces:
                trending_data[date] = spaces
                for space_id in target_spaces.keys():
                    rank = get_space_rank(spaces, space_id)
                    target_space_ranks[space_id].append(rank)
    
    return trending_data, target_space_ranks, dates

trending_data, target_space_ranks, dates = fetch_trending_data()

# μ‹œκ°ν™”
st.header("πŸ“ˆ Trending Rank Changes")

# Plotlyλ₯Ό μ‚¬μš©ν•œ μΈν„°λž™ν‹°λΈŒ κ·Έλž˜ν”„
fig = go.Figure()

for space_id, ranks in target_space_ranks.items():
    fig.add_trace(go.Scatter(
        x=dates,
        y=ranks,
        name=space_id,
        mode='lines+markers',
        hovertemplate=
        '<b>Date</b>: %{x}<br>' +
        '<b>Rank</b>: %{y}<br>' +
        '<b>Space</b>: ' + space_id
    ))

fig.update_layout(
    title='Trending Ranks Over Time',
    xaxis_title='Date',
    yaxis_title='Rank',
    yaxis_autorange='reversed',
    height=800,
    template='plotly_white',
    hovermode='x unified'
)

st.plotly_chart(fig, use_container_width=True)

# μ΅œμ‹  μˆœμœ„ 정보 좜λ ₯
st.header("πŸ† Latest Rankings")

latest_date = max(trending_data.keys())
latest_spaces = trending_data[latest_date]

cols = st.columns(3)
col_idx = 0

for space_id, url in target_spaces.items():
    rank = get_space_rank(latest_spaces, space_id)
    if rank:
        space_info = next((s for s in latest_spaces if s['id'] == space_id), None)
        if space_info:
            with cols[col_idx % 3]:
                with st.container():
                    st.markdown(f"""
                    <div class="trending-card">
                        <h3>#{rank} - {space_id}</h3>
                        <p>πŸ‘ Likes: {space_info.get('likes', 'N/A')}</p>
                        <p>πŸ“ {space_info.get('title', 'N/A')}</p>
                        <p>{space_info.get('description', 'N/A')[:100]}...</p>
                        <a href="{url}" target="_blank">Visit Space πŸ”—</a>
                    </div>
                    """, unsafe_allow_html=True)
            col_idx += 1

# λ‹€μš΄λ‘œλ“œ κΈ°λŠ₯
st.header("πŸ“Š Download Data")

# DataFrame 생성
df_data = []
for date in dates:
    spaces = trending_data.get(date, [])
    for space_id in target_spaces.keys():
        rank = get_space_rank(spaces, space_id)
        if rank:
            space_info = next((s for s in spaces if s['id'] == space_id), None)
            if space_info:
                df_data.append({
                    'Date': date,
                    'Space ID': space_id,
                    'Rank': rank,
                    'Likes': space_info.get('likes', 'N/A'),
                    'Title': space_info.get('title', 'N/A'),
                    'URL': target_spaces[space_id]
                })

df = pd.DataFrame(df_data)

# CSV λ‹€μš΄λ‘œλ“œ λ²„νŠΌ
csv = df.to_csv(index=False)
b64 = base64.b64encode(csv.encode()).decode()
href = f'<a href="data:file/csv;base64,{b64}" download="trending_data.csv">Download CSV File</a>'
st.markdown(href, unsafe_allow_html=True)

# ν‘Έν„°
st.markdown("""
---
Made with ❀️ using Streamlit and HuggingFace API
""")