Spaces:
Running
Running
Update app-backup4.py
Browse files- app-backup4.py +32 -65
app-backup4.py
CHANGED
@@ -4,7 +4,6 @@ import os
|
|
4 |
import requests
|
5 |
from typing import List, Dict, Union
|
6 |
import concurrent.futures
|
7 |
-
import base64
|
8 |
import traceback
|
9 |
|
10 |
# 환경 변수에서 토큰 가져오기
|
@@ -41,27 +40,6 @@ def get_most_liked_spaces(limit: int = 100) -> Union[List[Dict], str]:
|
|
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}/screenshot.jpg"
|
46 |
-
try:
|
47 |
-
response = requests.get(screenshot_url, headers=get_headers())
|
48 |
-
if response.status_code == 200:
|
49 |
-
return base64.b64encode(response.content).decode('utf-8')
|
50 |
-
except requests.RequestException:
|
51 |
-
pass
|
52 |
-
return ""
|
53 |
-
|
54 |
-
def get_app_py_content(space_id: str) -> str:
|
55 |
-
app_py_url = f"https://huggingface.co/spaces/{space_id}/raw/main/app.py"
|
56 |
-
try:
|
57 |
-
response = requests.get(app_py_url, headers=get_headers())
|
58 |
-
if response.status_code == 200:
|
59 |
-
return response.text
|
60 |
-
else:
|
61 |
-
return f"app.py file not found or inaccessible for space: {space_id}"
|
62 |
-
except requests.RequestException:
|
63 |
-
return f"Error fetching app.py content for space: {space_id}"
|
64 |
-
|
65 |
def format_space(space: Dict) -> Dict:
|
66 |
space_id = space.get('id', 'Unknown')
|
67 |
space_name = space_id.split('/')[-1] if '/' in space_id else space_id
|
@@ -73,15 +51,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]:
|
@@ -91,6 +66,17 @@ def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]:
|
|
91 |
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
|
92 |
return list(executor.map(format_space, spaces))
|
93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
def summarize_space(space: Dict) -> str:
|
95 |
system_message = "당신은 Hugging Face Space의 내용을 요약하는 AI 조수입니다. 주어진 정보를 바탕으로 간결하고 명확한 요약을 제공해주세요."
|
96 |
user_message = f"다음 Hugging Face Space를 요약해주세요: {space['name']} by {space['author']}. 좋아요 수: {space['likes']}. URL: {space['url']}"
|
@@ -108,58 +94,39 @@ def create_ui():
|
|
108 |
formatted_spaces = format_spaces(spaces_list)
|
109 |
print(f"Total spaces loaded: {len(formatted_spaces)}") # 디버깅 출력
|
110 |
|
111 |
-
space_choices = {f"{space['name']} by {space['author']} (Likes: {space['likes']})": space['id'] for space in formatted_spaces}
|
112 |
-
|
113 |
with gr.Blocks() as demo:
|
114 |
gr.Markdown("# Hugging Face Most Liked Spaces")
|
115 |
|
116 |
with gr.Row():
|
117 |
with gr.Column(scale=1):
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
|
|
123 |
|
124 |
-
with gr.Column(scale=
|
125 |
-
|
126 |
app_py_content = gr.Code(language="python", label="app.py 내용")
|
127 |
|
128 |
-
def on_select(
|
129 |
try:
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
print(f"Space not found for ID: {selected_id}") # 디버깅 출력
|
139 |
-
return "선택된 space를 찾을 수 없습니다.", ""
|
140 |
except Exception as e:
|
141 |
print(f"Error in on_select: {str(e)}")
|
142 |
-
print(traceback.format_exc())
|
143 |
return f"오류가 발생했습니다: {str(e)}", ""
|
144 |
|
145 |
-
|
146 |
-
|
147 |
-
if choice:
|
148 |
-
selected_id = space_choices[choice]
|
149 |
-
selected_space = next((space for space in formatted_spaces if space['id'] == selected_id), None)
|
150 |
-
if selected_space:
|
151 |
-
summary = summarize_space(selected_space)
|
152 |
-
print(f"Summarizing space: {selected_space['name']}") # 디버깅 출력
|
153 |
-
return 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 |
-
space_radio.change(on_select, space_radio, [output, app_py_content])
|
162 |
-
summarize_btn.click(on_summarize, space_radio, 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 |
# 환경 변수에서 토큰 가져오기
|
|
|
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 |
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]:
|
|
|
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 |
with gr.Blocks() as demo:
|
98 |
gr.Markdown("# Hugging Face Most Liked Spaces")
|
99 |
|
100 |
with gr.Row():
|
101 |
with gr.Column(scale=1):
|
102 |
+
space_buttons = []
|
103 |
+
for space in formatted_spaces:
|
104 |
+
with gr.Row():
|
105 |
+
gr.Markdown(f"{space['name']} by {space['author']} (Likes: {space['likes']})")
|
106 |
+
button = gr.Button("클릭", elem_id=f"btn-{space['id']}")
|
107 |
+
space_buttons.append(button)
|
108 |
|
109 |
+
with gr.Column(scale=1):
|
110 |
+
info_output = gr.Textbox(label="Space 정보 및 요약", lines=10)
|
111 |
app_py_content = gr.Code(language="python", label="app.py 내용")
|
112 |
|
113 |
+
def on_select(space):
|
114 |
try:
|
115 |
+
summary = summarize_space(space)
|
116 |
+
app_content = get_app_py_content(space['id'])
|
117 |
+
info = f"선택된 Space: {space['name']} (ID: {space['id']})\n"
|
118 |
+
info += f"Author: {space['author']}\n"
|
119 |
+
info += f"Likes: {space['likes']}\n"
|
120 |
+
info += f"URL: {space['url']}\n\n"
|
121 |
+
info += f"요약:\n{summary}"
|
122 |
+
return info, app_content
|
|
|
|
|
123 |
except Exception as e:
|
124 |
print(f"Error in on_select: {str(e)}")
|
125 |
+
print(traceback.format_exc())
|
126 |
return f"오류가 발생했습니다: {str(e)}", ""
|
127 |
|
128 |
+
for button, space in zip(space_buttons, formatted_spaces):
|
129 |
+
button.click(on_select, inputs=[gr.State(space)], outputs=[info_output, app_py_content])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
return demo
|
132 |
|