Spaces:
Runtime error
Runtime error
File size: 5,161 Bytes
b29883b 1e4bf1a b29883b 407a575 b29883b 1e4bf1a b29883b 0926d14 34428f1 b29883b |
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 |
import gradio as gr
import requests
from datetime import datetime, timedelta
import pandas as pd
import plotly.express as px
def get_user_spaces(token):
"""์ฌ์ฉ์์ ๋ชจ๋ Space ๋ชฉ๋ก์ ๊ฐ์ ธ์ต๋๋ค."""
headers = {"Authorization": f"Bearer {token}"}
try:
# ์ฌ์ฉ์ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
user_info = requests.get("https://huggingface.co/api/whoami", headers=headers)
user_info.raise_for_status()
username = user_info.json().get('name')
if not username:
return None, "์ฌ์ฉ์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค."
# ์ฌ์ฉ์์ Space ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ
spaces_url = f"https://huggingface.co/api/spaces/{username}"
spaces_response = requests.get(spaces_url, headers=headers)
spaces_response.raise_for_status()
return username, spaces_response.json()
except requests.exceptions.RequestException as e:
return None, f"API ์์ฒญ ์คํจ: {str(e)}"
def get_space_visitors(owner, space_name, token):
"""ํน์ Space์ ๋ฐฉ๋ฌธ์ ํต๊ณ๋ฅผ ๊ฐ์ ธ์ต๋๋ค."""
url = f"https://huggingface.co/api/spaces/{owner}/{space_name}/metrics"
headers = {"Authorization": f"Bearer {token}"}
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
# ์ต๊ทผ 7์ผ๊ฐ์ ๋ฐ์ดํฐ๋ง ํํฐ๋ง
today = datetime.now()
week_ago = today - timedelta(days=7)
daily_visits = {}
total_visits = 0
for record in response.json():
date = datetime.fromtimestamp(record['timestamp'])
if date >= week_ago:
date_str = date.strftime('%Y-%m-%d')
visits = record.get('visits', 0)
daily_visits[date_str] = daily_visits.get(date_str, 0) + visits
total_visits += visits
return {
'name': space_name,
'total_visits': total_visits,
'daily_visits': daily_visits
}
except requests.exceptions.RequestException:
return None
def analyze_spaces(token):
"""๋ชจ๋ Space์ ๋ฐฉ๋ฌธ์ ํต๊ณ๋ฅผ ๋ถ์ํฉ๋๋ค."""
if not token:
return "API ํ ํฐ์ ์
๋ ฅํด์ฃผ์ธ์.", None, None
# ์ฌ์ฉ์ ์ ๋ณด์ Space ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ
username, spaces_data = get_user_spaces(token)
if not username:
return f"์ค๋ฅ: {spaces_data}", None, None
if not isinstance(spaces_data, list):
return "Space ๋ชฉ๋ก์ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.", None, None
# ๊ฐ Space์ ๋ฐฉ๋ฌธ์ ํต๊ณ ์์ง
all_stats = []
daily_data = []
for space in spaces_data:
space_name = space.get('id')
if space_name:
stats = get_space_visitors(username, space_name, token)
if stats:
all_stats.append(stats)
for date, visits in stats['daily_visits'].items():
daily_data.append({
'date': date,
'space': space_name,
'visits': visits
})
if not all_stats:
return "๋ฐฉ๋ฌธ์ ํต๊ณ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.", None, None
# ๊ฒฐ๊ณผ ํ
์คํธ ์์ฑ
result_text = f"๊ณ์ : {username}\n\n"
for stats in sorted(all_stats, key=lambda x: x['total_visits'], reverse=True):
result_text += f"Space: {stats['name']}\n"
result_text += f"์ด ๋ฐฉ๋ฌธ์ ์: {stats['total_visits']}๋ช
\n\n"
# ๊ทธ๋ํ ์์ฑ
if daily_data:
df = pd.DataFrame(daily_data)
fig = px.line(df, x='date', y='visits', color='space',
title=f'Space๋ณ ์ผ์ผ ๋ฐฉ๋ฌธ์ ์',
labels={'date': '๋ ์ง', 'visits': '๋ฐฉ๋ฌธ์ ์', 'space': 'Space ์ด๋ฆ'})
# ๋ง๋ ๊ทธ๋ํ ์์ฑ
summary_df = pd.DataFrame([(s['name'], s['total_visits']) for s in all_stats],
columns=['space', 'total_visits'])
summary_fig = px.bar(summary_df, x='space', y='total_visits',
title='Space๋ณ ์ด ๋ฐฉ๋ฌธ์ ์',
labels={'space': 'Space ์ด๋ฆ', 'total_visits': '์ด ๋ฐฉ๋ฌธ์ ์'})
return result_text, fig, summary_fig
return result_text, None, None
# Gradio ์ธํฐํ์ด์ค ์์ฑ
with gr.Blocks() as app:
gr.Markdown("""
# Hugging Face Space ๋ฐฉ๋ฌธ์ ํต๊ณ
API ํ ํฐ์ ์
๋ ฅํ๋ฉด ๋ชจ๋ Space์ ๋ฐฉ๋ฌธ์ ํต๊ณ๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
""")
with gr.Row():
token_input = gr.Textbox(label="API ํ ํฐ", type="password")
submit_btn = gr.Button("ํต๊ณ ํ์ธ")
stats_output = gr.Textbox(label="ํต๊ณ ์ ๋ณด", lines=10)
plot_output = gr.Plot(label="์ผ๋ณ ๋ฐฉ๋ฌธ์ ์")
summary_plot = gr.Plot(label="์ด ๋ฐฉ๋ฌธ์ ์")
submit_btn.click(
analyze_spaces,
inputs=[token_input],
outputs=[stats_output, plot_output, summary_plot]
)
if __name__ == "__main__":
app.launch() |