ginipick commited on
Commit
5efe8ef
·
verified ·
1 Parent(s): 5233d34

Update app-backup3.py

Browse files
Files changed (1) hide show
  1. app-backup3.py +130 -28
app-backup3.py CHANGED
@@ -1,6 +1,8 @@
1
  import requests
2
- from flask import Flask, render_template_string
 
3
  from typing import List, Dict, Union
 
4
 
5
  app = Flask(__name__)
6
 
@@ -27,29 +29,57 @@ def get_most_liked_spaces(limit: int = 100) -> Union[List[Dict], str]:
27
  except ValueError as e:
28
  return f"JSON decoding error: {str(e)}"
29
 
30
- def format_spaces(spaces: Union[List[Dict], str]) -> List[str]:
31
- if isinstance(spaces, str):
32
- return [spaces] # 오류 메시지를 리스트로 반환
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
- formatted_spaces = []
35
- for space in spaces:
36
- if not isinstance(space, dict):
37
- formatted_spaces.append(f"Unexpected space data format: {type(space)}")
38
- continue
39
-
40
- space_id = space.get('id', 'Unknown')
41
- space_name = space_id.split('/')[-1] if '/' in space_id else space_id
42
-
43
- space_author = space.get('author', 'Unknown')
44
- if isinstance(space_author, dict):
45
- space_author = space_author.get('user', space_author.get('name', 'Unknown'))
46
-
47
- space_likes = space.get('likes', 'N/A')
48
-
49
- formatted_space = f"{space_name} by {space_author} (Likes: {space_likes})"
50
- formatted_spaces.append(formatted_space)
51
 
52
- return formatted_spaces
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  @app.route('/')
55
  def index():
@@ -63,19 +93,91 @@ def index():
63
  <meta charset="UTF-8">
64
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
65
  <title>Hugging Face Most Liked Spaces</title>
 
 
 
 
 
 
 
 
 
 
 
 
66
  </head>
67
  <body>
68
- <h1>Hugging Face Most Liked Spaces</h1>
69
- <ol>
70
- {% for space in spaces %}
71
- <li>{{ space }}</li>
72
- {% endfor %}
73
- </ol>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  </body>
75
  </html>
76
  """
77
 
78
  return render_template_string(html_template, spaces=formatted_spaces)
79
 
 
 
 
 
 
80
  if __name__ == "__main__":
81
  app.run(host='0.0.0.0', port=7860)
 
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
 
7
  app = Flask(__name__)
8
 
 
29
  except ValueError as e:
30
  return f"JSON decoding error: {str(e)}"
31
 
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:
39
+ pass
40
+ return ""
41
+
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')
55
+ space_name = space_id.split('/')[-1] if '/' in space_id else space_id
56
 
57
+ space_author = space.get('author', 'Unknown')
58
+ if isinstance(space_author, dict):
59
+ space_author = space_author.get('user', space_author.get('name', 'Unknown'))
60
+
61
+ space_likes = space.get('likes', 'N/A')
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,
69
+ "name": space_name,
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]:
78
+ if isinstance(spaces, str):
79
+ return [{"error": spaces}]
80
+
81
+ with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
82
+ return list(executor.map(format_space, spaces))
83
 
84
  @app.route('/')
85
  def index():
 
93
  <meta charset="UTF-8">
94
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
95
  <title>Hugging Face Most Liked Spaces</title>
96
+ <style>
97
+ body { font-family: Arial, sans-serif; margin: 0; padding: 0; }
98
+ .container { max-width: 1200px; margin: 0 auto; padding: 20px; }
99
+ .space-item { margin-bottom: 20px; }
100
+ .space-item img { max-width: 200px; max-height: 200px; }
101
+ .tab { overflow: hidden; border: 1px solid #ccc; background-color: #f1f1f1; }
102
+ .tab button { background-color: inherit; float: left; border: none; outline: none; cursor: pointer; padding: 14px 16px; transition: 0.3s; }
103
+ .tab button:hover { background-color: #ddd; }
104
+ .tab button.active { background-color: #ccc; }
105
+ .tabcontent { display: none; padding: 6px 12px; border: 1px solid #ccc; border-top: none; }
106
+ pre { white-space: pre-wrap; word-wrap: break-word; }
107
+ </style>
108
  </head>
109
  <body>
110
+ <div class="container">
111
+ <h1>Hugging Face Most Liked Spaces</h1>
112
+ <div class="tab">
113
+ <button class="tablinks active" onclick="openTab(event, 'MostLiked')">Most Liked</button>
114
+ <button class="tablinks" onclick="openTab(event, 'AppContent')">App.py Content</button>
115
+ </div>
116
+ <div id="MostLiked" class="tabcontent" style="display: block;">
117
+ <ol>
118
+ {% for space in spaces %}
119
+ <li class="space-item">
120
+ <a href="{{ space.url }}" target="_blank">
121
+ {{ space.name }} by {{ space.author }} (Likes: {{ space.likes }})
122
+ </a>
123
+ {% if space.thumbnail %}
124
+ <br>
125
+ <img src="data:image/jpeg;base64,{{ space.thumbnail }}" alt="{{ space.name }} thumbnail">
126
+ {% endif %}
127
+ </li>
128
+ {% endfor %}
129
+ </ol>
130
+ </div>
131
+ <div id="AppContent" class="tabcontent">
132
+ <h2>Select a space to view its app.py content:</h2>
133
+ <select id="spaceSelector" onchange="showAppContent()">
134
+ <option value="">Select a space</option>
135
+ {% for space in spaces %}
136
+ <option value="{{ space.id }}">{{ space.name }} by {{ space.author }}</option>
137
+ {% endfor %}
138
+ </select>
139
+ <pre id="appContent"></pre>
140
+ </div>
141
+ </div>
142
+ <script>
143
+ function openTab(evt, tabName) {
144
+ var i, tabcontent, tablinks;
145
+ tabcontent = document.getElementsByClassName("tabcontent");
146
+ for (i = 0; i < tabcontent.length; i++) {
147
+ tabcontent[i].style.display = "none";
148
+ }
149
+ tablinks = document.getElementsByClassName("tablinks");
150
+ for (i = 0; i < tablinks.length; i++) {
151
+ tablinks[i].className = tablinks[i].className.replace(" active", "");
152
+ }
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 = "";
168
+ }
169
+ }
170
+ </script>
171
  </body>
172
  </html>
173
  """
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)