Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -142,7 +142,6 @@ def take_screenshot(url):
|
|
142 |
def create_ui():
|
143 |
try:
|
144 |
spaces_list = get_most_liked_spaces()
|
145 |
-
print(f"Type of spaces_list: {type(spaces_list)}")
|
146 |
formatted_spaces = format_spaces(spaces_list)
|
147 |
print(f"Total spaces loaded: {len(formatted_spaces)}")
|
148 |
|
@@ -150,10 +149,18 @@ def create_ui():
|
|
150 |
footer {visibility: hidden;}
|
151 |
.minimal-button {min-width: 30px !important; height: 25px !important; line-height: 1 !important; font-size: 12px !important; padding: 2px 5px !important;}
|
152 |
.space-row {margin-bottom: 5px !important;}
|
153 |
-
#refresh-button {
|
154 |
width: 100% !important;
|
155 |
margin-top: 5px !important;
|
156 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
"""
|
158 |
|
159 |
with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
|
@@ -176,8 +183,10 @@ def create_ui():
|
|
176 |
|
177 |
screenshot_output = gr.Image(type="pil", label="Live ํ๋ฉด", height=360, width=600)
|
178 |
refresh_button = gr.Button("๐ ์๋ก๊ณ ์นจ", elem_id="refresh-button")
|
|
|
179 |
|
180 |
-
|
|
|
181 |
update_trigger = gr.Button("Update Screenshot", visible=False)
|
182 |
|
183 |
for _, button, space in space_rows:
|
@@ -201,6 +210,15 @@ def create_ui():
|
|
201 |
outputs=[screenshot_output, last_url_state]
|
202 |
)
|
203 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
|
205 |
update_trigger.click(
|
206 |
update_screenshot,
|
@@ -223,6 +241,136 @@ def create_ui():
|
|
223 |
print(traceback.format_exc())
|
224 |
raise
|
225 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
if __name__ == "__main__":
|
227 |
try:
|
228 |
demo = create_ui()
|
|
|
142 |
def create_ui():
|
143 |
try:
|
144 |
spaces_list = get_most_liked_spaces()
|
|
|
145 |
formatted_spaces = format_spaces(spaces_list)
|
146 |
print(f"Total spaces loaded: {len(formatted_spaces)}")
|
147 |
|
|
|
149 |
footer {visibility: hidden;}
|
150 |
.minimal-button {min-width: 30px !important; height: 25px !important; line-height: 1 !important; font-size: 12px !important; padding: 2px 5px !important;}
|
151 |
.space-row {margin-bottom: 5px !important;}
|
152 |
+
#refresh-button, #manual-button {
|
153 |
width: 100% !important;
|
154 |
margin-top: 5px !important;
|
155 |
}
|
156 |
+
#usage-guide {
|
157 |
+
height: 300px;
|
158 |
+
overflow-y: auto;
|
159 |
+
}
|
160 |
+
#app-py-content {
|
161 |
+
height: auto;
|
162 |
+
max-height: none;
|
163 |
+
}
|
164 |
"""
|
165 |
|
166 |
with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
|
|
|
183 |
|
184 |
screenshot_output = gr.Image(type="pil", label="Live ํ๋ฉด", height=360, width=600)
|
185 |
refresh_button = gr.Button("๐ ์๋ก๊ณ ์นจ", elem_id="refresh-button")
|
186 |
+
manual_button = gr.Button("์ ํ ์๋น์ค ๋ฉ๋ด์ผ", elem_id="manual-button")
|
187 |
|
188 |
+
usage_guide = gr.Textbox(label="์ฌ์ฉ ๋ฐฉ๋ฒ", elem_id="usage-guide", visible=False, lines=20)
|
189 |
+
app_py_content = gr.Code(language="python", label="๋ฉ์ธ ์์ค์ฝ๋", elem_id="app-py-content")
|
190 |
update_trigger = gr.Button("Update Screenshot", visible=False)
|
191 |
|
192 |
for _, button, space in space_rows:
|
|
|
210 |
outputs=[screenshot_output, last_url_state]
|
211 |
)
|
212 |
|
213 |
+
def show_usage_guide(app_content):
|
214 |
+
usage_text = generate_usage_guide(app_content)
|
215 |
+
return gr.update(value=usage_text, visible=True)
|
216 |
+
|
217 |
+
manual_button.click(
|
218 |
+
show_usage_guide,
|
219 |
+
inputs=[app_py_content],
|
220 |
+
outputs=[usage_guide]
|
221 |
+
)
|
222 |
|
223 |
update_trigger.click(
|
224 |
update_screenshot,
|
|
|
241 |
print(traceback.format_exc())
|
242 |
raise
|
243 |
|
244 |
+
def generate_usage_guide(app_content):
|
245 |
+
system_message = "๋น์ ์ Python ์ฝ๋๋ฅผ ๋ถ์ํ๊ณ ๊ทธ ์ฌ์ฉ๋ฒ์ ์ค๋ช
ํ๋ AI ์กฐ์์
๋๋ค. ์ฃผ์ด์ง ์ฝ๋๋ฅผ ๋ฐํ์ผ๋ก ์์ธํ ์ฌ์ฉ ๋ฐฉ๋ฒ์ ์ ๊ณตํด์ฃผ์ธ์."
|
246 |
+
user_message = f"๋ค์ Python ์ฝ๋์ ์ฌ์ฉ ๋ฐฉ๋ฒ์ ์ค๋ช
ํด์ฃผ์ธ์:\n\n{app_content}"
|
247 |
+
|
248 |
+
messages = [
|
249 |
+
{"role": "system", "content": system_message},
|
250 |
+
{"role": "user", "content": user_message}
|
251 |
+
]
|
252 |
+
|
253 |
+
try:
|
254 |
+
response = hf_client.chat_completion(messages, max_tokens=4000, temperature=0.7)
|
255 |
+
return response.choices[0].message.content
|
256 |
+
except Exception as e:
|
257 |
+
return f"์ฌ์ฉ ๋ฐฉ๋ฒ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"def create_ui():
|
258 |
+
try:
|
259 |
+
spaces_list = get_most_liked_spaces()
|
260 |
+
formatted_spaces = format_spaces(spaces_list)
|
261 |
+
print(f"Total spaces loaded: {len(formatted_spaces)}")
|
262 |
+
|
263 |
+
css = """
|
264 |
+
footer {visibility: hidden;}
|
265 |
+
.minimal-button {min-width: 30px !important; height: 25px !important; line-height: 1 !important; font-size: 12px !important; padding: 2px 5px !important;}
|
266 |
+
.space-row {margin-bottom: 5px !important;}
|
267 |
+
#refresh-button, #manual-button {
|
268 |
+
width: 100% !important;
|
269 |
+
margin-top: 5px !important;
|
270 |
+
}
|
271 |
+
#usage-guide {
|
272 |
+
height: 300px;
|
273 |
+
overflow-y: auto;
|
274 |
+
}
|
275 |
+
#app-py-content {
|
276 |
+
height: auto;
|
277 |
+
max-height: none;
|
278 |
+
}
|
279 |
+
"""
|
280 |
+
|
281 |
+
with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
|
282 |
+
gr.Markdown("# 300: HuggingFace Most Liked Spaces")
|
283 |
+
|
284 |
+
with gr.Row():
|
285 |
+
with gr.Column(scale=1):
|
286 |
+
space_rows = []
|
287 |
+
for space in formatted_spaces:
|
288 |
+
with gr.Row(elem_classes="space-row") as space_row:
|
289 |
+
with gr.Column():
|
290 |
+
gr.Markdown(f"{space['name']} by {space['author']} (Likes: {space['likes']})", elem_classes="space-info")
|
291 |
+
button = gr.Button("ํด๋ฆญ", elem_classes="minimal-button")
|
292 |
+
space_rows.append((space_row, button, space))
|
293 |
+
|
294 |
+
with gr.Column(scale=1):
|
295 |
+
info_output = gr.Textbox(label="Space ์ ๋ณด ๋ฐ ์์ฝ", lines=20)
|
296 |
+
url_state = gr.State("")
|
297 |
+
last_url_state = gr.State("")
|
298 |
+
|
299 |
+
screenshot_output = gr.Image(type="pil", label="Live ํ๋ฉด", height=360, width=600)
|
300 |
+
refresh_button = gr.Button("๐ ์๋ก๊ณ ์นจ", elem_id="refresh-button")
|
301 |
+
manual_button = gr.Button("์ ํ ์๋น์ค ๋ฉ๋ด์ผ", elem_id="manual-button")
|
302 |
+
|
303 |
+
usage_guide = gr.Textbox(label="์ฌ์ฉ ๋ฐฉ๋ฒ", elem_id="usage-guide", visible=False, lines=20)
|
304 |
+
app_py_content = gr.Code(language="python", label="๋ฉ์ธ ์์ค์ฝ๋", elem_id="app-py-content")
|
305 |
+
update_trigger = gr.Button("Update Screenshot", visible=False)
|
306 |
+
|
307 |
+
for _, button, space in space_rows:
|
308 |
+
button.click(
|
309 |
+
lambda s=space: on_select(s),
|
310 |
+
inputs=[],
|
311 |
+
outputs=[info_output, app_py_content, url_state]
|
312 |
+
).then(
|
313 |
+
update_screenshot,
|
314 |
+
inputs=[url_state, last_url_state],
|
315 |
+
outputs=[screenshot_output, last_url_state]
|
316 |
+
)
|
317 |
+
|
318 |
+
def refresh_screenshot(url, last_url):
|
319 |
+
print(f"Refresh button clicked. URL: {url}, Last URL: {last_url}")
|
320 |
+
return update_screenshot(url, last_url)
|
321 |
+
|
322 |
+
refresh_button.click(
|
323 |
+
refresh_screenshot,
|
324 |
+
inputs=[url_state, last_url_state],
|
325 |
+
outputs=[screenshot_output, last_url_state]
|
326 |
+
)
|
327 |
+
|
328 |
+
def show_usage_guide(app_content):
|
329 |
+
usage_text = generate_usage_guide(app_content)
|
330 |
+
return gr.update(value=usage_text, visible=True)
|
331 |
+
|
332 |
+
manual_button.click(
|
333 |
+
show_usage_guide,
|
334 |
+
inputs=[app_py_content],
|
335 |
+
outputs=[usage_guide]
|
336 |
+
)
|
337 |
+
|
338 |
+
update_trigger.click(
|
339 |
+
update_screenshot,
|
340 |
+
inputs=[url_state, last_url_state],
|
341 |
+
outputs=[screenshot_output, last_url_state]
|
342 |
+
)
|
343 |
+
|
344 |
+
# Start a background thread to trigger updates
|
345 |
+
def trigger_updates():
|
346 |
+
while True:
|
347 |
+
time.sleep(5)
|
348 |
+
update_trigger.click()
|
349 |
+
|
350 |
+
threading.Thread(target=trigger_updates, daemon=True).start()
|
351 |
+
|
352 |
+
return demo
|
353 |
+
|
354 |
+
except Exception as e:
|
355 |
+
print(f"Error in create_ui: {str(e)}")
|
356 |
+
print(traceback.format_exc())
|
357 |
+
raise
|
358 |
+
|
359 |
+
def generate_usage_guide(app_content):
|
360 |
+
system_message = "๋น์ ์ Python ์ฝ๋๋ฅผ ๋ถ์ํ๊ณ ๊ทธ ์ฌ์ฉ๋ฒ์ ์ค๋ช
ํ๋ AI ์กฐ์์
๋๋ค. ์ฃผ์ด์ง ์ฝ๋๋ฅผ ๋ฐํ์ผ๋ก ์์ธํ ์ฌ์ฉ ๋ฐฉ๋ฒ์ ์ ๊ณตํด์ฃผ์ธ์."
|
361 |
+
user_message = f"๋ค์ Python ์ฝ๋์ ์ฌ์ฉ ๋ฐฉ๋ฒ์ ์ค๋ช
ํด์ฃผ์ธ์:\n\n{app_content}"
|
362 |
+
|
363 |
+
messages = [
|
364 |
+
{"role": "system", "content": system_message},
|
365 |
+
{"role": "user", "content": user_message}
|
366 |
+
]
|
367 |
+
|
368 |
+
try:
|
369 |
+
response = hf_client.chat_completion(messages, max_tokens=4000, temperature=0.7)
|
370 |
+
return response.choices[0].message.content
|
371 |
+
except Exception as e:
|
372 |
+
return f"์ฌ์ฉ ๋ฐฉ๋ฒ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
373 |
+
|
374 |
if __name__ == "__main__":
|
375 |
try:
|
376 |
demo = create_ui()
|