Update app.py
Browse files
app.py
CHANGED
|
@@ -21,10 +21,10 @@ def get_headers():
|
|
| 21 |
raise ValueError("Hugging Face token not found in environment variables")
|
| 22 |
return {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 23 |
|
| 24 |
-
def
|
| 25 |
url = "https://huggingface.co/api/spaces"
|
| 26 |
params = {
|
| 27 |
-
"sort": "likes",
|
| 28 |
"direction": -1,
|
| 29 |
"limit": limit,
|
| 30 |
"full": "true"
|
|
@@ -33,93 +33,40 @@ def get_popular_spaces(limit: int = 300) -> Union[List[Dict], str]:
|
|
| 33 |
try:
|
| 34 |
response = requests.get(url, params=params, headers=get_headers())
|
| 35 |
response.raise_for_status()
|
| 36 |
-
|
| 37 |
-
print(f"API Response (first 2 items): {json.dumps(data[:2], indent=2)}")
|
| 38 |
-
return data
|
| 39 |
except requests.RequestException as e:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
def get_fallback_spaces() -> List[Dict]:
|
| 44 |
-
return [
|
| 45 |
-
{
|
| 46 |
-
"id": "facebook/llama-2-7b-chat",
|
| 47 |
-
"name": "LLaMA 2 7B Chat",
|
| 48 |
-
"author": "Meta AI",
|
| 49 |
-
"likes": 1000,
|
| 50 |
-
"url": "https://huggingface.co/spaces/facebook/llama-2-7b-chat"
|
| 51 |
-
},
|
| 52 |
-
{
|
| 53 |
-
"id": "openai/whisper-large-v3",
|
| 54 |
-
"name": "Whisper Large V3",
|
| 55 |
-
"author": "OpenAI",
|
| 56 |
-
"likes": 800,
|
| 57 |
-
"url": "https://huggingface.co/spaces/openai/whisper-large-v3"
|
| 58 |
-
},
|
| 59 |
-
# Add more fallback spaces as needed
|
| 60 |
-
]
|
| 61 |
|
| 62 |
def format_space(space: Dict) -> Dict:
|
| 63 |
-
print(f"Formatting space: {json.dumps(space, indent=2)}")
|
| 64 |
-
|
| 65 |
space_id = space.get('id', 'Unknown')
|
| 66 |
-
space_name =
|
| 67 |
|
| 68 |
space_author = space.get('author', 'Unknown')
|
| 69 |
if isinstance(space_author, dict):
|
| 70 |
space_author = space_author.get('user', space_author.get('name', 'Unknown'))
|
| 71 |
|
| 72 |
space_likes = space.get('likes', 'N/A')
|
| 73 |
-
space_url =
|
| 74 |
|
| 75 |
-
|
| 76 |
"id": space_id,
|
| 77 |
"name": space_name,
|
| 78 |
"author": space_author,
|
| 79 |
"likes": space_likes,
|
| 80 |
"url": space_url,
|
| 81 |
}
|
| 82 |
-
print(f"Formatted space: {json.dumps(formatted_space, indent=2)}")
|
| 83 |
-
return formatted_space
|
| 84 |
|
| 85 |
def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]:
|
| 86 |
if isinstance(spaces, str):
|
| 87 |
-
|
| 88 |
-
return get_fallback_spaces()
|
| 89 |
|
| 90 |
-
|
| 91 |
-
for space in spaces:
|
| 92 |
-
if isinstance(space, dict):
|
| 93 |
-
try:
|
| 94 |
-
formatted.append(format_space(space))
|
| 95 |
-
except Exception as e:
|
| 96 |
-
print(f"Error formatting space: {str(e)}")
|
| 97 |
-
print(f"Problematic space data: {json.dumps(space, indent=2)}")
|
| 98 |
-
print(f"Total formatted spaces: {len(formatted)}")
|
| 99 |
-
return formatted if formatted else get_fallback_spaces()
|
| 100 |
-
|
| 101 |
-
def on_select(space):
|
| 102 |
-
try:
|
| 103 |
-
print(f"Selected space: {json.dumps(space, indent=2)}")
|
| 104 |
-
summary = summarize_space(space)
|
| 105 |
-
app_content = get_app_py_content(space['id'])
|
| 106 |
-
|
| 107 |
-
info = f"์ ํ๋ Space: {space.get('name', 'Unknown')} (ID: {space.get('id', 'Unknown')})\n"
|
| 108 |
-
info += f"Author: {space.get('author', 'Unknown')}\n"
|
| 109 |
-
info += f"Likes: {space.get('likes', 'N/A')}\n"
|
| 110 |
-
info += f"URL: {space.get('url', 'Unknown')}\n\n"
|
| 111 |
-
info += f"์์ฝ:\n{summary}"
|
| 112 |
-
|
| 113 |
-
print(f"Returning URL: {space.get('url', 'Unknown')}")
|
| 114 |
-
return info, app_content, space.get('url', ''), "", "" # Returning empty strings for tree and list views
|
| 115 |
-
except Exception as e:
|
| 116 |
-
print(f"Error in on_select: {str(e)}")
|
| 117 |
-
print(traceback.format_exc())
|
| 118 |
-
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}", "", "", "", ""
|
| 119 |
|
| 120 |
def summarize_space(space: Dict) -> str:
|
| 121 |
system_message = "๋น์ ์ Hugging Face Space์ ๋ด์ฉ์ ์์ฝํ๋ AI ์กฐ์์
๋๋ค. ์ฃผ์ด์ง ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ๊ฐ๊ฒฐํ๊ณ ๋ช
ํํ ์์ฝ์ ์ ๊ณตํด์ฃผ์ธ์."
|
| 122 |
-
user_message = f"๋ค์ Hugging Face Space๋ฅผ ์์ฝํด์ฃผ์ธ์: {space
|
| 123 |
|
| 124 |
messages = [
|
| 125 |
{"role": "system", "content": system_message},
|
|
@@ -137,12 +84,89 @@ def get_app_py_content(space_id: str) -> str:
|
|
| 137 |
try:
|
| 138 |
response = requests.get(app_py_url, headers=get_headers())
|
| 139 |
if response.status_code == 200:
|
| 140 |
-
return response.text
|
| 141 |
else:
|
| 142 |
return f"app.py file not found or inaccessible for space: {space_id}"
|
| 143 |
except requests.RequestException:
|
| 144 |
return f"Error fetching app.py content for space: {space_id}"
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
def update_screenshot(url, last_url, force_update=False):
|
| 147 |
print(f"Updating screenshot. Current URL: {url}, Last URL: {last_url}, Force update: {force_update}")
|
| 148 |
if url and (url != last_url or force_update):
|
|
@@ -189,9 +213,9 @@ def generate_usage_guide(app_content):
|
|
| 189 |
|
| 190 |
def create_ui():
|
| 191 |
try:
|
| 192 |
-
spaces_list =
|
| 193 |
formatted_spaces = format_spaces(spaces_list)
|
| 194 |
-
print(f"Total
|
| 195 |
|
| 196 |
css = """
|
| 197 |
footer {visibility: hidden;}
|
|
@@ -228,7 +252,7 @@ def create_ui():
|
|
| 228 |
"""
|
| 229 |
|
| 230 |
with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
|
| 231 |
-
gr.Markdown("# 300: HuggingFace
|
| 232 |
|
| 233 |
with gr.Row():
|
| 234 |
with gr.Column(scale=1):
|
|
@@ -236,8 +260,7 @@ def create_ui():
|
|
| 236 |
for space in formatted_spaces:
|
| 237 |
with gr.Row(elem_classes="space-row") as space_row:
|
| 238 |
with gr.Column():
|
| 239 |
-
|
| 240 |
-
gr.Markdown(space_info, elem_classes="space-info")
|
| 241 |
button = gr.Button("ํด๋ฆญ", elem_classes="minimal-button")
|
| 242 |
space_rows.append((space_row, button, space))
|
| 243 |
|
|
@@ -269,6 +292,7 @@ def create_ui():
|
|
| 269 |
|
| 270 |
update_trigger = gr.Button("Update Screenshot", visible=False)
|
| 271 |
|
|
|
|
| 272 |
def on_select_with_link(space):
|
| 273 |
info, app_content, url, tree, list_structure = on_select(space)
|
| 274 |
info += f"\n\n์ ํํ Space URL: {url}"
|
|
@@ -287,7 +311,6 @@ def create_ui():
|
|
| 287 |
|
| 288 |
def open_space_in_browser(url):
|
| 289 |
if url:
|
| 290 |
-
|
| 291 |
import webbrowser
|
| 292 |
webbrowser.open(url)
|
| 293 |
return f"'{url}' ์ฃผ์๊ฐ ์ ํญ์์ ์ด๋ ธ์ต๋๋ค."
|
|
@@ -342,4 +365,4 @@ if __name__ == "__main__":
|
|
| 342 |
demo.launch()
|
| 343 |
except Exception as e:
|
| 344 |
print(f"Error in main: {str(e)}")
|
| 345 |
-
print(traceback.format_exc())
|
|
|
|
| 21 |
raise ValueError("Hugging Face token not found in environment variables")
|
| 22 |
return {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 23 |
|
| 24 |
+
def get_most_liked_spaces(limit: int = 300) -> Union[List[Dict], str]:
|
| 25 |
url = "https://huggingface.co/api/spaces"
|
| 26 |
params = {
|
| 27 |
+
"sort": "likes",
|
| 28 |
"direction": -1,
|
| 29 |
"limit": limit,
|
| 30 |
"full": "true"
|
|
|
|
| 33 |
try:
|
| 34 |
response = requests.get(url, params=params, headers=get_headers())
|
| 35 |
response.raise_for_status()
|
| 36 |
+
return response.json()
|
|
|
|
|
|
|
| 37 |
except requests.RequestException as e:
|
| 38 |
+
return f"API request error: {str(e)}"
|
| 39 |
+
except ValueError as e:
|
| 40 |
+
return f"JSON decoding error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 45 |
|
| 46 |
space_author = space.get('author', 'Unknown')
|
| 47 |
if isinstance(space_author, dict):
|
| 48 |
space_author = space_author.get('user', space_author.get('name', 'Unknown'))
|
| 49 |
|
| 50 |
space_likes = space.get('likes', 'N/A')
|
| 51 |
+
space_url = f"https://huggingface.co/spaces/{space_id}"
|
| 52 |
|
| 53 |
+
return {
|
| 54 |
"id": space_id,
|
| 55 |
"name": space_name,
|
| 56 |
"author": space_author,
|
| 57 |
"likes": space_likes,
|
| 58 |
"url": space_url,
|
| 59 |
}
|
|
|
|
|
|
|
| 60 |
|
| 61 |
def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]:
|
| 62 |
if isinstance(spaces, str):
|
| 63 |
+
return [{"error": spaces}]
|
|
|
|
| 64 |
|
| 65 |
+
return [format_space(space) for space in spaces if isinstance(space, dict)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
def summarize_space(space: Dict) -> str:
|
| 68 |
system_message = "๋น์ ์ Hugging Face Space์ ๋ด์ฉ์ ์์ฝํ๋ AI ์กฐ์์
๋๋ค. ์ฃผ์ด์ง ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ๊ฐ๊ฒฐํ๊ณ ๋ช
ํํ ์์ฝ์ ์ ๊ณตํด์ฃผ์ธ์."
|
| 69 |
+
user_message = f"๋ค์ Hugging Face Space๋ฅผ ์์ฝํด์ฃผ์ธ์: {space['name']} by {space['author']}. ์ข์์ ์: {space['likes']}. URL: {space['url']}"
|
| 70 |
|
| 71 |
messages = [
|
| 72 |
{"role": "system", "content": system_message},
|
|
|
|
| 84 |
try:
|
| 85 |
response = requests.get(app_py_url, headers=get_headers())
|
| 86 |
if response.status_code == 200:
|
| 87 |
+
return response.text # ์ ์ฒด ๋ด์ฉ์ ๋ฐํ
|
| 88 |
else:
|
| 89 |
return f"app.py file not found or inaccessible for space: {space_id}"
|
| 90 |
except requests.RequestException:
|
| 91 |
return f"Error fetching app.py content for space: {space_id}"
|
| 92 |
|
| 93 |
+
|
| 94 |
+
def get_space_structure(space_id: str) -> Dict:
|
| 95 |
+
try:
|
| 96 |
+
# space_id์์ owner์ repo_name์ ๋ถ๋ฆฌ
|
| 97 |
+
owner, repo_name = space_id.split('/')
|
| 98 |
+
|
| 99 |
+
# HfApi๋ฅผ ์ฌ์ฉํ์ฌ ํ์ผ ๋ชฉ๋ก์ ๊ฐ์ ธ์ด
|
| 100 |
+
files = hf_api.list_repo_files(repo_id=space_id, repo_type="space")
|
| 101 |
+
|
| 102 |
+
# ํ์ผ ๋ชฉ๋ก์ ํธ๋ฆฌ ๊ตฌ์กฐ๋ก ๋ณํ
|
| 103 |
+
tree = {"type": "directory", "path": "", "tree": []}
|
| 104 |
+
for file in files:
|
| 105 |
+
path_parts = file.split('/')
|
| 106 |
+
current = tree
|
| 107 |
+
for i, part in enumerate(path_parts):
|
| 108 |
+
if i == len(path_parts) - 1: # ํ์ผ
|
| 109 |
+
current["tree"].append({"type": "file", "path": part})
|
| 110 |
+
else: # ๋๋ ํ ๋ฆฌ
|
| 111 |
+
found = False
|
| 112 |
+
for item in current["tree"]:
|
| 113 |
+
if item["type"] == "directory" and item["path"] == part:
|
| 114 |
+
current = item
|
| 115 |
+
found = True
|
| 116 |
+
break
|
| 117 |
+
if not found:
|
| 118 |
+
new_dir = {"type": "directory", "path": part, "tree": []}
|
| 119 |
+
current["tree"].append(new_dir)
|
| 120 |
+
current = new_dir
|
| 121 |
+
|
| 122 |
+
return tree
|
| 123 |
+
except Exception as e:
|
| 124 |
+
print(f"Error in get_space_structure: {str(e)}")
|
| 125 |
+
return {"error": f"API request error: {str(e)}"}
|
| 126 |
+
|
| 127 |
+
def format_tree_structure(tree_data: Dict, indent: str = "") -> str:
|
| 128 |
+
formatted = ""
|
| 129 |
+
for item in tree_data.get("tree", []):
|
| 130 |
+
if item["type"] == "file":
|
| 131 |
+
formatted += f"{indent}โโโ {item['path']}\n"
|
| 132 |
+
elif item["type"] == "directory":
|
| 133 |
+
formatted += f"{indent}โโโ {item['path']}/\n"
|
| 134 |
+
formatted += format_tree_structure(item, indent + "โ ")
|
| 135 |
+
return formatted
|
| 136 |
+
|
| 137 |
+
def format_list_structure(tree_data: Dict) -> List[str]:
|
| 138 |
+
formatted = []
|
| 139 |
+
for item in tree_data.get("tree", []):
|
| 140 |
+
if item["type"] == "file":
|
| 141 |
+
formatted.append(item["path"])
|
| 142 |
+
elif item["type"] == "directory":
|
| 143 |
+
formatted.append(f"{item['path']}/")
|
| 144 |
+
formatted.extend(format_list_structure(item))
|
| 145 |
+
return formatted
|
| 146 |
+
|
| 147 |
+
def on_select(space):
|
| 148 |
+
try:
|
| 149 |
+
print(f"Selected space: {space['name']}")
|
| 150 |
+
summary = summarize_space(space)
|
| 151 |
+
app_content = get_app_py_content(space['id'])
|
| 152 |
+
tree_structure = get_space_structure(space['id'])
|
| 153 |
+
|
| 154 |
+
info = f"์ ํ๋ Space: {space['name']} (ID: {space['id']})\n"
|
| 155 |
+
info += f"Author: {space['author']}\n"
|
| 156 |
+
info += f"Likes: {space['likes']}\n"
|
| 157 |
+
info += f"URL: {space['url']}\n\n"
|
| 158 |
+
info += f"์์ฝ:\n{summary}"
|
| 159 |
+
|
| 160 |
+
tree_view = format_tree_structure(tree_structure)
|
| 161 |
+
list_view = "\n".join(format_list_structure(tree_structure))
|
| 162 |
+
|
| 163 |
+
print(f"Returning URL: {space['url']}")
|
| 164 |
+
return info, app_content, space['url'], tree_view, list_view
|
| 165 |
+
except Exception as e:
|
| 166 |
+
print(f"Error in on_select: {str(e)}")
|
| 167 |
+
print(traceback.format_exc())
|
| 168 |
+
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}", "", "", "", ""
|
| 169 |
+
|
| 170 |
def update_screenshot(url, last_url, force_update=False):
|
| 171 |
print(f"Updating screenshot. Current URL: {url}, Last URL: {last_url}, Force update: {force_update}")
|
| 172 |
if url and (url != last_url or force_update):
|
|
|
|
| 213 |
|
| 214 |
def create_ui():
|
| 215 |
try:
|
| 216 |
+
spaces_list = get_most_liked_spaces()
|
| 217 |
formatted_spaces = format_spaces(spaces_list)
|
| 218 |
+
print(f"Total spaces loaded: {len(formatted_spaces)}")
|
| 219 |
|
| 220 |
css = """
|
| 221 |
footer {visibility: hidden;}
|
|
|
|
| 252 |
"""
|
| 253 |
|
| 254 |
with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
|
| 255 |
+
gr.Markdown("# 300: HuggingFace Most Liked Spaces")
|
| 256 |
|
| 257 |
with gr.Row():
|
| 258 |
with gr.Column(scale=1):
|
|
|
|
| 260 |
for space in formatted_spaces:
|
| 261 |
with gr.Row(elem_classes="space-row") as space_row:
|
| 262 |
with gr.Column():
|
| 263 |
+
gr.Markdown(f"{space['name']} by {space['author']} (Likes: {space['likes']})", elem_classes="space-info")
|
|
|
|
| 264 |
button = gr.Button("ํด๋ฆญ", elem_classes="minimal-button")
|
| 265 |
space_rows.append((space_row, button, space))
|
| 266 |
|
|
|
|
| 292 |
|
| 293 |
update_trigger = gr.Button("Update Screenshot", visible=False)
|
| 294 |
|
| 295 |
+
|
| 296 |
def on_select_with_link(space):
|
| 297 |
info, app_content, url, tree, list_structure = on_select(space)
|
| 298 |
info += f"\n\n์ ํํ Space URL: {url}"
|
|
|
|
| 311 |
|
| 312 |
def open_space_in_browser(url):
|
| 313 |
if url:
|
|
|
|
| 314 |
import webbrowser
|
| 315 |
webbrowser.open(url)
|
| 316 |
return f"'{url}' ์ฃผ์๊ฐ ์ ํญ์์ ์ด๋ ธ์ต๋๋ค."
|
|
|
|
| 365 |
demo.launch()
|
| 366 |
except Exception as e:
|
| 367 |
print(f"Error in main: {str(e)}")
|
| 368 |
+
print(traceback.format_exc())
|