ginipick's picture
Update app.py
240c790 verified
raw
history blame
7.79 kB
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)