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(""" """, 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= 'Date: %{x}
' + 'Rank: %{y}
' + 'Space: ' + 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""" """, 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'Download CSV File' st.markdown(href, unsafe_allow_html=True) # 푸터 st.markdown(""" --- Made with ❤️ using Streamlit and HuggingFace API """)