openfree commited on
Commit
c26f143
ยท
verified ยท
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
+ """)