Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ import requests
|
|
5 |
from typing import List, Dict, Union
|
6 |
import concurrent.futures
|
7 |
import traceback
|
|
|
8 |
|
9 |
# 환경 변수에서 토큰 가져오기
|
10 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
@@ -40,6 +41,16 @@ def get_most_liked_spaces(limit: int = 100) -> Union[List[Dict], str]:
|
|
40 |
except ValueError as e:
|
41 |
return f"JSON decoding error: {str(e)}"
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
def format_space(space: Dict) -> Dict:
|
44 |
space_id = space.get('id', 'Unknown')
|
45 |
space_name = space_id.split('/')[-1] if '/' in space_id else space_id
|
@@ -51,12 +62,15 @@ def format_space(space: Dict) -> Dict:
|
|
51 |
space_likes = space.get('likes', 'N/A')
|
52 |
space_url = f"https://huggingface.co/spaces/{space_id}"
|
53 |
|
|
|
|
|
54 |
return {
|
55 |
"id": space_id,
|
56 |
"name": space_name,
|
57 |
"author": space_author,
|
58 |
"likes": space_likes,
|
59 |
-
"url": space_url
|
|
|
60 |
}
|
61 |
|
62 |
def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]:
|
@@ -105,6 +119,11 @@ def create_ui():
|
|
105 |
.space-row {
|
106 |
margin-bottom: 5px !important;
|
107 |
}
|
|
|
|
|
|
|
|
|
|
|
108 |
"""
|
109 |
|
110 |
with gr.Blocks(css=css) as demo:
|
@@ -114,13 +133,18 @@ def create_ui():
|
|
114 |
with gr.Column(scale=1):
|
115 |
for space in formatted_spaces:
|
116 |
with gr.Row(elem_classes="space-row"):
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
|
|
|
|
|
|
|
|
|
|
124 |
|
125 |
with gr.Column(scale=1):
|
126 |
info_output = gr.Textbox(label="Space 정보 및 요약", lines=10)
|
|
|
5 |
from typing import List, Dict, Union
|
6 |
import concurrent.futures
|
7 |
import traceback
|
8 |
+
import base64
|
9 |
|
10 |
# 환경 변수에서 토큰 가져오기
|
11 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
41 |
except ValueError as e:
|
42 |
return f"JSON decoding error: {str(e)}"
|
43 |
|
44 |
+
def capture_thumbnail(space_id: str) -> str:
|
45 |
+
screenshot_url = f"https://huggingface.co/spaces/{space_id}/thumbnail.png"
|
46 |
+
try:
|
47 |
+
response = requests.get(screenshot_url, headers=get_headers())
|
48 |
+
if response.status_code == 200:
|
49 |
+
return response.content
|
50 |
+
except requests.RequestException:
|
51 |
+
pass
|
52 |
+
return None
|
53 |
+
|
54 |
def format_space(space: Dict) -> Dict:
|
55 |
space_id = space.get('id', 'Unknown')
|
56 |
space_name = space_id.split('/')[-1] if '/' in space_id else space_id
|
|
|
62 |
space_likes = space.get('likes', 'N/A')
|
63 |
space_url = f"https://huggingface.co/spaces/{space_id}"
|
64 |
|
65 |
+
thumbnail = capture_thumbnail(space_id)
|
66 |
+
|
67 |
return {
|
68 |
"id": space_id,
|
69 |
"name": space_name,
|
70 |
"author": space_author,
|
71 |
"likes": space_likes,
|
72 |
+
"url": space_url,
|
73 |
+
"thumbnail": thumbnail
|
74 |
}
|
75 |
|
76 |
def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]:
|
|
|
119 |
.space-row {
|
120 |
margin-bottom: 5px !important;
|
121 |
}
|
122 |
+
.thumbnail {
|
123 |
+
max-width: 100px;
|
124 |
+
max-height: 100px;
|
125 |
+
object-fit: contain;
|
126 |
+
}
|
127 |
"""
|
128 |
|
129 |
with gr.Blocks(css=css) as demo:
|
|
|
133 |
with gr.Column(scale=1):
|
134 |
for space in formatted_spaces:
|
135 |
with gr.Row(elem_classes="space-row"):
|
136 |
+
if space['thumbnail']:
|
137 |
+
gr.Image(value=space['thumbnail'], show_label=False, elem_classes="thumbnail")
|
138 |
+
else:
|
139 |
+
gr.Markdown("No thumbnail")
|
140 |
+
with gr.Column():
|
141 |
+
gr.Markdown(f"{space['name']} by {space['author']} (Likes: {space['likes']})", elem_classes="space-info")
|
142 |
+
button = gr.Button("클릭", elem_classes="minimal-button")
|
143 |
+
button.click(
|
144 |
+
lambda s=space: on_select(s),
|
145 |
+
inputs=[],
|
146 |
+
outputs=[info_output, app_py_content]
|
147 |
+
)
|
148 |
|
149 |
with gr.Column(scale=1):
|
150 |
info_output = gr.Textbox(label="Space 정보 및 요약", lines=10)
|