Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,6 @@ import requests
|
|
2 |
import gradio as gr
|
3 |
from datetime import datetime
|
4 |
|
5 |
-
# ํ
์คํธ๋ฅผ ์ํด Gradio ๊ณต์ ๊ณ์ ์ผ๋ก ์ค์
|
6 |
USERNAME = "openfree"
|
7 |
|
8 |
def format_timestamp(timestamp):
|
@@ -13,57 +12,63 @@ def format_timestamp(timestamp):
|
|
13 |
|
14 |
def get_space_card(space):
|
15 |
"""Generate HTML card for a space"""
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
return f"""
|
17 |
-
<div style='border: 1px solid #ddd; padding:
|
18 |
-
background-color: white; box-shadow: 2px 2px
|
19 |
transition: transform 0.2s ease-in-out;'
|
20 |
onmouseover='this.style.transform="scale(1.02)"'
|
21 |
onmouseout='this.style.transform="scale(1)"'>
|
22 |
-
<h3 style='color: #2d2d2d; margin: 0 0
|
23 |
-
<a href='{
|
24 |
style='text-decoration: none; color: #2d2d2d;'>
|
25 |
-
{
|
26 |
</a>
|
27 |
</h3>
|
28 |
-
<
|
29 |
-
|
30 |
-
<
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
View Space
|
34 |
</a>
|
|
|
|
|
|
|
35 |
</div>
|
36 |
</div>
|
37 |
"""
|
38 |
|
39 |
def get_user_spaces():
|
40 |
-
|
41 |
-
url = f"https://huggingface.co/api/spaces?search={USERNAME}"
|
42 |
headers = {
|
43 |
"Accept": "application/json",
|
44 |
-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
|
45 |
}
|
46 |
|
47 |
try:
|
48 |
response = requests.get(url, headers=headers)
|
49 |
-
print(f"Status Code: {response.status_code}")
|
50 |
-
print(f"Response: {response.text[:500]}...") # ๋๋ฒ๊น
์ฉ
|
51 |
|
52 |
if response.status_code != 200:
|
53 |
return f"Error: Failed to fetch spaces (Status Code: {response.status_code})"
|
54 |
|
55 |
spaces_data = response.json()
|
56 |
|
57 |
-
#
|
58 |
-
user_spaces = []
|
59 |
-
for space in spaces_data:
|
60 |
-
if space.get('author', {}).get('name', '').lower() == USERNAME.lower():
|
61 |
-
space_info = {
|
62 |
-
"title": space.get('id', '').split('/')[-1],
|
63 |
-
"description": space.get('description', ''),
|
64 |
-
"url": f"https://huggingface.co/spaces/{space.get('id', '')}",
|
65 |
-
}
|
66 |
-
user_spaces.append(space_info)
|
67 |
|
68 |
if not user_spaces:
|
69 |
return f"""
|
@@ -74,11 +79,15 @@ def get_user_spaces():
|
|
74 |
</div>
|
75 |
"""
|
76 |
|
|
|
|
|
|
|
77 |
# Create HTML grid layout
|
78 |
html_content = f"""
|
79 |
<div style='padding: 20px; background-color: #f5f5f5;'>
|
80 |
<div style='margin-bottom: 20px;'>
|
81 |
-
<
|
|
|
82 |
</div>
|
83 |
<div style='
|
84 |
display: grid;
|
@@ -93,12 +102,12 @@ def get_user_spaces():
|
|
93 |
return html_content
|
94 |
|
95 |
except Exception as e:
|
96 |
-
print(f"Error: {str(e)}")
|
97 |
return f"""
|
98 |
<div style='padding: 20px; text-align: center; color: #666;'>
|
99 |
<h2>Error occurred while fetching spaces</h2>
|
100 |
<p>Error details: {str(e)}</p>
|
101 |
-
<p>Please try again later
|
102 |
</div>
|
103 |
"""
|
104 |
|
@@ -107,7 +116,7 @@ app = gr.Interface(
|
|
107 |
fn=get_user_spaces,
|
108 |
inputs=None,
|
109 |
outputs=gr.HTML(),
|
110 |
-
title=f"Hugging Face
|
111 |
description=f"Displays public Spaces from {USERNAME}",
|
112 |
theme=gr.themes.Soft(),
|
113 |
css="""
|
@@ -117,6 +126,6 @@ app = gr.Interface(
|
|
117 |
"""
|
118 |
)
|
119 |
|
120 |
-
# Launch the Gradio app
|
121 |
if __name__ == "__main__":
|
122 |
-
app.launch(
|
|
|
2 |
import gradio as gr
|
3 |
from datetime import datetime
|
4 |
|
|
|
5 |
USERNAME = "openfree"
|
6 |
|
7 |
def format_timestamp(timestamp):
|
|
|
12 |
|
13 |
def get_space_card(space):
|
14 |
"""Generate HTML card for a space"""
|
15 |
+
space_id = space.get('id', '')
|
16 |
+
space_name = space_id.split('/')[-1]
|
17 |
+
likes = space.get('likes', 0)
|
18 |
+
created_at = format_timestamp(space.get('createdAt'))
|
19 |
+
sdk = space.get('sdk', 'N/A')
|
20 |
+
|
21 |
return f"""
|
22 |
+
<div style='border: 1px solid #ddd; padding: 20px; margin: 10px; border-radius: 12px;
|
23 |
+
background-color: white; box-shadow: 2px 2px 10px rgba(0,0,0,0.1);
|
24 |
transition: transform 0.2s ease-in-out;'
|
25 |
onmouseover='this.style.transform="scale(1.02)"'
|
26 |
onmouseout='this.style.transform="scale(1)"'>
|
27 |
+
<h3 style='color: #2d2d2d; margin: 0 0 15px 0; font-size: 1.3em;'>
|
28 |
+
<a href='https://huggingface.co/spaces/{space_id}' target='_blank'
|
29 |
style='text-decoration: none; color: #2d2d2d;'>
|
30 |
+
{space_name}
|
31 |
</a>
|
32 |
</h3>
|
33 |
+
<div style='margin: 10px 0; color: #666;'>
|
34 |
+
<p style='margin: 5px 0;'><strong>SDK:</strong> {sdk}</p>
|
35 |
+
<p style='margin: 5px 0;'><strong>Created:</strong> {created_at}</p>
|
36 |
+
<p style='margin: 5px 0;'><strong>Likes:</strong> {likes} โค๏ธ</p>
|
37 |
+
</div>
|
38 |
+
<div style='margin-top: 15px; display: flex; justify-content: space-between; align-items: center;'>
|
39 |
+
<a href='https://huggingface.co/spaces/{space_id}' target='_blank'
|
40 |
+
style='background-color: #0084ff; color: white; padding: 8px 16px;
|
41 |
+
border-radius: 6px; text-decoration: none; display: inline-block;
|
42 |
+
font-weight: 500; transition: background-color 0.2s;'
|
43 |
+
onmouseover='this.style.backgroundColor="#0066cc"'
|
44 |
+
onmouseout='this.style.backgroundColor="#0084ff"'>
|
45 |
View Space
|
46 |
</a>
|
47 |
+
<span style='color: #666; font-size: 0.9em;'>
|
48 |
+
ID: {space_id}
|
49 |
+
</span>
|
50 |
</div>
|
51 |
</div>
|
52 |
"""
|
53 |
|
54 |
def get_user_spaces():
|
55 |
+
url = "https://huggingface.co/api/spaces"
|
|
|
56 |
headers = {
|
57 |
"Accept": "application/json",
|
58 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
|
59 |
}
|
60 |
|
61 |
try:
|
62 |
response = requests.get(url, headers=headers)
|
63 |
+
print(f"Status Code: {response.status_code}")
|
|
|
64 |
|
65 |
if response.status_code != 200:
|
66 |
return f"Error: Failed to fetch spaces (Status Code: {response.status_code})"
|
67 |
|
68 |
spaces_data = response.json()
|
69 |
|
70 |
+
# Filter spaces for the specific user
|
71 |
+
user_spaces = [space for space in spaces_data if space.get('id', '').startswith(f"{USERNAME}/")]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
if not user_spaces:
|
74 |
return f"""
|
|
|
79 |
</div>
|
80 |
"""
|
81 |
|
82 |
+
# Sort spaces by likes (most liked first)
|
83 |
+
user_spaces.sort(key=lambda x: x.get('likes', 0), reverse=True)
|
84 |
+
|
85 |
# Create HTML grid layout
|
86 |
html_content = f"""
|
87 |
<div style='padding: 20px; background-color: #f5f5f5;'>
|
88 |
<div style='margin-bottom: 20px;'>
|
89 |
+
<h2 style='color: #333; margin: 0 0 10px 0;'>Spaces by {USERNAME}</h2>
|
90 |
+
<p style='color: #666; margin: 0;'>Found {len(user_spaces)} public spaces</p>
|
91 |
</div>
|
92 |
<div style='
|
93 |
display: grid;
|
|
|
102 |
return html_content
|
103 |
|
104 |
except Exception as e:
|
105 |
+
print(f"Error: {str(e)}")
|
106 |
return f"""
|
107 |
<div style='padding: 20px; text-align: center; color: #666;'>
|
108 |
<h2>Error occurred while fetching spaces</h2>
|
109 |
<p>Error details: {str(e)}</p>
|
110 |
+
<p>Please try again later.</p>
|
111 |
</div>
|
112 |
"""
|
113 |
|
|
|
116 |
fn=get_user_spaces,
|
117 |
inputs=None,
|
118 |
outputs=gr.HTML(),
|
119 |
+
title=f"Hugging Face Spaces Dashboard - {USERNAME}",
|
120 |
description=f"Displays public Spaces from {USERNAME}",
|
121 |
theme=gr.themes.Soft(),
|
122 |
css="""
|
|
|
126 |
"""
|
127 |
)
|
128 |
|
129 |
+
# Launch the Gradio app
|
130 |
if __name__ == "__main__":
|
131 |
+
app.launch()
|