Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ from typing import List, Dict, Union
|
|
6 |
import concurrent.futures
|
7 |
import base64
|
8 |
import traceback
|
|
|
9 |
|
10 |
# 환경 변수에서 토큰 가져오기
|
11 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
@@ -73,15 +74,12 @@ def format_space(space: Dict) -> Dict:
|
|
73 |
space_likes = space.get('likes', 'N/A')
|
74 |
space_url = f"https://huggingface.co/spaces/{space_id}"
|
75 |
|
76 |
-
thumbnail = capture_thumbnail(space_id)
|
77 |
-
|
78 |
return {
|
79 |
"id": space_id,
|
80 |
"name": space_name,
|
81 |
"author": space_author,
|
82 |
"likes": space_likes,
|
83 |
-
"url": space_url
|
84 |
-
"thumbnail": thumbnail
|
85 |
}
|
86 |
|
87 |
def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]:
|
@@ -108,45 +106,51 @@ def create_ui():
|
|
108 |
formatted_spaces = format_spaces(spaces_list)
|
109 |
print(f"Total spaces loaded: {len(formatted_spaces)}") # 디버깅 출력
|
110 |
|
111 |
-
|
|
|
112 |
|
113 |
with gr.Blocks() as demo:
|
114 |
gr.Markdown("# Hugging Face Most Liked Spaces")
|
115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
with gr.Row():
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
app_py_content = gr.Code(language="python", label="app.py 내용")
|
125 |
-
|
126 |
-
def on_select(choice):
|
127 |
try:
|
128 |
-
|
129 |
-
selected_space = next((space for space in formatted_spaces if f"{space['name']} by {space['author']} (Likes: {space['likes']})" == choice), None)
|
130 |
if selected_space:
|
131 |
-
app_content = get_app_py_content(
|
132 |
-
print(f"Selected space: {selected_space['name']} (ID: {
|
133 |
-
return f"선택된 Space: {selected_space['name']} (ID: {
|
134 |
else:
|
135 |
-
print(f"Space not found for
|
136 |
return "선택된 space를 찾을 수 없습니다.", ""
|
137 |
except Exception as e:
|
138 |
print(f"Error in on_select: {str(e)}")
|
139 |
print(traceback.format_exc()) # 상세한 오류 정보 출력
|
140 |
return f"오류가 발생했습니다: {str(e)}", ""
|
141 |
|
142 |
-
def on_summarize(
|
143 |
try:
|
144 |
-
if
|
145 |
-
selected_space = next((space for space in formatted_spaces if
|
146 |
if selected_space:
|
147 |
summary = summarize_space(selected_space)
|
148 |
print(f"Summarizing space: {selected_space['name']}") # 디버깅 출력
|
149 |
-
return summary
|
150 |
print("No space selected for summarization") # 디버깅 출력
|
151 |
return "선택된 space가 없습니다. 먼저 리스트에서 space를 선택해주세요."
|
152 |
except Exception as e:
|
@@ -154,25 +158,8 @@ def create_ui():
|
|
154 |
print(traceback.format_exc()) # 상세한 오류 정보 출력
|
155 |
return f"요약 중 오류가 발생했습니다: {str(e)}"
|
156 |
|
157 |
-
|
158 |
-
|
159 |
-
selected_space = next((space for space in formatted_spaces if f"{space['name']} by {space['author']} (Likes: {space['likes']})" == choice), None)
|
160 |
-
if selected_space:
|
161 |
-
return f"<script>window.open('{selected_space['url']}', '_blank');</script>"
|
162 |
-
return ""
|
163 |
-
|
164 |
-
space_dropdown.change(on_select, space_dropdown, [output, app_py_content])
|
165 |
-
summarize_btn.click(on_summarize, inputs=[space_dropdown], outputs=[output])
|
166 |
-
link_btn.click(open_space_link, inputs=[space_dropdown], outputs=[gr.HTML()])
|
167 |
-
|
168 |
-
# Add JavaScript to handle link opening
|
169 |
-
gr.HTML("""
|
170 |
-
<script>
|
171 |
-
function openSpaceLink(url) {
|
172 |
-
window.open(url, '_blank');
|
173 |
-
}
|
174 |
-
</script>
|
175 |
-
""")
|
176 |
|
177 |
return demo
|
178 |
|
|
|
6 |
import concurrent.futures
|
7 |
import base64
|
8 |
import traceback
|
9 |
+
import pandas as pd
|
10 |
|
11 |
# 환경 변수에서 토큰 가져오기
|
12 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
74 |
space_likes = space.get('likes', 'N/A')
|
75 |
space_url = f"https://huggingface.co/spaces/{space_id}"
|
76 |
|
|
|
|
|
77 |
return {
|
78 |
"id": space_id,
|
79 |
"name": space_name,
|
80 |
"author": space_author,
|
81 |
"likes": space_likes,
|
82 |
+
"url": space_url
|
|
|
83 |
}
|
84 |
|
85 |
def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]:
|
|
|
106 |
formatted_spaces = format_spaces(spaces_list)
|
107 |
print(f"Total spaces loaded: {len(formatted_spaces)}") # 디버깅 출력
|
108 |
|
109 |
+
df = pd.DataFrame(formatted_spaces)
|
110 |
+
df['Open'] = df['url'].apply(lambda x: f'<a href="{x}" target="_blank">🔗</a>')
|
111 |
|
112 |
with gr.Blocks() as demo:
|
113 |
gr.Markdown("# Hugging Face Most Liked Spaces")
|
114 |
|
115 |
+
space_table = gr.Dataframe(
|
116 |
+
value=df[['name', 'author', 'likes', 'Open']],
|
117 |
+
headers=['Name', 'Author', 'Likes', 'Open'],
|
118 |
+
row_count=(len(formatted_spaces), "fixed"),
|
119 |
+
col_count=(4, "fixed"),
|
120 |
+
interactive=False,
|
121 |
+
wrap=True
|
122 |
+
)
|
123 |
+
|
124 |
with gr.Row():
|
125 |
+
selected_space = gr.Dropdown(choices=[space['id'] for space in formatted_spaces], label="Select a Space for Summary")
|
126 |
+
summarize_btn = gr.Button("요약")
|
127 |
+
|
128 |
+
output = gr.Textbox(label="Space 정보 및 요약", lines=10)
|
129 |
+
app_py_content = gr.Code(language="python", label="app.py 내용")
|
130 |
+
|
131 |
+
def on_select(space_id):
|
|
|
|
|
|
|
132 |
try:
|
133 |
+
selected_space = next((space for space in formatted_spaces if space['id'] == space_id), None)
|
|
|
134 |
if selected_space:
|
135 |
+
app_content = get_app_py_content(space_id)
|
136 |
+
print(f"Selected space: {selected_space['name']} (ID: {space_id})") # 디버깅 출력
|
137 |
+
return f"선택된 Space: {selected_space['name']} (ID: {space_id})\nURL: {selected_space['url']}", app_content
|
138 |
else:
|
139 |
+
print(f"Space not found for ID: {space_id}") # 디버깅 출력
|
140 |
return "선택된 space를 찾을 수 없습니다.", ""
|
141 |
except Exception as e:
|
142 |
print(f"Error in on_select: {str(e)}")
|
143 |
print(traceback.format_exc()) # 상세한 오류 정보 출력
|
144 |
return f"오류가 발생했습니다: {str(e)}", ""
|
145 |
|
146 |
+
def on_summarize(space_id):
|
147 |
try:
|
148 |
+
if space_id:
|
149 |
+
selected_space = next((space for space in formatted_spaces if space['id'] == space_id), None)
|
150 |
if selected_space:
|
151 |
summary = summarize_space(selected_space)
|
152 |
print(f"Summarizing space: {selected_space['name']}") # 디버깅 출력
|
153 |
+
return f"Space: {selected_space['name']} by {selected_space['author']}\nLikes: {selected_space['likes']}\nURL: {selected_space['url']}\n\n요약:\n{summary}"
|
154 |
print("No space selected for summarization") # 디버깅 출력
|
155 |
return "선택된 space가 없습니다. 먼저 리스트에서 space를 선택해주세요."
|
156 |
except Exception as e:
|
|
|
158 |
print(traceback.format_exc()) # 상세한 오류 정보 출력
|
159 |
return f"요약 중 오류가 발생했습니다: {str(e)}"
|
160 |
|
161 |
+
selected_space.change(on_select, selected_space, [output, app_py_content])
|
162 |
+
summarize_btn.click(on_summarize, inputs=[selected_space], outputs=[output])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
return demo
|
165 |
|