Spaces:
Runtime error
Runtime error
Update app.py
Browse files5 unmodified lines678910111213141516171819202122232425262728293034 unmodified lines65666768697071727374757677788 unmodified lines8788899091929394 unmodified lines188189190191192193194193 unmodified lines388389390391392393394395102 unmodified lines4984995005015025035045055065075085095105115 unmodified linesapp = Flask(__name__)# Function to fetch trending spaces from Huggingfacedef fetch_trending_spaces(limit=100):try:url = "https://huggingface.co/api/spaces"params = {"limit": limit,"sort": "trending","direction": -1}response = requests.get(url, params=params, timeout=10)if response.status_code == 200:return response.json()else:print(f"Error fetching trending spaces: {response.status_code}")return []except Exception as e:print(f"Exception when fetching trending spaces: {e}")return []# Transform Huggingface URL to direct space URLdef transform_url(owner, name):34 unmodified linesdef 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()limit = int(request.args.get('limit', 100))# Fetch trending spacesspaces_data = fetch_trending_spaces(limit)# Process and filter spacesresults = []8 unmodified linestitle = space_info['title'].lower()owner = space_info['owner'].lower()url = space_info['url'].lower()tags = ' '.join(space_info.get('tags', [])).lower()if (search_query not in title andsearch_query not in owner and94 unmodified linesborder: 2px solid white;border-top-color: transparent;border-radius: 50%;animation: spin 1s linear infinite;}.refreshing .refresh-icon {193 unmodified linesreturn response.json();}// Load trending spacesasync function loadTrendingSpaces() {setLoading(true);try {102 unmodified lineselements.searchInput.addEventListener('input', () => {// Debounce input to prevent API calls on every keystrokeclearTimeout(state.searchTimeout);state.searchTimeout = setTimeout(loadTrendingSpaces, 300);});// Refresh button event listenerelements.refreshButton.addEventListener('click', loadTrendingSpaces);// InitializeloadTrendingSpaces();</script></body></html>5 unmodified lines6789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676834 unmodified lines1031041051061071081091101111121131141151168 unmodified lines12512612712812913013194 unmodified lines226227228229230231232193 unmodified lines426427428429430431432433102 unmodified lines5365375385395405415425435445455465475485495 unmodified linesapp = Flask(__name__)# Function to fetch spaces from Huggingfacedef fetch_spaces(limit=100):try:# Get trending spaces sorted by likes count (since trending API is giving 400 error)url = "https://huggingface.co/api/spaces"params = {"limit": limit,"sort": "likes","order": "desc"}response = requests.get(url, params=params, timeout=10)if response.status_code == 200:return response.json()else:print(f"Error fetching spaces: {response.status_code}")# Fallback to hardcoded list if API failsreturn get_fallback_spaces()except Exception as e:print(f"Exception when fetching spaces: {e}")# Fallback to hardcoded list if API failsreturn get_fallback_spaces()# Fallback function to provide some spaces if the API call failsdef get_fallback_spaces():return [{"id": "stabilityai/stable-diffusion-xl-base-1.0","owner": "stabilityai","title": "Stable Diffusion XL Base 1.0","likes": 4857},{"id": "meta-llama/Llama-2-7b-chat-hf","owner": "meta-llama","title": "Llama 2 7B Chat","likes": 3821},{"id": "mistralai/Mistral-7B-Instruct-v0.2","owner": "mistralai","title": "Mistral 7B Instruct v0.2","likes": 2953},{"id": "runwayml/stable-diffusion-v1-5","owner": "runwayml","title": "Stable Diffusion v1.5","likes": 2649},{"id": "microsoft/phi-2","owner": "microsoft","title": "Phi-2","likes": 2134}]# Transform Huggingface URL to direct space URLdef transform_url(owner, name):34 unmodified linesdef home():return render_template('index.html')# Spaces API@app.route('/api/trending-spaces', methods=['GET'])def trending_spaces():search_query = request.args.get('search', '').lower()limit = int(request.args.get('limit', 100))# Fetch spacesspaces_data = fetch_spaces(limit)# Process and filter spacesresults = []8 unmodified linestitle = 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 andsearch_query not in owner and94 unmodified linesborder: 2px solid white;border-top-color: transparent;border-radius: 50%;animation: none;}.refreshing .refresh-icon {193 unmodified linesreturn response.json();}// Load spacesasync function loadSpaces() {setLoading(true);try {102 unmodified lineselements.searchInput.addEventListener('input', () => {// Debounce input to prevent API calls on every keystrokeclearTimeout(state.searchTimeout);state.searchTimeout = setTimeout(loadSpaces, 300);});// Refresh button event listenerelements.refreshButton.addEventListener('click', loadSpaces);// InitializeloadSpaces();</script></body></html>
app.py+56-18