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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -61
app.py CHANGED
@@ -2,7 +2,7 @@ import requests
2
  import gradio as gr
3
  from datetime import datetime
4
 
5
- # 테스트를 위해 Gradio 공식 계정으로 변경
6
  USERNAME = "gradio"
7
 
8
  def format_timestamp(timestamp):
@@ -11,8 +11,29 @@ def format_timestamp(timestamp):
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,83 +41,73 @@ def get_space_card(space):
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.get("url", "#")}' target='_blank'
24
  style='text-decoration: none; color: #2d2d2d;'>
25
- {space.get('title', 'Unnamed Space')}
26
  </a>
27
  </h3>
28
- <p style='margin: 5px 0; color: #666;'>{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.get("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
- <span style='color: #666;'>
36
- ❤️ {space.get('likes', 0)}
37
- </span>
38
  </div>
39
  </div>
40
  """
41
 
42
  def get_user_spaces():
43
- # Hugging Face search API 사용
44
- url = "https://huggingface.co/api/spaces"
45
- params = {
46
- "author": USERNAME,
47
- "limit": 100,
48
- "full": True
49
- }
50
 
51
- try:
52
- response = requests.get(url, params=params)
53
- print(f"Status Code: {response.status_code}") # 디버깅용
54
- print(f"URL: {response.url}") # 디버깅용
55
- print(f"Response: {response.text[:500]}...") # 디버깅용
 
 
 
 
 
 
 
 
 
56
 
57
- if response.status_code != 200:
58
- return f"Error: Failed to fetch spaces (Status Code: {response.status_code})"
59
-
60
- spaces = response.json()
61
-
62
- if not spaces:
63
- # 다른 API 엔드포인트 시도
64
- alternate_url = f"https://huggingface.co/api/spaces/{USERNAME}"
65
- response = requests.get(alternate_url)
66
- spaces = response.json() if response.status_code == 200 else []
67
-
68
- if not spaces:
69
- return "No public Spaces found for this user."
70
-
71
- if not isinstance(spaces, list):
72
- spaces = [spaces]
73
-
74
- # Create HTML grid layout
75
- html_content = f"""
76
- <div style='padding: 20px; background-color: #f5f5f5;'>
77
- <div style='margin-bottom: 20px;'>
78
- <p style='color: #666; margin: 0;'>Found {len(spaces)} public spaces for {USERNAME}</p>
79
- </div>
80
- <div style='
81
- display: grid;
82
- grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
83
- gap: 20px;
84
- '>
85
- {"".join(get_space_card({
86
- 'title': space.get('id', '').split('/')[-1],
87
- 'description': space.get('description', ''),
88
- 'url': f"https://huggingface.co/spaces/{space.get('id', '')}",
89
- 'likes': space.get('likes', 0)
90
- }) for space in spaces)}
91
- </div>
92
  </div>
93
  """
94
 
95
- return html_content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
- except Exception as e:
98
- print(f"Error: {str(e)}") # 디버깅용
99
- return f"Error occurred: {str(e)}"
100
 
101
  # Creating the Gradio interface
102
  app = gr.Interface(
 
2
  import gradio as gr
3
  from datetime import datetime
4
 
5
+ # 테스트를 위해 Gradio 공식 계정으로 설정
6
  USERNAME = "gradio"
7
 
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
  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>
59
  </div>
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(