Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
from flask import Flask, render_template_string
|
| 4 |
from typing import List, Dict, Union
|
| 5 |
import base64
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
-
|
| 10 |
url = "https://huggingface.co/api/spaces"
|
| 11 |
params = {
|
| 12 |
"sort": "likes",
|
|
@@ -15,70 +15,62 @@ async def get_most_liked_spaces(limit: int = 100) -> Union[List[Dict], str]:
|
|
| 15 |
"full": "true"
|
| 16 |
}
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
return f"JSON decoding error: {str(e)}"
|
| 32 |
|
| 33 |
-
|
| 34 |
screenshot_url = f"https://huggingface.co/spaces/{space_id}/screenshot.jpg"
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
except aiohttp.ClientError:
|
| 42 |
-
pass
|
| 43 |
return ""
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
if isinstance(spaces, str):
|
| 47 |
return [{"error": spaces}]
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
if not isinstance(space, dict):
|
| 52 |
-
formatted_spaces.append({"error": f"Unexpected space data format: {type(space)}"})
|
| 53 |
-
continue
|
| 54 |
-
|
| 55 |
-
space_id = space.get('id', 'Unknown')
|
| 56 |
-
space_name = space_id.split('/')[-1] if '/' in space_id else space_id
|
| 57 |
-
|
| 58 |
-
space_author = space.get('author', 'Unknown')
|
| 59 |
-
if isinstance(space_author, dict):
|
| 60 |
-
space_author = space_author.get('user', space_author.get('name', 'Unknown'))
|
| 61 |
-
|
| 62 |
-
space_likes = space.get('likes', 'N/A')
|
| 63 |
-
space_url = f"https://huggingface.co/spaces/{space_id}"
|
| 64 |
-
|
| 65 |
-
thumbnail = await capture_thumbnail(space_id)
|
| 66 |
-
|
| 67 |
-
formatted_space = {
|
| 68 |
-
"name": space_name,
|
| 69 |
-
"author": space_author,
|
| 70 |
-
"likes": space_likes,
|
| 71 |
-
"url": space_url,
|
| 72 |
-
"thumbnail": thumbnail
|
| 73 |
-
}
|
| 74 |
-
formatted_spaces.append(formatted_space)
|
| 75 |
-
|
| 76 |
-
return formatted_spaces
|
| 77 |
|
| 78 |
@app.route('/')
|
| 79 |
-
|
| 80 |
-
spaces_list =
|
| 81 |
-
formatted_spaces =
|
| 82 |
|
| 83 |
html_template = """
|
| 84 |
<!DOCTYPE html>
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import concurrent.futures
|
| 3 |
from flask import Flask, render_template_string
|
| 4 |
from typing import List, Dict, Union
|
| 5 |
import base64
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
+
def get_most_liked_spaces(limit: int = 100) -> Union[List[Dict], str]:
|
| 10 |
url = "https://huggingface.co/api/spaces"
|
| 11 |
params = {
|
| 12 |
"sort": "likes",
|
|
|
|
| 15 |
"full": "true"
|
| 16 |
}
|
| 17 |
|
| 18 |
+
try:
|
| 19 |
+
response = requests.get(url, params=params)
|
| 20 |
+
response.raise_for_status()
|
| 21 |
+
data = response.json()
|
| 22 |
+
|
| 23 |
+
if isinstance(data, list):
|
| 24 |
+
return data
|
| 25 |
+
else:
|
| 26 |
+
return f"Unexpected API response format: {type(data)}"
|
| 27 |
+
except requests.RequestException as e:
|
| 28 |
+
return f"API request error: {str(e)}"
|
| 29 |
+
except ValueError as e:
|
| 30 |
+
return f"JSON decoding error: {str(e)}"
|
|
|
|
| 31 |
|
| 32 |
+
def capture_thumbnail(space_id: str) -> str:
|
| 33 |
screenshot_url = f"https://huggingface.co/spaces/{space_id}/screenshot.jpg"
|
| 34 |
+
try:
|
| 35 |
+
response = requests.get(screenshot_url)
|
| 36 |
+
if response.status_code == 200:
|
| 37 |
+
return base64.b64encode(response.content).decode('utf-8')
|
| 38 |
+
except requests.RequestException:
|
| 39 |
+
pass
|
|
|
|
|
|
|
| 40 |
return ""
|
| 41 |
|
| 42 |
+
def format_space(space: Dict) -> Dict:
|
| 43 |
+
space_id = space.get('id', 'Unknown')
|
| 44 |
+
space_name = space_id.split('/')[-1] if '/' in space_id else space_id
|
| 45 |
+
|
| 46 |
+
space_author = space.get('author', 'Unknown')
|
| 47 |
+
if isinstance(space_author, dict):
|
| 48 |
+
space_author = space_author.get('user', space_author.get('name', 'Unknown'))
|
| 49 |
+
|
| 50 |
+
space_likes = space.get('likes', 'N/A')
|
| 51 |
+
space_url = f"https://huggingface.co/spaces/{space_id}"
|
| 52 |
+
|
| 53 |
+
thumbnail = capture_thumbnail(space_id)
|
| 54 |
+
|
| 55 |
+
return {
|
| 56 |
+
"name": space_name,
|
| 57 |
+
"author": space_author,
|
| 58 |
+
"likes": space_likes,
|
| 59 |
+
"url": space_url,
|
| 60 |
+
"thumbnail": thumbnail
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]:
|
| 64 |
if isinstance(spaces, str):
|
| 65 |
return [{"error": spaces}]
|
| 66 |
|
| 67 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
|
| 68 |
+
return list(executor.map(format_space, spaces))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
@app.route('/')
|
| 71 |
+
def index():
|
| 72 |
+
spaces_list = get_most_liked_spaces()
|
| 73 |
+
formatted_spaces = format_spaces(spaces_list)
|
| 74 |
|
| 75 |
html_template = """
|
| 76 |
<!DOCTYPE html>
|