openfree commited on
Commit
0c4189b
·
verified ·
1 Parent(s): db22c56

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -17
app.py CHANGED
@@ -239,6 +239,62 @@ def get_hardware_info(item: dict) -> tuple:
239
  print(f"Error parsing hardware info: {str(e)}")
240
  return 'Standard', 'None', 'N/A'
241
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
  def get_card(item: dict, index: int, card_type: str = "space") -> str:
243
  """통합 카드 HTML 생성"""
244
  item_id = item.get('id', '')
@@ -246,25 +302,21 @@ def get_card(item: dict, index: int, card_type: str = "space") -> str:
246
  likes = format(item.get('likes', 0), ',')
247
  created = item.get('createdAt', '').split('T')[0]
248
 
249
- # short_description 가져오기
250
- short_description = item.get('cardData', {}).get('short_description', '')
251
 
252
  # title과 short_description을 포함한 헤더 HTML
253
  title_html = f"""
254
- <h3 style='
255
- margin: 0 0 15px 0;
256
- color: #333;
257
- font-size: 1.3em;
258
- line-height: 1.4;
259
- display: -webkit-box;
260
- -webkit-line-clamp: 2;
261
- -webkit-box-orient: vertical;
262
- overflow: hidden;
263
- text-overflow: ellipsis;
264
- text-shadow: 1px 1px 1px rgba(255,255,255,0.8);'>
265
- {title}
266
- {f'<span style="display: block; font-size: 0.7em; color: #666; margin-top: 5px; font-weight: normal; font-style: italic;">{short_description}</span>' if short_description else ''}
267
- </h3>
268
  """
269
 
270
 
@@ -466,7 +518,7 @@ def get_trending_spaces(search_query="", sort_by="rank", progress=gr.Progress())
466
  progress(0, desc="Fetching spaces data...")
467
  params = {
468
  'full': 'true',
469
- 'limit': 100
470
  }
471
 
472
  response = requests.get(url, params=params)
 
239
  print(f"Error parsing hardware info: {str(e)}")
240
  return 'Standard', 'None', 'N/A'
241
 
242
+ # 저장소 정보 캐시
243
+ REPO_INFO_CACHE = {}
244
+
245
+ def get_repo_info(repo_id: str, repo_type: str) -> dict:
246
+ """저장소의 상세 정보를 가져오는 함수 (캐시 적용)"""
247
+ cache_key = f"{repo_type}:{repo_id}"
248
+
249
+ if cache_key in REPO_INFO_CACHE:
250
+ return REPO_INFO_CACHE[cache_key]
251
+
252
+ try:
253
+ # API 호출 코드는 동일
254
+ response = requests.get(url)
255
+ response.raise_for_status()
256
+ repo_info = response.json()
257
+
258
+ # 캐시에 저장
259
+ REPO_INFO_CACHE[cache_key] = repo_info
260
+ return repo_info
261
+ except Exception as e:
262
+ print(f"Error fetching repo info for {repo_id}: {e}")
263
+ return {}
264
+
265
+
266
+
267
+ def get_short_description(item: dict, repo_type: str) -> str:
268
+ """README와 카드 메타데이터에서 설명 추출"""
269
+ try:
270
+ # 1. 먼저 cardData에서 확인
271
+ card_data = item.get('cardData', {})
272
+ if card_data and 'short_description' in card_data:
273
+ return card_data['short_description']
274
+
275
+ # 2. 저장소 상세 정보 가져오기
276
+ repo_id = item.get('id', '')
277
+ repo_info = get_repo_info(repo_id, repo_type)
278
+
279
+ # 3. tags와 siblings에서 설명 찾기
280
+ if repo_info.get('tags'):
281
+ return ', '.join(repo_info.get('tags')[:3]) # 상위 3개 태그만
282
+
283
+ # 4. README에서 찾기
284
+ readme = repo_info.get('readme', '')
285
+ if readme:
286
+ # README 파싱
287
+ paragraphs = readme.split('\n\n')
288
+ for p in paragraphs:
289
+ p = p.strip()
290
+ if p and not p.startswith('#') and not p.startswith('[!['):
291
+ return p[:150] + '...' if len(p) > 150 else p
292
+
293
+ return ''
294
+ except Exception as e:
295
+ print(f"Error getting description: {e}")
296
+ return ''
297
+
298
  def get_card(item: dict, index: int, card_type: str = "space") -> str:
299
  """통합 카드 HTML 생성"""
300
  item_id = item.get('id', '')
 
302
  likes = format(item.get('likes', 0), ',')
303
  created = item.get('createdAt', '').split('T')[0]
304
 
305
+ # short_description 가져오기 (수정된 부분)
306
+ short_description = get_short_description(item, card_type)
307
 
308
  # title과 short_description을 포함한 헤더 HTML
309
  title_html = f"""
310
+ <div style='margin-bottom: 15px;'>
311
+ <h3 style='
312
+ margin: 0;
313
+ color: #333;
314
+ font-size: 1.3em;
315
+ line-height: 1.4;'>
316
+ {title}
317
+ </h3>
318
+ {f'<p style="margin: 5px 0 0 0; font-size: 0.9em; color: #666; font-style: italic;">{short_description}</p>' if short_description else ''}
319
+ </div>
 
 
 
 
320
  """
321
 
322
 
 
518
  progress(0, desc="Fetching spaces data...")
519
  params = {
520
  'full': 'true',
521
+ 'limit': 10
522
  }
523
 
524
  response = requests.get(url, params=params)