Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,9 @@
|
|
1 |
-
import os
|
2 |
import requests
|
3 |
import gradio as gr
|
4 |
from datetime import datetime
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
def get_username_from_token():
|
10 |
-
"""Retrieve the username using the Hugging Face token"""
|
11 |
-
if not HF_TOKEN:
|
12 |
-
return None
|
13 |
-
|
14 |
-
headers = {
|
15 |
-
"Authorization": f"Bearer {HF_TOKEN}",
|
16 |
-
"Accept": "application/json"
|
17 |
-
}
|
18 |
-
|
19 |
-
response = requests.get("https://huggingface.co/api/whoami", headers=headers)
|
20 |
-
print(f"Response status: {response.status_code}")
|
21 |
-
print(f"Response content: {response.text}")
|
22 |
-
|
23 |
-
if response.status_code == 200:
|
24 |
-
return response.json().get("name")
|
25 |
-
return None
|
26 |
-
|
27 |
-
# Retrieve the username
|
28 |
-
USERNAME = get_username_from_token()
|
29 |
-
|
30 |
-
if not USERNAME:
|
31 |
-
raise ValueError("Error: Could not retrieve username. Check your Hugging Face token.")
|
32 |
|
33 |
def format_timestamp(timestamp):
|
34 |
if timestamp:
|
@@ -38,58 +13,63 @@ def format_timestamp(timestamp):
|
|
38 |
|
39 |
def get_space_card(space):
|
40 |
"""Generate HTML card for a space"""
|
|
|
41 |
return f"""
|
42 |
<div style='border: 1px solid #ddd; padding: 15px; margin: 10px; border-radius: 8px;
|
43 |
-
background-color: white; box-shadow: 2px 2px 5px rgba(0,0,0,0.1);
|
|
|
|
|
|
|
44 |
<h3 style='color: #2d2d2d; margin: 0 0 10px 0;'>
|
45 |
<a href='https://huggingface.co/spaces/{space["repo_id"]}' target='_blank'
|
46 |
style='text-decoration: none; color: #2d2d2d;'>
|
47 |
-
{
|
48 |
</a>
|
49 |
</h3>
|
50 |
-
<p style='margin: 5px 0;'><strong>
|
51 |
-
|
52 |
-
<p style='margin: 5px 0;'><strong>Last Modified:</strong>
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
</div>
|
55 |
"""
|
56 |
|
57 |
def get_user_spaces():
|
58 |
-
|
59 |
-
return "Error: Hugging Face token not found."
|
60 |
-
|
61 |
-
headers = {
|
62 |
-
"Authorization": f"Bearer {HF_TOKEN}",
|
63 |
-
"Accept": "application/json"
|
64 |
-
}
|
65 |
-
|
66 |
-
# Get user's repositories filtered by space type
|
67 |
response = requests.get(
|
68 |
f"https://huggingface.co/api/models?author={USERNAME}&filter=space",
|
69 |
-
headers=
|
70 |
)
|
71 |
|
72 |
if response.status_code != 200:
|
73 |
-
return f"Error: Failed to fetch spaces (Status Code: {response.status_code})
|
74 |
|
75 |
spaces = response.json()
|
76 |
|
77 |
if not spaces:
|
78 |
-
return "No Spaces found for this user."
|
79 |
-
|
80 |
-
# Print response for debugging
|
81 |
-
print(f"Found {len(spaces)} spaces: {spaces}")
|
82 |
|
83 |
-
# Create HTML grid layout
|
84 |
html_content = f"""
|
85 |
-
<div style='
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
93 |
</div>
|
94 |
"""
|
95 |
|
@@ -100,8 +80,8 @@ app = gr.Interface(
|
|
100 |
fn=get_user_spaces,
|
101 |
inputs=None,
|
102 |
outputs=gr.HTML(),
|
103 |
-
title=f"Hugging Face Spaces
|
104 |
-
description=f"Displays
|
105 |
theme=gr.themes.Soft(),
|
106 |
css="""
|
107 |
.gradio-container {
|
@@ -112,4 +92,4 @@ app = gr.Interface(
|
|
112 |
|
113 |
# Launch the Gradio app
|
114 |
if __name__ == "__main__":
|
115 |
-
app.launch()
|
|
|
|
|
1 |
import requests
|
2 |
import gradio as gr
|
3 |
from datetime import datetime
|
4 |
|
5 |
+
# ๊ณ ์ ๋ ์ฌ์ฉ์ ์ด๋ฆ
|
6 |
+
USERNAME = "openfree"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
def format_timestamp(timestamp):
|
9 |
if timestamp:
|
|
|
13 |
|
14 |
def get_space_card(space):
|
15 |
"""Generate HTML card for a space"""
|
16 |
+
space_name = space["repo_id"].split('/')[-1]
|
17 |
return f"""
|
18 |
<div style='border: 1px solid #ddd; padding: 15px; margin: 10px; border-radius: 8px;
|
19 |
+
background-color: white; box-shadow: 2px 2px 5px rgba(0,0,0,0.1);
|
20 |
+
transition: transform 0.2s ease-in-out;'
|
21 |
+
onmouseover='this.style.transform="scale(1.02)"'
|
22 |
+
onmouseout='this.style.transform="scale(1)"'>
|
23 |
<h3 style='color: #2d2d2d; margin: 0 0 10px 0;'>
|
24 |
<a href='https://huggingface.co/spaces/{space["repo_id"]}' target='_blank'
|
25 |
style='text-decoration: none; color: #2d2d2d;'>
|
26 |
+
{space_name}
|
27 |
</a>
|
28 |
</h3>
|
29 |
+
<p style='margin: 5px 0;'><strong>Description:</strong>
|
30 |
+
{space.get("description", "No description available")}</p>
|
31 |
+
<p style='margin: 5px 0;'><strong>Last Modified:</strong>
|
32 |
+
{format_timestamp(space.get("lastModified"))}</p>
|
33 |
+
<p style='margin: 5px 0;'><strong>Likes:</strong>
|
34 |
+
{space.get("likes", 0)} โค๏ธ</p>
|
35 |
+
<div style='margin-top: 10px;'>
|
36 |
+
<a href='https://huggingface.co/spaces/{space["repo_id"]}' target='_blank'
|
37 |
+
style='background-color: #0084ff; color: white; padding: 5px 10px;
|
38 |
+
border-radius: 5px; text-decoration: none; display: inline-block;'>
|
39 |
+
View Space
|
40 |
+
</a>
|
41 |
+
</div>
|
42 |
</div>
|
43 |
"""
|
44 |
|
45 |
def get_user_spaces():
|
46 |
+
# ๊ณต๊ฐ API ํธ์ถ (ํ ํฐ ๋ถํ์)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
response = requests.get(
|
48 |
f"https://huggingface.co/api/models?author={USERNAME}&filter=space",
|
49 |
+
headers={"Accept": "application/json"}
|
50 |
)
|
51 |
|
52 |
if response.status_code != 200:
|
53 |
+
return f"Error: Failed to fetch spaces (Status Code: {response.status_code})"
|
54 |
|
55 |
spaces = response.json()
|
56 |
|
57 |
if not spaces:
|
58 |
+
return "No public Spaces found for this user."
|
|
|
|
|
|
|
59 |
|
60 |
+
# Create HTML grid layout with title and count
|
61 |
html_content = f"""
|
62 |
+
<div style='padding: 20px; background-color: #f5f5f5;'>
|
63 |
+
<div style='margin-bottom: 20px;'>
|
64 |
+
<p style='color: #666; margin: 0;'>Found {len(spaces)} public spaces</p>
|
65 |
+
</div>
|
66 |
+
<div style='
|
67 |
+
display: grid;
|
68 |
+
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
|
69 |
+
gap: 20px;
|
70 |
+
'>
|
71 |
+
{"".join(get_space_card(space) for space in spaces)}
|
72 |
+
</div>
|
73 |
</div>
|
74 |
"""
|
75 |
|
|
|
80 |
fn=get_user_spaces,
|
81 |
inputs=None,
|
82 |
outputs=gr.HTML(),
|
83 |
+
title=f"Hugging Face Public Spaces - {USERNAME}",
|
84 |
+
description=f"Displays public Spaces from {USERNAME}",
|
85 |
theme=gr.themes.Soft(),
|
86 |
css="""
|
87 |
.gradio-container {
|
|
|
92 |
|
93 |
# Launch the Gradio app
|
94 |
if __name__ == "__main__":
|
95 |
+
app.launch()
|