ginipick commited on
Commit
240c790
·
verified ·
1 Parent(s): 5efe8ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -15
app.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
- spaces_list = get_most_liked_spaces()
87
- formatted_spaces = format_spaces(spaces_list)
88
-
 
 
 
89
  html_template = """
90
  <!DOCTYPE html>
91
  <html lang="en">
@@ -158,10 +168,13 @@ def index():
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 +187,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
- content = get_app_py_content(space_id)
180
- return jsonify({'content': content})
 
 
 
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">
 
168
  var selector = document.getElementById("spaceSelector");
169
  var spaceId = selector.value;
170
  if (spaceId) {
171
+ fetch('/app_content/' + encodeURIComponent(spaceId))
172
  .then(response => response.json())
173
  .then(data => {
174
  document.getElementById("appContent").textContent = data.content;
175
+ })
176
+ .catch(error => {
177
+ document.getElementById("appContent").textContent = "Error fetching app.py content: " + error;
178
  });
179
  } else {
180
  document.getElementById("appContent").textContent = "";
 
187
 
188
  return render_template_string(html_template, spaces=formatted_spaces)
189
 
190
+ @app.route('/app_content/<path:space_id>')
191
  def app_content(space_id):
192
+ try:
193
+ content = get_app_py_content(space_id)
194
+ return jsonify({'content': content})
195
+ except ValueError as e:
196
+ return jsonify({'error': str(e)}), 500
197
 
198
  if __name__ == "__main__":
199
  app.run(host='0.0.0.0', port=7860)