Spaces:
Running
Running
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| import os | |
| import requests | |
| from typing import List, Dict, Union | |
| import traceback | |
| from PIL import Image | |
| from io import BytesIO | |
| import asyncio | |
| from gradio_client import Client | |
| import time | |
| import threading | |
| HF_TOKEN = os.getenv("HF_TOKEN") | |
| hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=HF_TOKEN) | |
| def get_headers(): | |
| if not HF_TOKEN: | |
| raise ValueError("Hugging Face token not found in environment variables") | |
| return {"Authorization": f"Bearer {HF_TOKEN}"} | |
| def get_most_liked_spaces(limit: int = 300) -> Union[List[Dict], str]: | |
| url = "https://huggingface.co/api/spaces" | |
| params = { | |
| "sort": "likes", | |
| "direction": -1, | |
| "limit": limit, | |
| "full": "true" | |
| } | |
| try: | |
| response = requests.get(url, params=params, headers=get_headers()) | |
| response.raise_for_status() | |
| return response.json() | |
| except requests.RequestException as e: | |
| return f"API request error: {str(e)}" | |
| except ValueError as e: | |
| return f"JSON decoding error: {str(e)}" | |
| def format_space(space: Dict) -> Dict: | |
| space_id = space.get('id', 'Unknown') | |
| space_name = space_id.split('/')[-1] if '/' in space_id else space_id | |
| space_author = space.get('author', 'Unknown') | |
| if isinstance(space_author, dict): | |
| space_author = space_author.get('user', space_author.get('name', 'Unknown')) | |
| space_likes = space.get('likes', 'N/A') | |
| space_url = f"https://huggingface.co/spaces/{space_id}" | |
| return { | |
| "id": space_id, | |
| "name": space_name, | |
| "author": space_author, | |
| "likes": space_likes, | |
| "url": space_url, | |
| } | |
| def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]: | |
| if isinstance(spaces, str): | |
| return [{"error": spaces}] | |
| return [format_space(space) for space in spaces if isinstance(space, dict)] | |
| def summarize_space(space: Dict) -> str: | |
| system_message = "๋น์ ์ Hugging Face Space์ ๋ด์ฉ์ ์์ฝํ๋ AI ์กฐ์์ ๋๋ค. ์ฃผ์ด์ง ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ๊ฐ๊ฒฐํ๊ณ ๋ช ํํ ์์ฝ์ ์ ๊ณตํด์ฃผ์ธ์." | |
| user_message = f"๋ค์ Hugging Face Space๋ฅผ ์์ฝํด์ฃผ์ธ์: {space['name']} by {space['author']}. ์ข์์ ์: {space['likes']}. URL: {space['url']}" | |
| messages = [ | |
| {"role": "system", "content": system_message}, | |
| {"role": "user", "content": user_message} | |
| ] | |
| try: | |
| response = hf_client.chat_completion(messages, max_tokens=400, temperature=0.7) | |
| return response.choices[0].message.content | |
| except Exception as e: | |
| return f"์์ฝ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}" | |
| def get_app_py_content(space_id: str) -> str: | |
| app_py_url = f"https://huggingface.co/spaces/{space_id}/raw/main/app.py" | |
| try: | |
| response = requests.get(app_py_url, headers=get_headers()) | |
| if response.status_code == 200: | |
| content = response.text | |
| if len(content) > 500: # ๋ด์ฉ์ 500์๋ก ์ ํํฉ๋๋ค | |
| content = content[:497] + "..." | |
| return content | |
| else: | |
| return f"app.py file not found or inaccessible for space: {space_id}" | |
| except requests.RequestException: | |
| return f"Error fetching app.py content for space: {space_id}" | |
| def on_select(space): | |
| try: | |
| print(f"Selected space: {space['name']}") | |
| summary = summarize_space(space) | |
| app_content = get_app_py_content(space['id']) | |
| info = f"์ ํ๋ Space: {space['name']} (ID: {space['id']})\n" | |
| info += f"Author: {space['author']}\n" | |
| info += f"Likes: {space['likes']}\n" | |
| info += f"URL: {space['url']}\n\n" | |
| info += f"์์ฝ:\n{summary}" | |
| print(f"Returning URL: {space['url']}") | |
| return info, app_content, space['url'] | |
| except Exception as e: | |
| print(f"Error in on_select: {str(e)}") | |
| print(traceback.format_exc()) | |
| return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}", "", "" | |
| def update_screenshot(url, last_url, force_update=False): | |
| print(f"Updating screenshot. Current URL: {url}, Last URL: {last_url}, Force update: {force_update}") | |
| if url and (url != last_url or force_update): | |
| screenshot = take_screenshot(url) | |
| print("Screenshot updated") | |
| return screenshot, url | |
| print("No update needed") | |
| return gr.update(), last_url | |
| def refresh_screenshot(url, last_url): | |
| print(f"Refresh button clicked. URL: {url}, Last URL: {last_url}") | |
| # ํญ์ ๊ฐ์ ๋ก ์ ๋ฐ์ดํธ | |
| return update_screenshot(url, last_url, force_update=True) | |
| def take_screenshot(url): | |
| try: | |
| print(f"Taking screenshot of URL: {url}") | |
| client = Client("ginipick/selenium-screenshot-gradio") | |
| result = client.predict(url=url, api_name="/predict") | |
| print(f"Screenshot result: {result}") | |
| if isinstance(result, str) and os.path.exists(result): | |
| return Image.open(result) | |
| else: | |
| print(f"Invalid result from API: {result}") | |
| return Image.new('RGB', (600, 360), color='lightgray') | |
| except Exception as e: | |
| print(f"Screenshot error: {str(e)}") | |
| return Image.new('RGB', (600, 360), color='lightgray') | |
| def generate_usage_guide(app_content): | |
| system_message = "๋น์ ์ Python ์ฝ๋๋ฅผ ๋ถ์ํ์ฌ, ํ๋ฉด ๋ณด๋ฏ์ด ์ด์ฉ ๋ฐฉ๋ฒ์ ์ค๋ช ํ๋ AI ์กฐ์์ ๋๋ค. app.py ์ฝ๋๋ฅผ ๋ฐํ์ผ๋ก ์ฝ๋์ ๋ํ ์ธ๊ธ์ ์ ์ธํ๊ณ , ์ด์ฉ์ ๊ด์ ์์ 1) ๊ธฐ์กด ์ ์ฌ ๊ธฐ์ ๋ฐฉ์๊ดด ๋น๊ตํด ํน์ง, ์ฅ์ ์ ๋ํด ์น์ ํ๊ณ ์์ธํ๊ฒ ์์ธํ ์ฌ์ฉ ๋ฐฉ๋ฒ์ ์ ๊ณตํด์ฃผ์ธ์." | |
| user_message = f"๋ค์ Python ์ฝ๋๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ๋ฉด UI/UX์ ์ธก๋ฉด์ผ๋ก ํน์ง๊ณผ ์ฌ์ฉ ๋ฐฉ๋ฒ์ ์ค๋ช ํด์ฃผ์ธ์:\n\n{app_content}" | |
| messages = [ | |
| {"role": "system", "content": system_message}, | |
| {"role": "user", "content": user_message} | |
| ] | |
| try: | |
| response = hf_client.chat_completion(messages, max_tokens=4000, temperature=0.7) | |
| return response.choices[0].message.content | |
| except Exception as e: | |
| return f"์ฌ์ฉ ๋ฐฉ๋ฒ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}" | |
| def create_ui(): | |
| try: | |
| spaces_list = get_most_liked_spaces() | |
| formatted_spaces = format_spaces(spaces_list) | |
| print(f"Total spaces loaded: {len(formatted_spaces)}") | |
| css = """ | |
| footer {visibility: hidden;} | |
| .minimal-button {min-width: 30px !important; height: 25px !important; line-height: 1 !important; font-size: 12px !important; padding: 2px 5px !important;} | |
| .space-row {margin-bottom: 5px !important;} | |
| #refresh-button, #manual-button { | |
| width: 100% !important; | |
| margin-top: 5px !important; | |
| } | |
| #info-output, #usage-guide { | |
| height: 400px; /* ๋์ด๋ฅผ 400px๋ก ์ฆ๊ฐ */ | |
| overflow-y: auto; | |
| padding-right: 10px; | |
| } | |
| #app-py-content { | |
| height: auto !important; | |
| max-height: none !important; | |
| overflow-y: visible !important; | |
| } | |
| .output-group { | |
| border: 1px solid #ddd; | |
| border-radius: 5px; | |
| padding: 10px; | |
| margin-bottom: 20px; | |
| } | |
| /* ์คํฌ๋กค๋ฐ ์คํ์ผ ์์ */ | |
| ::-webkit-scrollbar { | |
| width: 10px; | |
| } | |
| ::-webkit-scrollbar-track { | |
| background: #f1f1f1; | |
| } | |
| ::-webkit-scrollbar-thumb { | |
| background: #888; | |
| border-radius: 5px; | |
| } | |
| ::-webkit-scrollbar-thumb:hover { | |
| background: #555; | |
| } | |
| """ | |
| with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo: | |
| gr.Markdown("# 300: HuggingFace Most Liked Spaces") | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| space_rows = [] | |
| for space in formatted_spaces: | |
| with gr.Row(elem_classes="space-row") as space_row: | |
| with gr.Column(): | |
| gr.Markdown(f"{space['name']} by {space['author']} (Likes: {space['likes']})", elem_classes="space-info") | |
| button = gr.Button("ํด๋ฆญ", elem_classes="minimal-button") | |
| space_rows.append((space_row, button, space)) | |
| with gr.Column(scale=1): | |
| with gr.Group(elem_classes="output-group"): | |
| info_output = gr.Textbox(label="Space ์ ๋ณด ๋ฐ ์์ฝ", elem_id="info-output", lines=20, max_lines=30) | |
| url_state = gr.State("") | |
| last_url_state = gr.State("") | |
| screenshot_output = gr.Image(type="pil", label="Live ํ๋ฉด", height=360, width=600) | |
| refresh_button = gr.Button("๐ ์๋น์ค ํ๋ฉด", elem_id="refresh-button") | |
| manual_button = gr.Button("์ ํ ์๋น์ค ํน์ง ๋ฐ ์ฌ์ฉ๋ฒ", elem_id="manual-button") | |
| with gr.Group(elem_classes="output-group"): | |
| usage_guide = gr.Textbox(label="์ ํ ์๋น์ค ํน์ง ๋ฐ ์ฌ์ฉ๋ฒ", elem_id="usage-guide", visible=False, lines=20, max_lines=30) | |
| with gr.Group(elem_classes="output-group"): | |
| app_py_content = gr.Code(language="python", label="๋ฉ์ธ ์์ค์ฝ๋", elem_id="app-py-content", lines=None, max_lines=None) | |
| update_trigger = gr.Button("Update Screenshot", visible=False) | |
| for _, button, space in space_rows: | |
| button.click( | |
| lambda s=space: on_select(s), | |
| inputs=[], | |
| outputs=[info_output, app_py_content, url_state] | |
| ).then( | |
| update_screenshot, | |
| inputs=[url_state, last_url_state], | |
| outputs=[screenshot_output, last_url_state] | |
| ) | |
| def refresh_screenshot(url, last_url): | |
| print(f"Refresh button clicked. URL: {url}, Last URL: {last_url}") | |
| return update_screenshot(url, last_url) | |
| refresh_button.click( | |
| refresh_screenshot, | |
| inputs=[url_state, last_url_state], | |
| outputs=[screenshot_output, last_url_state] | |
| ) | |
| def show_usage_guide(app_content): | |
| usage_text = generate_usage_guide(app_content) | |
| return gr.update(value=usage_text, visible=True) | |
| manual_button.click( | |
| show_usage_guide, | |
| inputs=[app_py_content], | |
| outputs=[usage_guide] | |
| ) | |
| update_trigger.click( | |
| update_screenshot, | |
| inputs=[url_state, last_url_state], | |
| outputs=[screenshot_output, last_url_state] | |
| ) | |
| # Start a background thread to trigger updates | |
| def trigger_updates(): | |
| while True: | |
| time.sleep(5) | |
| update_trigger.click() | |
| threading.Thread(target=trigger_updates, daemon=True).start() | |
| return demo | |
| except Exception as e: | |
| print(f"Error in create_ui: {str(e)}") | |
| print(traceback.format_exc()) | |
| raise | |
| if __name__ == "__main__": | |
| try: | |
| demo = create_ui() | |
| demo.launch() | |
| except Exception as e: | |
| print(f"Error in main: {str(e)}") | |
| print(traceback.format_exc()) |