seawolf2357 commited on
Commit
98d3d46
·
verified ·
1 Parent(s): 4f82889

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -44
app.py CHANGED
@@ -1,8 +1,7 @@
1
- from flask import Flask, render_template, request, redirect, url_for, jsonify, session, Response
2
  import requests
3
  import os
4
  from datetime import timedelta
5
- from urllib.parse import urlparse, urljoin
6
 
7
  app = Flask(__name__)
8
  app.secret_key = os.urandom(24) # Session encryption key
@@ -31,38 +30,14 @@ HUGGINGFACE_URLS = [
31
  "https://huggingface.co/spaces/openfree/Article-Generator",
32
  ]
33
 
34
- # Extract model/space info from URL
35
- def extract_model_info(url):
36
  parts = url.split('/')
37
- if len(parts) < 6:
38
- return None
39
-
40
- if parts[3] == 'spaces' or parts[3] == 'models':
41
- return {
42
- 'type': parts[3],
43
- 'owner': parts[4],
44
- 'repo': parts[5],
45
- 'full_id': f"{parts[4]}/{parts[5]}"
46
- }
47
- elif len(parts) >= 5:
48
- # Other URL format
49
- return {
50
- 'type': 'models', # Default
51
- 'owner': parts[3],
52
- 'repo': parts[4],
53
- 'full_id': f"{parts[3]}/{parts[4]}"
54
- }
55
-
56
- return None
57
-
58
- # Extract direct embed URL
59
- def get_embed_url(url):
60
- model_info = extract_model_info(url)
61
- if not model_info or model_info['type'] != 'spaces':
62
- return url
63
-
64
- # For spaces, use the embedded version
65
- return f"https://huggingface.co/spaces/{model_info['owner']}/{model_info['repo']}/embed"
66
 
67
  # Extract title from the last part of URL
68
  def extract_title(url):
@@ -145,19 +120,14 @@ def get_urls():
145
  results = []
146
  for url in HUGGINGFACE_URLS:
147
  title = extract_title(url)
148
- model_info = extract_model_info(url)
149
-
150
- if not model_info:
151
- continue
152
 
153
  if search_query and search_query not in url.lower() and search_query not in title.lower():
154
  continue
155
 
156
  results.append({
157
  'url': url,
158
- 'embedUrl': get_embed_url(url),
159
- 'title': title,
160
- 'model_info': model_info
161
  })
162
 
163
  return jsonify(results)
@@ -272,7 +242,7 @@ if __name__ == '__main__':
272
 
273
  .grid-container {
274
  display: grid;
275
- grid-template-columns: repeat(auto-fill, minmax(480px, 1fr));
276
  gap: 1.5rem;
277
  }
278
 
@@ -283,7 +253,7 @@ if __name__ == '__main__':
283
  box-shadow: 0 4px 15px rgba(0,0,0,0.1);
284
  display: flex;
285
  flex-direction: column;
286
- height: 650px;
287
  }
288
 
289
  .grid-header {
@@ -608,7 +578,7 @@ if __name__ == '__main__':
608
  }
609
 
610
  urls.forEach(item => {
611
- const { url, embedUrl, title } = item;
612
 
613
  // Create grid item
614
  const gridItem = document.createElement('div');
@@ -638,7 +608,7 @@ if __name__ == '__main__':
638
 
639
  // Create iframe to display the content
640
  const iframe = document.createElement('iframe');
641
- iframe.src = embedUrl; // Use the embed URL directly
642
  iframe.title = title;
643
  iframe.allow = 'accelerometer; camera; encrypted-media; geolocation; gyroscope; microphone; midi';
644
  iframe.setAttribute('allowfullscreen', '');
 
1
+ from flask import Flask, render_template, request, redirect, url_for, jsonify, session
2
  import requests
3
  import os
4
  from datetime import timedelta
 
5
 
6
  app = Flask(__name__)
7
  app.secret_key = os.urandom(24) # Session encryption key
 
30
  "https://huggingface.co/spaces/openfree/Article-Generator",
31
  ]
32
 
33
+ # Get direct iframe URL for Huggingface spaces
34
+ def get_iframe_url(url):
35
  parts = url.split('/')
36
+ if len(parts) >= 6 and parts[3] == 'spaces':
37
+ owner = parts[4]
38
+ repo = parts[5]
39
+ return f"https://{owner}-{repo}.hf.space"
40
+ return url
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  # Extract title from the last part of URL
43
  def extract_title(url):
 
120
  results = []
121
  for url in HUGGINGFACE_URLS:
122
  title = extract_title(url)
 
 
 
 
123
 
124
  if search_query and search_query not in url.lower() and search_query not in title.lower():
125
  continue
126
 
127
  results.append({
128
  'url': url,
129
+ 'iframeUrl': get_iframe_url(url),
130
+ 'title': title
 
131
  })
132
 
133
  return jsonify(results)
 
242
 
243
  .grid-container {
244
  display: grid;
245
+ grid-template-columns: repeat(auto-fill, minmax(450px, 1fr));
246
  gap: 1.5rem;
247
  }
248
 
 
253
  box-shadow: 0 4px 15px rgba(0,0,0,0.1);
254
  display: flex;
255
  flex-direction: column;
256
+ height: 600px;
257
  }
258
 
259
  .grid-header {
 
578
  }
579
 
580
  urls.forEach(item => {
581
+ const { url, iframeUrl, title } = item;
582
 
583
  // Create grid item
584
  const gridItem = document.createElement('div');
 
608
 
609
  // Create iframe to display the content
610
  const iframe = document.createElement('iframe');
611
+ iframe.src = iframeUrl;
612
  iframe.title = title;
613
  iframe.allow = 'accelerometer; camera; encrypted-media; geolocation; gyroscope; microphone; midi';
614
  iframe.setAttribute('allowfullscreen', '');