openfree commited on
Commit
060cebe
โ€ข
1 Parent(s): 5b2e51f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -47
app.py CHANGED
@@ -2,8 +2,8 @@ import requests
2
  import gradio as gr
3
  from datetime import datetime
4
 
5
- # ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด ์•Œ๋ ค์ง„ ์‚ฌ์šฉ์ž๋กœ ๋ณ€๊ฒฝ
6
- USERNAME = "multimodalart" # ๋งŽ์€ public spaces๋ฅผ ๊ฐ€์ง„ ์‚ฌ์šฉ์ž
7
 
8
  def format_timestamp(timestamp):
9
  if timestamp:
@@ -13,7 +13,6 @@ def format_timestamp(timestamp):
13
 
14
  def get_space_card(space):
15
  """Generate HTML card for a space"""
16
- space_name = space["id"].split('/')[-1] if 'id' in space else space.get('name', 'Unknown')
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);
@@ -21,68 +20,83 @@ def get_space_card(space):
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/{USERNAME}/{space_name}' target='_blank'
25
  style='text-decoration: none; color: #2d2d2d;'>
26
- {space_name}
27
  </a>
28
  </h3>
29
- <p style='margin: 5px 0;'><strong>Status:</strong>
30
- <span style='color: {"green" if space.get("status") == "running" else "orange"}'>
31
- {space.get("status", "unknown")}</span>
32
- </p>
33
- <p style='margin: 5px 0;'><strong>SDK:</strong> {space.get("sdk", "N/A")}</p>
34
- <div style='margin-top: 10px;'>
35
- <a href='https://huggingface.co/spaces/{USERNAME}/{space_name}' target='_blank'
36
  style='background-color: #0084ff; color: white; padding: 5px 10px;
37
- border-radius: 5px; text-decoration: none; display: inline-block;'>
38
  View Space
39
  </a>
 
 
 
40
  </div>
41
  </div>
42
  """
43
 
44
  def get_user_spaces():
45
- # spaces ๊ฒ€์ƒ‰ API ์‚ฌ์šฉ
46
- response = requests.get(
47
- "https://huggingface.co/api/spaces",
48
- params={"search": USERNAME},
49
- headers={"Accept": "application/json"}
50
- )
 
 
 
 
 
 
 
51
 
52
- # ๋””๋ฒ„๊น…์„ ์œ„ํ•œ ์ถœ๋ ฅ
53
- print(f"Status Code: {response.status_code}")
54
- print(f"Response: {response.text[:500]}...") # ์‘๋‹ต์˜ ์ฒ˜์Œ 500์ž๋งŒ ์ถœ๋ ฅ
55
 
56
- if response.status_code != 200:
57
- return f"Error: Failed to fetch spaces (Status Code: {response.status_code})"
 
 
 
 
 
58
 
59
- try:
60
- all_spaces = response.json()
61
- # ํ•ด๋‹น ์‚ฌ์šฉ์ž์˜ spaces๋งŒ ํ•„ํ„ฐ๋ง
62
- user_spaces = [space for space in all_spaces if space.get('author', '') == USERNAME]
63
- except Exception as e:
64
- return f"Error parsing response: {str(e)}"
65
 
66
- if not user_spaces:
67
- return "No public Spaces found for this user."
68
 
69
- # Create HTML grid layout with title and count
70
- html_content = f"""
71
- <div style='padding: 20px; background-color: #f5f5f5;'>
72
- <div style='margin-bottom: 20px;'>
73
- <p style='color: #666; margin: 0;'>Found {len(user_spaces)} public spaces for {USERNAME}</p>
74
- </div>
75
- <div style='
76
- display: grid;
77
- grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
78
- gap: 20px;
79
- '>
80
- {"".join(get_space_card(space) for space in user_spaces)}
 
 
 
 
 
 
81
  </div>
82
- </div>
83
- """
84
 
85
- return html_content
 
 
 
 
86
 
87
  # Creating the Gradio interface
88
  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):
9
  if 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: 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.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(