seawolf2357 commited on
Commit
ada9833
·
verified ·
1 Parent(s): cfcb6db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -59
app.py CHANGED
@@ -6,63 +6,28 @@ import time
6
 
7
  app = Flask(__name__)
8
 
9
- # Function to fetch spaces from Huggingface
10
- def fetch_spaces(limit=100):
11
  try:
12
- # Get trending spaces sorted by likes count (since trending API is giving 400 error)
13
  url = "https://huggingface.co/api/spaces"
14
  params = {
15
  "limit": limit,
16
- "sort": "likes",
17
- "order": "desc"
18
  }
19
  response = requests.get(url, params=params, timeout=10)
20
 
21
  if response.status_code == 200:
22
- return response.json()
 
 
 
23
  else:
24
- print(f"Error fetching spaces: {response.status_code}")
25
- # Fallback to hardcoded list if API fails
26
- return get_fallback_spaces()
27
  except Exception as e:
28
- print(f"Exception when fetching spaces: {e}")
29
- # Fallback to hardcoded list if API fails
30
- return get_fallback_spaces()
31
-
32
- # Fallback function to provide some spaces if the API call fails
33
- def get_fallback_spaces():
34
- return [
35
- {
36
- "id": "stabilityai/stable-diffusion-xl-base-1.0",
37
- "owner": "stabilityai",
38
- "title": "Stable Diffusion XL Base 1.0",
39
- "likes": 4857
40
- },
41
- {
42
- "id": "meta-llama/Llama-2-7b-chat-hf",
43
- "owner": "meta-llama",
44
- "title": "Llama 2 7B Chat",
45
- "likes": 3821
46
- },
47
- {
48
- "id": "mistralai/Mistral-7B-Instruct-v0.2",
49
- "owner": "mistralai",
50
- "title": "Mistral 7B Instruct v0.2",
51
- "likes": 2953
52
- },
53
- {
54
- "id": "runwayml/stable-diffusion-v1-5",
55
- "owner": "runwayml",
56
- "title": "Stable Diffusion v1.5",
57
- "likes": 2649
58
- },
59
- {
60
- "id": "microsoft/phi-2",
61
- "owner": "microsoft",
62
- "title": "Phi-2",
63
- "likes": 2134
64
- }
65
- ]
66
 
67
  # Transform Huggingface URL to direct space URL
68
  def transform_url(owner, name):
@@ -71,19 +36,28 @@ def transform_url(owner, name):
71
  # Get space details
72
  def get_space_details(space_data):
73
  try:
74
- # Basic info
75
- owner = space_data.get('owner')
76
- name = space_data.get('id')
77
- title = space_data.get('title') or name
 
 
 
 
 
 
78
 
79
- # URL construction
80
  original_url = f"https://huggingface.co/spaces/{owner}/{name}"
81
  embed_url = transform_url(owner, name)
82
 
83
- # Get likes count
84
  likes_count = space_data.get('likes', 0)
85
 
86
- # Tags
 
 
 
87
  tags = space_data.get('tags', [])
88
 
89
  return {
@@ -103,18 +77,18 @@ def get_space_details(space_data):
103
  def home():
104
  return render_template('index.html')
105
 
106
- # Spaces API
107
  @app.route('/api/trending-spaces', methods=['GET'])
108
  def trending_spaces():
109
  search_query = request.args.get('search', '').lower()
110
  limit = int(request.args.get('limit', 100))
111
 
112
- # Fetch spaces
113
- spaces_data = fetch_spaces(limit)
114
 
115
  # Process and filter spaces
116
  results = []
117
- for space_data in spaces_data:
118
  space_info = get_space_details(space_data)
119
 
120
  if not space_info:
@@ -459,9 +433,14 @@ if __name__ == '__main__':
459
  return;
460
  }
461
 
462
- spaces.forEach(item => {
463
  const { url, embedUrl, title, likes_count, owner } = item;
464
 
 
 
 
 
 
465
  // Create grid item
466
  const gridItem = document.createElement('div');
467
  gridItem.className = 'grid-item';
 
6
 
7
  app = Flask(__name__)
8
 
9
+ # Function to fetch trending spaces from Huggingface
10
+ def fetch_trending_spaces(limit=100):
11
  try:
12
+ # 트렌딩 스페이스 가져오기 (원래의 API 파라미터 사용)
13
  url = "https://huggingface.co/api/spaces"
14
  params = {
15
  "limit": limit,
16
+ "sort": "trending" # 단순히 trending 파라미터만 사용
 
17
  }
18
  response = requests.get(url, params=params, timeout=10)
19
 
20
  if response.status_code == 200:
21
+ # None 값이 있는 항목 필터링
22
+ spaces = response.json()
23
+ filtered_spaces = [space for space in spaces if space.get('owner') != 'None' and space.get('id', '').split('/', 1)[0] != 'None']
24
+ return filtered_spaces
25
  else:
26
+ print(f"Error fetching trending spaces: {response.status_code}")
27
+ return []
 
28
  except Exception as e:
29
+ print(f"Exception when fetching trending spaces: {e}")
30
+ return []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  # Transform Huggingface URL to direct space URL
33
  def transform_url(owner, name):
 
36
  # Get space details
37
  def get_space_details(space_data):
38
  try:
39
+ # 공통 정보 추출
40
+ if '/' in space_data.get('id', ''):
41
+ owner, name = space_data.get('id', '').split('/', 1)
42
+ else:
43
+ owner = space_data.get('owner', '')
44
+ name = space_data.get('id', '')
45
+
46
+ # None이 포함된 경우 무시
47
+ if owner == 'None' or name == 'None':
48
+ return None
49
 
50
+ # URL 구성
51
  original_url = f"https://huggingface.co/spaces/{owner}/{name}"
52
  embed_url = transform_url(owner, name)
53
 
54
+ # 좋아요
55
  likes_count = space_data.get('likes', 0)
56
 
57
+ # 제목 추출
58
+ title = space_data.get('title', name)
59
+
60
+ # 태그
61
  tags = space_data.get('tags', [])
62
 
63
  return {
 
77
  def home():
78
  return render_template('index.html')
79
 
80
+ # Trending spaces API
81
  @app.route('/api/trending-spaces', methods=['GET'])
82
  def trending_spaces():
83
  search_query = request.args.get('search', '').lower()
84
  limit = int(request.args.get('limit', 100))
85
 
86
+ # Fetch trending spaces
87
+ spaces_data = fetch_trending_spaces(limit)
88
 
89
  # Process and filter spaces
90
  results = []
91
+ for index, space_data in enumerate(spaces_data):
92
  space_info = get_space_details(space_data)
93
 
94
  if not space_info:
 
433
  return;
434
  }
435
 
436
+ spaces.forEach((item, index) => {
437
  const { url, embedUrl, title, likes_count, owner } = item;
438
 
439
+ // Skip if owner is 'None'
440
+ if (owner === 'None') {
441
+ return;
442
+ }
443
+
444
  // Create grid item
445
  const gridItem = document.createElement('div');
446
  gridItem.className = 'grid-item';