openfree commited on
Commit
2d1b715
·
verified ·
1 Parent(s): 6cb3060

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -22
app.py CHANGED
@@ -15,24 +15,19 @@ def format_timestamp(timestamp):
15
 
16
  def get_space_card(space):
17
  """Generate HTML card for a space"""
18
- space_id = space.get('id', '').split('/')[-1]
19
  return f"""
20
  <div style='border: 1px solid #ddd; padding: 15px; margin: 10px; border-radius: 8px;
21
  background-color: white; box-shadow: 2px 2px 5px rgba(0,0,0,0.1);'>
22
  <h3 style='color: #2d2d2d; margin: 0 0 10px 0;'>
23
- <a href='https://huggingface.co/spaces/{USERNAME}/{space_id}' target='_blank'
24
  style='text-decoration: none; color: #2d2d2d;'>
25
- {space_id}
26
  </a>
27
  </h3>
28
- <p style='margin: 5px 0;'><strong>Space ID:</strong> {space_id}</p>
29
- <p style='margin: 5px 0;'><strong>SDK:</strong> {space.get('sdk', 'N/A')}</p>
30
- <p style='margin: 5px 0;'><strong>Status:</strong>
31
- <span style='color: {"green" if space.get("status") == "running" else "red"}'>
32
- {space.get('status', 'N/A')}</span>
33
- </p>
34
- <p style='margin: 5px 0;'><strong>Created:</strong> {format_timestamp(space.get("createdAt"))}</p>
35
- <p style='margin: 5px 0;'><strong>Updated:</strong> {format_timestamp(space.get("updatedAt"))}</p>
36
  </div>
37
  """
38
 
@@ -40,26 +35,28 @@ def get_user_spaces():
40
  if not HF_TOKEN:
41
  return "Error: Hugging Face token not found."
42
 
43
- headers = {"Authorization": f"Bearer {HF_TOKEN}"}
 
 
 
44
 
45
- # Get all spaces and filter by username
46
  response = requests.get(
47
- "https://huggingface.co/api/spaces",
48
- headers=headers,
49
- params={"author": USERNAME} # Filter by author
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
- # Filter spaces for the specific user
58
- user_spaces = [space for space in spaces if space.get('author', '') == USERNAME]
59
 
60
- if not user_spaces:
61
  return "No Spaces found for this user."
62
 
 
 
 
63
  # Create HTML grid layout
64
  html_content = f"""
65
  <div style='
@@ -69,7 +66,7 @@ def get_user_spaces():
69
  padding: 20px;
70
  background-color: #f5f5f5;
71
  '>
72
- {"".join(get_space_card(space) for space in user_spaces)}
73
  </div>
74
  """
75
 
 
15
 
16
  def get_space_card(space):
17
  """Generate HTML card for a space"""
 
18
  return f"""
19
  <div style='border: 1px solid #ddd; padding: 15px; margin: 10px; border-radius: 8px;
20
  background-color: white; box-shadow: 2px 2px 5px rgba(0,0,0,0.1);'>
21
  <h3 style='color: #2d2d2d; margin: 0 0 10px 0;'>
22
+ <a href='https://huggingface.co/spaces/{space["repo_id"]}' target='_blank'
23
  style='text-decoration: none; color: #2d2d2d;'>
24
+ {space["repo_id"].split('/')[-1]}
25
  </a>
26
  </h3>
27
+ <p style='margin: 5px 0;'><strong>Repository:</strong> {space["repo_id"]}</p>
28
+ <p style='margin: 5px 0;'><strong>Private:</strong> {space.get("private", False)}</p>
29
+ <p style='margin: 5px 0;'><strong>Last Modified:</strong> {format_timestamp(space.get("lastModified"))}</p>
30
+ <p style='margin: 5px 0;'><strong>Space SDK:</strong> {space.get("sdk", "N/A")}</p>
 
 
 
 
31
  </div>
32
  """
33
 
 
35
  if not HF_TOKEN:
36
  return "Error: Hugging Face token not found."
37
 
38
+ headers = {
39
+ "Authorization": f"Bearer {HF_TOKEN}",
40
+ "Accept": "application/json"
41
+ }
42
 
43
+ # Get user's repositories filtered by space type
44
  response = requests.get(
45
+ f"https://huggingface.co/api/models?author={USERNAME}&filter=space",
46
+ headers=headers
 
47
  )
48
 
49
  if response.status_code != 200:
50
+ return f"Error: Failed to fetch spaces (Status Code: {response.status_code})\nResponse: {response.text}"
51
 
52
  spaces = response.json()
 
 
 
53
 
54
+ if not spaces:
55
  return "No Spaces found for this user."
56
 
57
+ # Print response for debugging
58
+ print(f"Found {len(spaces)} spaces: {spaces}")
59
+
60
  # Create HTML grid layout
61
  html_content = f"""
62
  <div style='
 
66
  padding: 20px;
67
  background-color: #f5f5f5;
68
  '>
69
+ {"".join(get_space_card(space) for space in spaces)}
70
  </div>
71
  """
72