Update app-backup3.py
Browse files- app-backup3.py +31 -16
app-backup3.py
CHANGED
|
@@ -3,9 +3,18 @@ import concurrent.futures
|
|
| 3 |
from flask import Flask, render_template_string, jsonify
|
| 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 = {
|
|
@@ -16,7 +25,7 @@ def get_most_liked_spaces(limit: int = 100) -> Union[List[Dict], str]:
|
|
| 16 |
}
|
| 17 |
|
| 18 |
try:
|
| 19 |
-
response = requests.get(url, params=params)
|
| 20 |
response.raise_for_status()
|
| 21 |
data = response.json()
|
| 22 |
|
|
@@ -32,7 +41,7 @@ def get_most_liked_spaces(limit: int = 100) -> Union[List[Dict], str]:
|
|
| 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:
|
|
@@ -42,13 +51,13 @@ def capture_thumbnail(space_id: str) -> str:
|
|
| 42 |
def get_app_py_content(space_id: str) -> str:
|
| 43 |
app_py_url = f"https://huggingface.co/spaces/{space_id}/raw/main/app.py"
|
| 44 |
try:
|
| 45 |
-
response = requests.get(app_py_url)
|
| 46 |
if response.status_code == 200:
|
| 47 |
return response.text
|
| 48 |
else:
|
| 49 |
-
return "app.py file not found or inaccessible"
|
| 50 |
except requests.RequestException:
|
| 51 |
-
return "Error fetching app.py content"
|
| 52 |
|
| 53 |
def format_space(space: Dict) -> Dict:
|
| 54 |
space_id = space.get('id', 'Unknown')
|
|
@@ -62,7 +71,6 @@ def format_space(space: Dict) -> Dict:
|
|
| 62 |
space_url = f"https://huggingface.co/spaces/{space_id}"
|
| 63 |
|
| 64 |
thumbnail = capture_thumbnail(space_id)
|
| 65 |
-
app_py_content = get_app_py_content(space_id)
|
| 66 |
|
| 67 |
return {
|
| 68 |
"id": space_id,
|
|
@@ -70,8 +78,7 @@ def format_space(space: Dict) -> Dict:
|
|
| 70 |
"author": space_author,
|
| 71 |
"likes": space_likes,
|
| 72 |
"url": space_url,
|
| 73 |
-
"thumbnail": thumbnail
|
| 74 |
-
"app_py_content": app_py_content
|
| 75 |
}
|
| 76 |
|
| 77 |
def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]:
|
|
@@ -83,9 +90,12 @@ def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]:
|
|
| 83 |
|
| 84 |
@app.route('/')
|
| 85 |
def index():
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
| 89 |
html_template = """
|
| 90 |
<!DOCTYPE html>
|
| 91 |
<html lang="en">
|
|
@@ -153,15 +163,17 @@ def index():
|
|
| 153 |
document.getElementById(tabName).style.display = "block";
|
| 154 |
evt.currentTarget.className += " active";
|
| 155 |
}
|
| 156 |
-
|
| 157 |
function showAppContent() {
|
| 158 |
var selector = document.getElementById("spaceSelector");
|
| 159 |
var spaceId = selector.value;
|
| 160 |
if (spaceId) {
|
| 161 |
-
fetch('/app_content/' + spaceId)
|
| 162 |
.then(response => response.json())
|
| 163 |
.then(data => {
|
| 164 |
document.getElementById("appContent").textContent = data.content;
|
|
|
|
|
|
|
|
|
|
| 165 |
});
|
| 166 |
} else {
|
| 167 |
document.getElementById("appContent").textContent = "";
|
|
@@ -174,10 +186,13 @@ def index():
|
|
| 174 |
|
| 175 |
return render_template_string(html_template, spaces=formatted_spaces)
|
| 176 |
|
| 177 |
-
@app.route('/app_content/<space_id>')
|
| 178 |
def app_content(space_id):
|
| 179 |
-
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
if __name__ == "__main__":
|
| 183 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 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 = {
|
|
|
|
| 25 |
}
|
| 26 |
|
| 27 |
try:
|
| 28 |
+
response = requests.get(url, params=params, headers=get_headers())
|
| 29 |
response.raise_for_status()
|
| 30 |
data = response.json()
|
| 31 |
|
|
|
|
| 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:
|
|
|
|
| 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')
|
|
|
|
| 71 |
space_url = f"https://huggingface.co/spaces/{space_id}"
|
| 72 |
|
| 73 |
thumbnail = capture_thumbnail(space_id)
|
|
|
|
| 74 |
|
| 75 |
return {
|
| 76 |
"id": space_id,
|
|
|
|
| 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]:
|
|
|
|
| 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">
|
|
|
|
| 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 = "";
|
|
|
|
| 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)
|