Spaces:
Running
Running
File size: 7,784 Bytes
0041e1e 5efe8ef 0041e1e 5efe8ef fc11c0b 0041e1e 9665223 fc11c0b 06391aa 0041e1e fc11c0b 0041e1e 5efe8ef fc11c0b 5efe8ef fc11c0b 5efe8ef fc11c0b 5efe8ef fc11c0b 5efe8ef 0041e1e 5efe8ef 0041e1e 5efe8ef fc11c0b 5efe8ef 0041e1e 9665223 fc11c0b 9665223 5efe8ef 9665223 5efe8ef fc11c0b 5efe8ef fc11c0b 5efe8ef 9665223 0041e1e fc11c0b 5efe8ef fc11c0b 5efe8ef 0041e1e 9665223 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
import requests
import concurrent.futures
from flask import Flask, render_template_string, jsonify
from typing import List, Dict, Union
import base64
import os
app = Flask(__name__)
# 환경 변수에서 토큰 가져오기
HF_TOKEN = os.getenv("HF_TOKEN")
def get_headers():
if not HF_TOKEN:
raise ValueError("Hugging Face token not found in environment variables")
return {"Authorization": f"Bearer {HF_TOKEN}"}
def get_most_liked_spaces(limit: int = 100) -> Union[List[Dict], str]:
url = "https://huggingface.co/api/spaces"
params = {
"sort": "likes",
"direction": -1,
"limit": limit,
"full": "true"
}
try:
response = requests.get(url, params=params, headers=get_headers())
response.raise_for_status()
data = response.json()
if isinstance(data, list):
return data
else:
return f"Unexpected API response format: {type(data)}"
except requests.RequestException as e:
return f"API request error: {str(e)}"
except ValueError as e:
return f"JSON decoding error: {str(e)}"
def capture_thumbnail(space_id: str) -> str:
screenshot_url = f"https://huggingface.co/spaces/{space_id}/screenshot.jpg"
try:
response = requests.get(screenshot_url, headers=get_headers())
if response.status_code == 200:
return base64.b64encode(response.content).decode('utf-8')
except requests.RequestException:
pass
return ""
def get_app_py_content(space_id: str) -> str:
app_py_url = f"https://huggingface.co/spaces/{space_id}/raw/main/app.py"
try:
response = requests.get(app_py_url, headers=get_headers())
if response.status_code == 200:
return response.text
else:
return f"app.py file not found or inaccessible for space: {space_id}"
except requests.RequestException:
return f"Error fetching app.py content for space: {space_id}"
def format_space(space: Dict) -> Dict:
space_id = space.get('id', 'Unknown')
space_name = space_id.split('/')[-1] if '/' in space_id else space_id
space_author = space.get('author', 'Unknown')
if isinstance(space_author, dict):
space_author = space_author.get('user', space_author.get('name', 'Unknown'))
space_likes = space.get('likes', 'N/A')
space_url = f"https://huggingface.co/spaces/{space_id}"
thumbnail = capture_thumbnail(space_id)
return {
"id": space_id,
"name": space_name,
"author": space_author,
"likes": space_likes,
"url": space_url,
"thumbnail": thumbnail
}
def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]:
if isinstance(spaces, str):
return [{"error": spaces}]
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
return list(executor.map(format_space, spaces))
@app.route('/')
def index():
try:
spaces_list = get_most_liked_spaces()
formatted_spaces = format_spaces(spaces_list)
except ValueError as e:
return str(e), 500
html_template = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hugging Face Most Liked Spaces</title>
<style>
body { font-family: Arial, sans-serif; margin: 0; padding: 0; }
.container { max-width: 1200px; margin: 0 auto; padding: 20px; }
.space-item { margin-bottom: 20px; }
.space-item img { max-width: 200px; max-height: 200px; }
.tab { overflow: hidden; border: 1px solid #ccc; background-color: #f1f1f1; }
.tab button { background-color: inherit; float: left; border: none; outline: none; cursor: pointer; padding: 14px 16px; transition: 0.3s; }
.tab button:hover { background-color: #ddd; }
.tab button.active { background-color: #ccc; }
.tabcontent { display: none; padding: 6px 12px; border: 1px solid #ccc; border-top: none; }
pre { white-space: pre-wrap; word-wrap: break-word; }
</style>
</head>
<body>
<div class="container">
<h1>Hugging Face Most Liked Spaces</h1>
<div class="tab">
<button class="tablinks active" onclick="openTab(event, 'MostLiked')">Most Liked</button>
<button class="tablinks" onclick="openTab(event, 'AppContent')">App.py Content</button>
</div>
<div id="MostLiked" class="tabcontent" style="display: block;">
<ol>
{% for space in spaces %}
<li class="space-item">
<a href="{{ space.url }}" target="_blank">
{{ space.name }} by {{ space.author }} (Likes: {{ space.likes }})
</a>
{% if space.thumbnail %}
<br>
<img src="data:image/jpeg;base64,{{ space.thumbnail }}" alt="{{ space.name }} thumbnail">
{% endif %}
</li>
{% endfor %}
</ol>
</div>
<div id="AppContent" class="tabcontent">
<h2>Select a space to view its app.py content:</h2>
<select id="spaceSelector" onchange="showAppContent()">
<option value="">Select a space</option>
{% for space in spaces %}
<option value="{{ space.id }}">{{ space.name }} by {{ space.author }}</option>
{% endfor %}
</select>
<pre id="appContent"></pre>
</div>
</div>
<script>
function openTab(evt, tabName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
}
function showAppContent() {
var selector = document.getElementById("spaceSelector");
var spaceId = selector.value;
if (spaceId) {
fetch('/app_content/' + encodeURIComponent(spaceId))
.then(response => response.json())
.then(data => {
document.getElementById("appContent").textContent = data.content;
})
.catch(error => {
document.getElementById("appContent").textContent = "Error fetching app.py content: " + error;
});
} else {
document.getElementById("appContent").textContent = "";
}
}
</script>
</body>
</html>
"""
return render_template_string(html_template, spaces=formatted_spaces)
@app.route('/app_content/<path:space_id>')
def app_content(space_id):
try:
content = get_app_py_content(space_id)
return jsonify({'content': content})
except ValueError as e:
return jsonify({'error': str(e)}), 500
if __name__ == "__main__":
app.run(host='0.0.0.0', port=7860) |