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