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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -17
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import requests
2
  import concurrent.futures
3
- from flask import Flask, render_template_string
4
  from typing import List, Dict, Union
5
  import base64
6
 
@@ -39,6 +39,17 @@ def capture_thumbnail(space_id: str) -> str:
39
  pass
40
  return ""
41
 
 
 
 
 
 
 
 
 
 
 
 
42
  def format_space(space: Dict) -> Dict:
43
  space_id = space.get('id', 'Unknown')
44
  space_name = space_id.split('/')[-1] if '/' in space_id else space_id
@@ -51,13 +62,16 @@ def format_space(space: Dict) -> Dict:
51
  space_url = f"https://huggingface.co/spaces/{space_id}"
52
 
53
  thumbnail = capture_thumbnail(space_id)
 
54
 
55
  return {
 
56
  "name": space_name,
57
  "author": space_author,
58
  "likes": space_likes,
59
  "url": space_url,
60
- "thumbnail": thumbnail
 
61
  }
62
 
63
  def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]:
@@ -80,31 +94,90 @@ def index():
80
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
81
  <title>Hugging Face Most Liked Spaces</title>
82
  <style>
83
- body { font-family: Arial, sans-serif; }
 
84
  .space-item { margin-bottom: 20px; }
85
  .space-item img { max-width: 200px; max-height: 200px; }
 
 
 
 
 
 
86
  </style>
87
  </head>
88
  <body>
89
- <h1>Hugging Face Most Liked Spaces</h1>
90
- <ol>
91
- {% for space in spaces %}
92
- <li class="space-item">
93
- <a href="{{ space.url }}" target="_blank">
94
- {{ space.name }} by {{ space.author }} (Likes: {{ space.likes }})
95
- </a>
96
- {% if space.thumbnail %}
97
- <br>
98
- <img src="data:image/jpeg;base64,{{ space.thumbnail }}" alt="{{ space.name }} thumbnail">
99
- {% endif %}
100
- </li>
101
- {% endfor %}
102
- </ol>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  </body>
104
  </html>
105
  """
106
 
107
  return render_template_string(html_template, spaces=formatted_spaces)
108
 
 
 
 
 
 
109
  if __name__ == "__main__":
110
  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
 
 
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
 
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]:
 
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)