openfree commited on
Commit
c26f143
β€’
1 Parent(s): 5ecb31f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +177 -0
app.py ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+ import seaborn as sns
5
+ from datetime import datetime, timedelta
6
+ import numpy as np
7
+ import streamlit as st
8
+ import plotly.graph_objects as go
9
+ import plotly.express as px
10
+ from PIL import Image
11
+ import io
12
+ import base64
13
+
14
+ # νŽ˜μ΄μ§€ μ„€μ •
15
+ st.set_page_config(layout="wide", page_title="HuggingFace Spaces Trending Analysis")
16
+
17
+ # μŠ€νƒ€μΌ 적용
18
+ st.markdown("""
19
+ <style>
20
+ .main {
21
+ background-color: #f5f5f5;
22
+ }
23
+ .stButton>button {
24
+ background-color: #ff4b4b;
25
+ color: white;
26
+ border-radius: 5px;
27
+ }
28
+ .trending-card {
29
+ padding: 20px;
30
+ border-radius: 10px;
31
+ background-color: white;
32
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
33
+ margin: 10px 0;
34
+ }
35
+ </style>
36
+ """, unsafe_allow_html=True)
37
+
38
+ # 타이틀
39
+ st.title("πŸ€— HuggingFace Spaces Trending Analysis")
40
+
41
+ # 관심 슀페이슀 URL λ¦¬μŠ€νŠΈμ™€ 정보
42
+ target_spaces = {
43
+ "ginipick/FLUXllama": "https://huggingface.co/spaces/ginipick/FLUXllama",
44
+ "ginipick/SORA-3D": "https://huggingface.co/spaces/ginipick/SORA-3D",
45
+ "fantaxy/Sound-AI-SFX": "https://huggingface.co/spaces/fantaxy/Sound-AI-SFX",
46
+ # ... [λ‚˜λ¨Έμ§€ μŠ€νŽ˜μ΄μŠ€λ“€λ„ λ™μΌν•œ ν˜•μ‹μœΌλ‘œ μΆ”κ°€]
47
+ "NCSOFT/VARCO_Arena": "https://huggingface.co/spaces/NCSOFT/VARCO_Arena"
48
+ }
49
+
50
+ def get_trending_spaces(date):
51
+ url = f"https://huggingface.co/api/spaces/trending?date={date}&limit=300"
52
+ response = requests.get(url)
53
+ if response.status_code == 200:
54
+ return response.json()
55
+ return None
56
+
57
+ def get_space_rank(spaces, space_id):
58
+ for idx, space in enumerate(spaces, 1):
59
+ if space.get('id', '') == space_id:
60
+ return idx
61
+ return None
62
+
63
+ # 데이터 μˆ˜μ§‘
64
+ @st.cache_data
65
+ def fetch_trending_data():
66
+ start_date = datetime(2023, 12, 1)
67
+ end_date = datetime(2023, 12, 31)
68
+ dates = [(start_date + timedelta(days=x)).strftime('%Y-%m-%d')
69
+ for x in range((end_date - start_date).days + 1)]
70
+
71
+ trending_data = {}
72
+ target_space_ranks = {space: [] for space in target_spaces.keys()}
73
+
74
+ with st.spinner('데이터λ₯Ό λΆˆλŸ¬μ˜€λŠ” 쀑...'):
75
+ for date in dates:
76
+ spaces = get_trending_spaces(date)
77
+ if spaces:
78
+ trending_data[date] = spaces
79
+ for space_id in target_spaces.keys():
80
+ rank = get_space_rank(spaces, space_id)
81
+ target_space_ranks[space_id].append(rank)
82
+
83
+ return trending_data, target_space_ranks, dates
84
+
85
+ trending_data, target_space_ranks, dates = fetch_trending_data()
86
+
87
+ # μ‹œκ°ν™”
88
+ st.header("πŸ“ˆ Trending Rank Changes")
89
+
90
+ # Plotlyλ₯Ό μ‚¬μš©ν•œ μΈν„°λž™ν‹°λΈŒ κ·Έλž˜ν”„
91
+ fig = go.Figure()
92
+
93
+ for space_id, ranks in target_space_ranks.items():
94
+ fig.add_trace(go.Scatter(
95
+ x=dates,
96
+ y=ranks,
97
+ name=space_id,
98
+ mode='lines+markers',
99
+ hovertemplate=
100
+ '<b>Date</b>: %{x}<br>' +
101
+ '<b>Rank</b>: %{y}<br>' +
102
+ '<b>Space</b>: ' + space_id
103
+ ))
104
+
105
+ fig.update_layout(
106
+ title='Trending Ranks Over Time',
107
+ xaxis_title='Date',
108
+ yaxis_title='Rank',
109
+ yaxis_autorange='reversed',
110
+ height=800,
111
+ template='plotly_white',
112
+ hovermode='x unified'
113
+ )
114
+
115
+ st.plotly_chart(fig, use_container_width=True)
116
+
117
+ # μ΅œμ‹  μˆœμœ„ 정보 좜λ ₯
118
+ st.header("πŸ† Latest Rankings")
119
+
120
+ latest_date = max(trending_data.keys())
121
+ latest_spaces = trending_data[latest_date]
122
+
123
+ cols = st.columns(3)
124
+ col_idx = 0
125
+
126
+ for space_id, url in target_spaces.items():
127
+ rank = get_space_rank(latest_spaces, space_id)
128
+ if rank:
129
+ space_info = next((s for s in latest_spaces if s['id'] == space_id), None)
130
+ if space_info:
131
+ with cols[col_idx % 3]:
132
+ with st.container():
133
+ st.markdown(f"""
134
+ <div class="trending-card">
135
+ <h3>#{rank} - {space_id}</h3>
136
+ <p>πŸ‘ Likes: {space_info.get('likes', 'N/A')}</p>
137
+ <p>πŸ“ {space_info.get('title', 'N/A')}</p>
138
+ <p>{space_info.get('description', 'N/A')[:100]}...</p>
139
+ <a href="{url}" target="_blank">Visit Space πŸ”—</a>
140
+ </div>
141
+ """, unsafe_allow_html=True)
142
+ col_idx += 1
143
+
144
+ # λ‹€μš΄λ‘œλ“œ κΈ°λŠ₯
145
+ st.header("πŸ“Š Download Data")
146
+
147
+ # DataFrame 생성
148
+ df_data = []
149
+ for date in dates:
150
+ spaces = trending_data.get(date, [])
151
+ for space_id in target_spaces.keys():
152
+ rank = get_space_rank(spaces, space_id)
153
+ if rank:
154
+ space_info = next((s for s in spaces if s['id'] == space_id), None)
155
+ if space_info:
156
+ df_data.append({
157
+ 'Date': date,
158
+ 'Space ID': space_id,
159
+ 'Rank': rank,
160
+ 'Likes': space_info.get('likes', 'N/A'),
161
+ 'Title': space_info.get('title', 'N/A'),
162
+ 'URL': target_spaces[space_id]
163
+ })
164
+
165
+ df = pd.DataFrame(df_data)
166
+
167
+ # CSV λ‹€μš΄λ‘œλ“œ λ²„νŠΌ
168
+ csv = df.to_csv(index=False)
169
+ b64 = base64.b64encode(csv.encode()).decode()
170
+ href = f'<a href="data:file/csv;base64,{b64}" download="trending_data.csv">Download CSV File</a>'
171
+ st.markdown(href, unsafe_allow_html=True)
172
+
173
+ # ν‘Έν„°
174
+ st.markdown("""
175
+ ---
176
+ Made with ❀️ using Streamlit and HuggingFace API
177
+ """)