openfree commited on
Commit
8f79fb3
1 Parent(s): 268e50d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -19
app.py CHANGED
@@ -574,13 +574,15 @@ def refresh_models(huggingface_token):
574
  model_info_response = requests.get(model_card_url, headers=headers)
575
  model_info = model_info_response.json()
576
 
577
- # samples 폴더의 숫자로 jpg 파일 찾기
578
- fs = HfFileSystem(token=huggingface_token)
579
- samples_path = f"{model_id}/samples"
580
 
581
  try:
 
 
 
582
  files = fs.ls(samples_path, detail=True)
583
- # 파일 이름만 추출
584
  jpg_files = [
585
  f['name'] for f in files
586
  if isinstance(f, dict) and
@@ -590,29 +592,37 @@ def refresh_models(huggingface_token):
590
  ]
591
 
592
  if jpg_files:
593
- # 숫자가 포함된 첫 번째 jpg 파일 사용
594
- image_url = f"https://huggingface.co/{model_id}/resolve/main/samples/{os.path.basename(jpg_files[0])}"
595
- print(f"Found image for {model_id}: {image_url}")
596
- else:
597
- # 기본 이미지 대신 첫 번째 발견된 jpg 사용
598
- image_url = f"https://huggingface.co/{model_id}/resolve/main/samples/1732195028106__000001000_0.jpg"
599
- print(f"Using default numbered image for {model_id}")
600
  except Exception as e:
601
  print(f"Error accessing samples folder for {model_id}: {str(e)}")
602
- image_url = f"https://huggingface.co/{model_id}/resolve/main/samples/1732195028106__000001000_0.jpg"
603
 
604
- # 트리거 워드 추출
605
- trigger_word = ""
606
- if 'instance_prompt' in model_info:
607
- trigger_word = model_info['instance_prompt']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
608
 
609
  model_info = {
610
  "image": image_url,
611
- "title": f"[Private] {model_id.split('/')[-1]}" if model.get('private') else model_id.split('/')[-1],
612
  "repo": model_id,
613
  "weights": "pytorch_lora_weights.safetensors",
614
- "trigger_word": trigger_word,
615
- "private": model.get('private', False)
616
  }
617
  new_models.append(model_info)
618
  print(f"Added model: {model_id} with image: {image_url}")
 
574
  model_info_response = requests.get(model_card_url, headers=headers)
575
  model_info = model_info_response.json()
576
 
577
+ # 이미지 URL에 토큰을 포함시키는 방식으로 변경
578
+ is_private = model.get('private', False)
579
+ base_image_name = "1732195028106__000001000_0.jpg" # 기본 이미지 이름
580
 
581
  try:
582
+ # 실제 이미지 파일 확인
583
+ fs = HfFileSystem(token=huggingface_token)
584
+ samples_path = f"{model_id}/samples"
585
  files = fs.ls(samples_path, detail=True)
 
586
  jpg_files = [
587
  f['name'] for f in files
588
  if isinstance(f, dict) and
 
592
  ]
593
 
594
  if jpg_files:
595
+ base_image_name = os.path.basename(jpg_files[0])
 
 
 
 
 
 
596
  except Exception as e:
597
  print(f"Error accessing samples folder for {model_id}: {str(e)}")
 
598
 
599
+ # 이미지 URL 구성 (토큰 포함)
600
+ if is_private:
601
+ # Private 모델의 경우 로컬 캐시 경로 사용
602
+ cache_dir = f"models/{model_id.replace('/', '_')}/samples"
603
+ os.makedirs(cache_dir, exist_ok=True)
604
+
605
+ # 이미지 다운로드
606
+ image_url = f"https://huggingface.co/{model_id}/resolve/main/samples/{base_image_name}"
607
+ local_image_path = os.path.join(cache_dir, base_image_name)
608
+
609
+ if not os.path.exists(local_image_path):
610
+ response = requests.get(image_url, headers=headers)
611
+ if response.status_code == 200:
612
+ with open(local_image_path, 'wb') as f:
613
+ f.write(response.content)
614
+
615
+ image_url = local_image_path
616
+ else:
617
+ image_url = f"https://huggingface.co/{model_id}/resolve/main/samples/{base_image_name}"
618
 
619
  model_info = {
620
  "image": image_url,
621
+ "title": f"[Private] {model_id.split('/')[-1]}" if is_private else model_id.split('/')[-1],
622
  "repo": model_id,
623
  "weights": "pytorch_lora_weights.safetensors",
624
+ "trigger_word": model_info.get('instance_prompt', ''),
625
+ "private": is_private
626
  }
627
  new_models.append(model_info)
628
  print(f"Added model: {model_id} with image: {image_url}")