ginipick commited on
Commit
1748922
·
verified ·
1 Parent(s): 4af25eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -9
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
  import os
4
  import requests
5
  from typing import List, Dict, Union
@@ -14,6 +14,7 @@ import json
14
 
15
  HF_TOKEN = os.getenv("HF_TOKEN")
16
  hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=HF_TOKEN)
 
17
 
18
  def get_headers():
19
  if not HF_TOKEN:
@@ -93,12 +94,36 @@ def get_app_py_content(space_id: str) -> str:
93
  return f"Error fetching app.py content for space: {space_id}"
94
 
95
  def get_space_structure(space_id: str) -> Dict:
96
- api_url = f"https://huggingface.co/api/spaces/{space_id}/tree"
97
  try:
98
- response = requests.get(api_url, headers=get_headers())
99
- response.raise_for_status()
100
- return response.json()
101
- except requests.RequestException as e:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  return {"error": f"API request error: {str(e)}"}
103
 
104
  def format_tree_structure(tree_data: Dict, indent: str = "") -> str:
@@ -333,6 +358,4 @@ if __name__ == "__main__":
333
  demo.launch()
334
  except Exception as e:
335
  print(f"Error in main: {str(e)}")
336
- print(traceback.format_exc())
337
-
338
-
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient, HfApi
3
  import os
4
  import requests
5
  from typing import List, Dict, Union
 
14
 
15
  HF_TOKEN = os.getenv("HF_TOKEN")
16
  hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=HF_TOKEN)
17
+ hf_api = HfApi(token=HF_TOKEN)
18
 
19
  def get_headers():
20
  if not HF_TOKEN:
 
94
  return f"Error fetching app.py content for space: {space_id}"
95
 
96
  def get_space_structure(space_id: str) -> Dict:
 
97
  try:
98
+ # space_id에서 owner와 repo_name을 분리
99
+ owner, repo_name = space_id.split('/')
100
+
101
+ # HfApi를 사용하여 파일 목록을 가져옴
102
+ files = hf_api.list_repo_files(repo_id=space_id, repo_type="space")
103
+
104
+ # 파일 목록을 트리 구조로 변환
105
+ tree = {"type": "directory", "path": "", "tree": []}
106
+ for file in files:
107
+ path_parts = file.split('/')
108
+ current = tree
109
+ for i, part in enumerate(path_parts):
110
+ if i == len(path_parts) - 1: # 파일
111
+ current["tree"].append({"type": "file", "path": part})
112
+ else: # 디렉토리
113
+ found = False
114
+ for item in current["tree"]:
115
+ if item["type"] == "directory" and item["path"] == part:
116
+ current = item
117
+ found = True
118
+ break
119
+ if not found:
120
+ new_dir = {"type": "directory", "path": part, "tree": []}
121
+ current["tree"].append(new_dir)
122
+ current = new_dir
123
+
124
+ return tree
125
+ except Exception as e:
126
+ print(f"Error in get_space_structure: {str(e)}")
127
  return {"error": f"API request error: {str(e)}"}
128
 
129
  def format_tree_structure(tree_data: Dict, indent: str = "") -> str:
 
358
  demo.launch()
359
  except Exception as e:
360
  print(f"Error in main: {str(e)}")
361
+ print(traceback.format_exc())