Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,25 +6,63 @@ import time
|
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
-
# Function to fetch
|
| 10 |
-
def
|
| 11 |
try:
|
|
|
|
| 12 |
url = "https://huggingface.co/api/spaces"
|
| 13 |
params = {
|
| 14 |
"limit": limit,
|
| 15 |
-
"sort": "
|
| 16 |
-
"
|
| 17 |
}
|
| 18 |
response = requests.get(url, params=params, timeout=10)
|
| 19 |
|
| 20 |
if response.status_code == 200:
|
| 21 |
return response.json()
|
| 22 |
else:
|
| 23 |
-
print(f"Error fetching
|
| 24 |
-
|
|
|
|
| 25 |
except Exception as e:
|
| 26 |
-
print(f"Exception when fetching
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# Transform Huggingface URL to direct space URL
|
| 30 |
def transform_url(owner, name):
|
|
@@ -65,14 +103,14 @@ def get_space_details(space_data):
|
|
| 65 |
def home():
|
| 66 |
return render_template('index.html')
|
| 67 |
|
| 68 |
-
#
|
| 69 |
@app.route('/api/trending-spaces', methods=['GET'])
|
| 70 |
def trending_spaces():
|
| 71 |
search_query = request.args.get('search', '').lower()
|
| 72 |
limit = int(request.args.get('limit', 100))
|
| 73 |
|
| 74 |
-
# Fetch
|
| 75 |
-
spaces_data =
|
| 76 |
|
| 77 |
# Process and filter spaces
|
| 78 |
results = []
|
|
@@ -87,7 +125,7 @@ def trending_spaces():
|
|
| 87 |
title = space_info['title'].lower()
|
| 88 |
owner = space_info['owner'].lower()
|
| 89 |
url = space_info['url'].lower()
|
| 90 |
-
tags = ' '.join(space_info.get('tags', [])).lower()
|
| 91 |
|
| 92 |
if (search_query not in title and
|
| 93 |
search_query not in owner and
|
|
@@ -188,7 +226,7 @@ if __name__ == '__main__':
|
|
| 188 |
border: 2px solid white;
|
| 189 |
border-top-color: transparent;
|
| 190 |
border-radius: 50%;
|
| 191 |
-
animation:
|
| 192 |
}
|
| 193 |
|
| 194 |
.refreshing .refresh-icon {
|
|
@@ -388,8 +426,8 @@ if __name__ == '__main__':
|
|
| 388 |
return response.json();
|
| 389 |
}
|
| 390 |
|
| 391 |
-
// Load
|
| 392 |
-
async function
|
| 393 |
setLoading(true);
|
| 394 |
|
| 395 |
try {
|
|
@@ -498,14 +536,14 @@ if __name__ == '__main__':
|
|
| 498 |
elements.searchInput.addEventListener('input', () => {
|
| 499 |
// Debounce input to prevent API calls on every keystroke
|
| 500 |
clearTimeout(state.searchTimeout);
|
| 501 |
-
state.searchTimeout = setTimeout(
|
| 502 |
});
|
| 503 |
|
| 504 |
// Refresh button event listener
|
| 505 |
-
elements.refreshButton.addEventListener('click',
|
| 506 |
|
| 507 |
// Initialize
|
| 508 |
-
|
| 509 |
</script>
|
| 510 |
</body>
|
| 511 |
</html>
|
|
|
|
| 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):
|
|
|
|
| 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 = []
|
|
|
|
| 125 |
title = space_info['title'].lower()
|
| 126 |
owner = space_info['owner'].lower()
|
| 127 |
url = space_info['url'].lower()
|
| 128 |
+
tags = ' '.join([str(tag) for tag in space_info.get('tags', [])]).lower()
|
| 129 |
|
| 130 |
if (search_query not in title and
|
| 131 |
search_query not in owner and
|
|
|
|
| 226 |
border: 2px solid white;
|
| 227 |
border-top-color: transparent;
|
| 228 |
border-radius: 50%;
|
| 229 |
+
animation: none;
|
| 230 |
}
|
| 231 |
|
| 232 |
.refreshing .refresh-icon {
|
|
|
|
| 426 |
return response.json();
|
| 427 |
}
|
| 428 |
|
| 429 |
+
// Load spaces
|
| 430 |
+
async function loadSpaces() {
|
| 431 |
setLoading(true);
|
| 432 |
|
| 433 |
try {
|
|
|
|
| 536 |
elements.searchInput.addEventListener('input', () => {
|
| 537 |
// Debounce input to prevent API calls on every keystroke
|
| 538 |
clearTimeout(state.searchTimeout);
|
| 539 |
+
state.searchTimeout = setTimeout(loadSpaces, 300);
|
| 540 |
});
|
| 541 |
|
| 542 |
// Refresh button event listener
|
| 543 |
+
elements.refreshButton.addEventListener('click', loadSpaces);
|
| 544 |
|
| 545 |
// Initialize
|
| 546 |
+
loadSpaces();
|
| 547 |
</script>
|
| 548 |
</body>
|
| 549 |
</html>
|