Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,9 +4,7 @@ import os
|
|
4 |
import requests
|
5 |
from typing import List, Dict, Union
|
6 |
import concurrent.futures
|
7 |
-
import base64
|
8 |
import traceback
|
9 |
-
import pandas as pd
|
10 |
|
11 |
# 환경 변수에서 토큰 가져오기
|
12 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
@@ -42,27 +40,6 @@ def get_most_liked_spaces(limit: int = 100) -> Union[List[Dict], str]:
|
|
42 |
except ValueError as e:
|
43 |
return f"JSON decoding error: {str(e)}"
|
44 |
|
45 |
-
def capture_thumbnail(space_id: str) -> str:
|
46 |
-
screenshot_url = f"https://huggingface.co/spaces/{space_id}/screenshot.jpg"
|
47 |
-
try:
|
48 |
-
response = requests.get(screenshot_url, headers=get_headers())
|
49 |
-
if response.status_code == 200:
|
50 |
-
return base64.b64encode(response.content).decode('utf-8')
|
51 |
-
except requests.RequestException:
|
52 |
-
pass
|
53 |
-
return ""
|
54 |
-
|
55 |
-
def get_app_py_content(space_id: str) -> str:
|
56 |
-
app_py_url = f"https://huggingface.co/spaces/{space_id}/raw/main/app.py"
|
57 |
-
try:
|
58 |
-
response = requests.get(app_py_url, headers=get_headers())
|
59 |
-
if response.status_code == 200:
|
60 |
-
return response.text
|
61 |
-
else:
|
62 |
-
return f"app.py file not found or inaccessible for space: {space_id}"
|
63 |
-
except requests.RequestException:
|
64 |
-
return f"Error fetching app.py content for space: {space_id}"
|
65 |
-
|
66 |
def format_space(space: Dict) -> Dict:
|
67 |
space_id = space.get('id', 'Unknown')
|
68 |
space_name = space_id.split('/')[-1] if '/' in space_id else space_id
|
@@ -89,6 +66,17 @@ def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]:
|
|
89 |
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
|
90 |
return list(executor.map(format_space, spaces))
|
91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
def summarize_space(space: Dict) -> str:
|
93 |
system_message = "당신은 Hugging Face Space의 내용을 요약하는 AI 조수입니다. 주어진 정보를 바탕으로 간결하고 명확한 요약을 제공해주세요."
|
94 |
user_message = f"다음 Hugging Face Space를 요약해주세요: {space['name']} by {space['author']}. 좋아요 수: {space['likes']}. URL: {space['url']}"
|
@@ -106,60 +94,57 @@ def create_ui():
|
|
106 |
formatted_spaces = format_spaces(spaces_list)
|
107 |
print(f"Total spaces loaded: {len(formatted_spaces)}") # 디버깅 출력
|
108 |
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
|
|
132 |
try:
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
|
|
|
|
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 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
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:
|
157 |
-
print(f"Error in on_summarize: {str(e)}")
|
158 |
-
print(traceback.format_exc()) # 상세한 오류 정보 출력
|
159 |
-
return f"요약 중 오류가 발생했습니다: {str(e)}"
|
160 |
|
161 |
-
|
162 |
-
summarize_btn.click(on_summarize, inputs=[selected_space], outputs=[output])
|
163 |
|
164 |
return demo
|
165 |
|
|
|
4 |
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 |
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
|
|
|
66 |
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
|
67 |
return list(executor.map(format_space, spaces))
|
68 |
|
69 |
+
def get_app_py_content(space_id: str) -> str:
|
70 |
+
app_py_url = f"https://huggingface.co/spaces/{space_id}/raw/main/app.py"
|
71 |
+
try:
|
72 |
+
response = requests.get(app_py_url, headers=get_headers())
|
73 |
+
if response.status_code == 200:
|
74 |
+
return response.text
|
75 |
+
else:
|
76 |
+
return f"app.py file not found or inaccessible for space: {space_id}"
|
77 |
+
except requests.RequestException:
|
78 |
+
return f"Error fetching app.py content for space: {space_id}"
|
79 |
+
|
80 |
def summarize_space(space: Dict) -> str:
|
81 |
system_message = "당신은 Hugging Face Space의 내용을 요약하는 AI 조수입니다. 주어진 정보를 바탕으로 간결하고 명확한 요약을 제공해주세요."
|
82 |
user_message = f"다음 Hugging Face Space를 요약해주세요: {space['name']} by {space['author']}. 좋아요 수: {space['likes']}. URL: {space['url']}"
|
|
|
94 |
formatted_spaces = format_spaces(spaces_list)
|
95 |
print(f"Total spaces loaded: {len(formatted_spaces)}") # 디버깅 출력
|
96 |
|
97 |
+
def create_table_html(spaces):
|
98 |
+
html = "<table><tr><th>Title</th><th>Author</th><th>Likes</th><th>Action</th></tr>"
|
99 |
+
for i, space in enumerate(spaces):
|
100 |
+
html += f"""
|
101 |
+
<tr>
|
102 |
+
<td>{space['name']}</td>
|
103 |
+
<td>{space['author']}</td>
|
104 |
+
<td>{space['likes']}</td>
|
105 |
+
<td><button onclick="selectSpace({i})">클릭</button></td>
|
106 |
+
</tr>
|
107 |
+
"""
|
108 |
+
html += "</table>"
|
109 |
+
return html
|
110 |
|
111 |
with gr.Blocks() as demo:
|
112 |
gr.Markdown("# Hugging Face Most Liked Spaces")
|
113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
with gr.Row():
|
115 |
+
with gr.Column(scale=1):
|
116 |
+
space_table = gr.HTML(create_table_html(formatted_spaces))
|
117 |
+
|
118 |
+
with gr.Column(scale=1):
|
119 |
+
info_output = gr.Textbox(label="Space 정보 및 요약", lines=10)
|
120 |
+
app_py_content = gr.Code(language="python", label="app.py 내용")
|
121 |
+
|
122 |
+
def on_select(space_index):
|
123 |
try:
|
124 |
+
space_index = int(space_index)
|
125 |
+
selected_space = formatted_spaces[space_index]
|
126 |
+
summary = summarize_space(selected_space)
|
127 |
+
app_content = get_app_py_content(selected_space['id'])
|
128 |
+
info = f"선택된 Space: {selected_space['name']} (ID: {selected_space['id']})\n"
|
129 |
+
info += f"Author: {selected_space['author']}\n"
|
130 |
+
info += f"Likes: {selected_space['likes']}\n"
|
131 |
+
info += f"URL: {selected_space['url']}\n\n"
|
132 |
+
info += f"요약:\n{summary}"
|
133 |
+
return info, app_content
|
134 |
except Exception as e:
|
135 |
print(f"Error in on_select: {str(e)}")
|
136 |
+
print(traceback.format_exc())
|
137 |
return f"오류가 발생했습니다: {str(e)}", ""
|
138 |
|
139 |
+
demo.load(None, None, None, _js="""
|
140 |
+
function selectSpace(index) {
|
141 |
+
document.querySelectorAll('button').forEach(btn => btn.classList.remove('selected'));
|
142 |
+
event.target.classList.add('selected');
|
143 |
+
gradio('on_select', index);
|
144 |
+
}
|
145 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
+
demo.load(on_select, None, [info_output, app_py_content], _js="(x) => selectedIndex")
|
|
|
148 |
|
149 |
return demo
|
150 |
|