from flask import Flask, render_template, request, jsonify import requests import os import time app = Flask(__name__) # Function to fetch trending spaces from Huggingface with pagination def fetch_trending_spaces(offset=0, limit=72): try: # 단순하게 데이터 가져오기 url = "https://huggingface.co/api/spaces" params = {"limit": 500} # 최대 500개 가져오기 # 타임아웃 늘리기 response = requests.get(url, params=params, timeout=30) if response.status_code == 200: spaces = response.json() filtered_spaces = [space for space in spaces if space.get('owner') != 'None' and space.get('id', '').split('/', 1)[0] != 'None'] # 요청된 offset과 limit에 맞게 슬라이싱 start = min(offset, len(filtered_spaces)) end = min(offset + limit, len(filtered_spaces)) print(f"Fetched {len(filtered_spaces)} spaces, returning {end-start} items from {start} to {end}") return { 'spaces': filtered_spaces[start:end], 'total': len(filtered_spaces), 'offset': offset, 'limit': limit } else: print(f"Error fetching spaces: {response.status_code}") # 빈 공간 반환하지만 200개 제한의 가짜 데이터 return { 'spaces': generate_dummy_spaces(limit), 'total': 200, 'offset': offset, 'limit': limit } except Exception as e: print(f"Exception when fetching spaces: {e}") # 가짜 데이터 생성 return { 'spaces': generate_dummy_spaces(limit), 'total': 200, 'offset': offset, 'limit': limit } # 오류 시 가짜 데이터 생성 def generate_dummy_spaces(count): spaces = [] for i in range(count): spaces.append({ 'id': f'dummy/space-{i}', 'owner': 'dummy', 'title': f'Example Space {i+1}', 'likes': 100 - i, 'createdAt': '2023-01-01T00:00:00.000Z' }) return spaces # Transform Huggingface URL to direct space URL def transform_url(owner, name): # 1. '.' 문자를 '-'로 변경 name = name.replace('.', '-') # 2. '_' 문자를 '-'로 변경 name = name.replace('_', '-') # 3. 대소문자 구분 없이 모두 소문자로 변경 owner = owner.lower() name = name.lower() return f"https://{owner}-{name}.hf.space" # Get space details def get_space_details(space_data, index, offset): try: # 공통 정보 추출 if '/' in space_data.get('id', ''): owner, name = space_data.get('id', '').split('/', 1) else: owner = space_data.get('owner', '') name = space_data.get('id', '') # None이 포함된 경우 무시 if owner == 'None' or name == 'None': return None # URL 구성 original_url = f"https://huggingface.co/spaces/{owner}/{name}" embed_url = transform_url(owner, name) # 좋아요 수 likes_count = space_data.get('likes', 0) # 제목 추출 title = space_data.get('title', name) # 태그 tags = space_data.get('tags', []) return { 'url': original_url, 'embedUrl': embed_url, 'title': title, 'owner': owner, 'name': name, # Space 이름 추가 저장 'likes_count': likes_count, 'tags': tags, 'rank': offset + index + 1 } except Exception as e: print(f"Error processing space data: {e}") # 오류 발생 시에도 기본 객체 반환 return { 'url': 'https://huggingface.co/spaces', 'embedUrl': 'https://huggingface.co/spaces', 'title': 'Error Loading Space', 'owner': 'huggingface', 'name': 'error', 'likes_count': 0, 'tags': [], 'rank': offset + index + 1 } # Homepage route @app.route('/') def home(): return render_template('index.html') # Trending spaces API @app.route('/api/trending-spaces', methods=['GET']) def trending_spaces(): search_query = request.args.get('search', '').lower() offset = int(request.args.get('offset', 0)) limit = int(request.args.get('limit', 72)) # 기본값 72개로 변경 # Fetch trending spaces spaces_data = fetch_trending_spaces(offset, limit) # Process and filter spaces results = [] for index, space_data in enumerate(spaces_data['spaces']): space_info = get_space_details(space_data, index, offset) if not space_info: continue # Apply search filter if needed if search_query: title = space_info['title'].lower() owner = space_info['owner'].lower() url = space_info['url'].lower() tags = ' '.join([str(tag) for tag in space_info.get('tags', [])]).lower() if (search_query not in title and search_query not in owner and search_query not in url and search_query not in tags): continue results.append(space_info) return jsonify({ 'spaces': results, 'total': spaces_data['total'], 'offset': offset, 'limit': limit }) if __name__ == '__main__': # Create templates folder os.makedirs('templates', exist_ok=True) # Create index.html file with open('templates/index.html', 'w', encoding='utf-8') as f: f.write('''
Discover the top 500 trending spaces from the Huggingface