openfree commited on
Commit
bf508de
1 Parent(s): 4805e44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -74
app.py CHANGED
@@ -11,29 +11,8 @@ def format_timestamp(timestamp):
11
  return dt.strftime('%Y-%m-%d %H:%M')
12
  return 'N/A'
13
 
14
- def search_spaces():
15
- """Search for spaces using the Hugging Face search API"""
16
- url = "https://huggingface.co/api/spaces"
17
- headers = {"Accept": "application/json"}
18
-
19
- try:
20
- # First, try to get all spaces
21
- response = requests.get(url, headers=headers)
22
- if response.status_code == 200:
23
- all_spaces = response.json()
24
- # Filter spaces by username
25
- user_spaces = [space for space in all_spaces
26
- if space.get('author', '').lower() == USERNAME.lower()]
27
- return user_spaces
28
- except Exception as e:
29
- print(f"Error in search_spaces: {e}")
30
- return []
31
-
32
  def get_space_card(space):
33
  """Generate HTML card for a space"""
34
- space_id = space.get('id', '')
35
- space_name = space_id.split('/')[-1] if space_id else 'Unknown'
36
-
37
  return f"""
38
  <div style='border: 1px solid #ddd; padding: 15px; margin: 10px; border-radius: 8px;
39
  background-color: white; box-shadow: 2px 2px 5px rgba(0,0,0,0.1);
@@ -41,18 +20,16 @@ def get_space_card(space):
41
  onmouseover='this.style.transform="scale(1.02)"'
42
  onmouseout='this.style.transform="scale(1)"'>
43
  <h3 style='color: #2d2d2d; margin: 0 0 10px 0;'>
44
- <a href='https://huggingface.co/spaces/{space_id}' target='_blank'
45
  style='text-decoration: none; color: #2d2d2d;'>
46
- {space_name}
47
  </a>
48
  </h3>
49
- <p style='margin: 5px 0;'><strong>Author:</strong> {space.get('author', 'Unknown')}</p>
50
- <p style='margin: 5px 0;'><strong>SDK:</strong> {space.get('sdk', 'N/A')}</p>
51
- <p style='margin: 5px 0;'><strong>Likes:</strong> {space.get('likes', 0)} ❤️</p>
52
- <div style='margin-top: 10px;'>
53
- <a href='https://huggingface.co/spaces/{space_id}' target='_blank'
54
  style='background-color: #0084ff; color: white; padding: 5px 10px;
55
- border-radius: 5px; text-decoration: none; display: inline-block;'>
56
  View Space
57
  </a>
58
  </div>
@@ -60,54 +37,70 @@ def get_space_card(space):
60
  """
61
 
62
  def get_user_spaces():
63
- # Get spaces for the user
64
- spaces = search_spaces()
65
-
66
- if not spaces:
67
- # Try alternative search method
68
- try:
69
- response = requests.get(
70
- f"https://huggingface.co/search/full-text",
71
- params={"q": f"user:{USERNAME} type:space"},
72
- headers={"Accept": "application/json"}
73
- )
74
- if response.status_code == 200:
75
- search_results = response.json()
76
- if 'hits' in search_results:
77
- spaces = search_results['hits']
78
- except Exception as e:
79
- print(f"Error in alternative search: {e}")
80
 
81
- if not spaces:
82
- return """
83
- <div style='padding: 20px; text-align: center; color: #666;'>
84
- <h2>No public Spaces found for user: {USERNAME}</h2>
85
- <p>This could be because:</p>
86
- <ul style='list-style: none; padding: 0;'>
87
- <li>The user has no public spaces</li>
88
- <li>The username might be incorrect</li>
89
- <li>There might be an issue with the API</li>
90
- </ul>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  </div>
92
  """
93
 
94
- # Create HTML grid layout
95
- html_content = f"""
96
- <div style='padding: 20px; background-color: #f5f5f5;'>
97
- <div style='margin-bottom: 20px;'>
98
- <p style='color: #666; margin: 0;'>Found {len(spaces)} public spaces for {USERNAME}</p>
99
- </div>
100
- <div style='
101
- display: grid;
102
- grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
103
- gap: 20px;
104
- '>
105
- {"".join(get_space_card(space) for space in spaces)}
106
- </div>
107
- </div>
108
- """
109
 
110
- return html_content
 
 
 
 
 
 
 
 
111
 
112
  # Creating the Gradio interface
113
  app = gr.Interface(
@@ -124,6 +117,6 @@ app = gr.Interface(
124
  """
125
  )
126
 
127
- # Launch the Gradio app
128
  if __name__ == "__main__":
129
- app.launch()
 
11
  return dt.strftime('%Y-%m-%d %H:%M')
12
  return 'N/A'
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: 15px; margin: 10px; border-radius: 8px;
18
  background-color: white; box-shadow: 2px 2px 5px rgba(0,0,0,0.1);
 
20
  onmouseover='this.style.transform="scale(1.02)"'
21
  onmouseout='this.style.transform="scale(1)"'>
22
  <h3 style='color: #2d2d2d; margin: 0 0 10px 0;'>
23
+ <a href='{space["url"]}' target='_blank'
24
  style='text-decoration: none; color: #2d2d2d;'>
25
+ {space["title"]}
26
  </a>
27
  </h3>
28
+ <p style='margin: 5px 0;'>{space.get("description", "No description available")}</p>
29
+ <div style='margin-top: 10px; display: flex; justify-content: space-between; align-items: center;'>
30
+ <a href='{space["url"]}' target='_blank'
 
 
31
  style='background-color: #0084ff; color: white; padding: 5px 10px;
32
+ border-radius: 5px; text-decoration: none;'>
33
  View Space
34
  </a>
35
  </div>
 
37
  """
38
 
39
  def get_user_spaces():
40
+ # Hugging Face API v2 사용
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 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/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
+ # 사용자의 spaces만 필터링
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"""
70
+ <div style='padding: 20px; text-align: center; color: #666;'>
71
+ <h2>No public Spaces found for user: {USERNAME}</h2>
72
+ <p>Try visiting: <a href='https://huggingface.co/{USERNAME}' target='_blank'>
73
+ https://huggingface.co/{USERNAME}</a></p>
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
+ <p style='color: #666; margin: 0;'>Found {len(user_spaces)} public spaces for {USERNAME}</p>
82
+ </div>
83
+ <div style='
84
+ display: grid;
85
+ grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
86
+ gap: 20px;
87
+ '>
88
+ {"".join(get_space_card(space) for space in user_spaces)}
89
+ </div>
90
  </div>
91
  """
92
 
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 or check if the username is correct.</p>
102
+ </div>
103
+ """
104
 
105
  # Creating the Gradio interface
106
  app = gr.Interface(
 
117
  """
118
  )
119
 
120
+ # Launch the Gradio app with sharing enabled
121
  if __name__ == "__main__":
122
+ app.launch(share=True) # share=True를 추가하여 공개 링크 생성