Spaces:
Running
Running
File size: 8,277 Bytes
7cb753c 9c9264e 7cb753c 9c9264e 7cb753c 9c9264e 7cb753c 9c9264e 7cb753c 9c9264e 7cb753c 9c9264e 7cb753c 9c9264e 7cb753c 9c9264e 7cb753c 9c9264e 7cb753c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
import gradio as gr
from huggingface_hub import InferenceClient
import os
import requests
from typing import List, Dict, Union
import concurrent.futures
import base64
import traceback
# ํ๊ฒฝ ๋ณ์์์ ํ ํฐ ๊ฐ์ ธ์ค๊ธฐ
HF_TOKEN = os.getenv("HF_TOKEN")
# ์ถ๋ก API ํด๋ผ์ด์ธํธ ์ค์
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 = 100) -> 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()
data = response.json()
if isinstance(data, list):
return data
else:
return f"Unexpected API response format: {type(data)}"
except requests.RequestException as e:
return f"API request error: {str(e)}"
except ValueError as e:
return f"JSON decoding error: {str(e)}"
def capture_thumbnail(space_id: str) -> str:
screenshot_url = f"https://huggingface.co/spaces/{space_id}/screenshot.jpg"
try:
response = requests.get(screenshot_url, headers=get_headers())
if response.status_code == 200:
return base64.b64encode(response.content).decode('utf-8')
except requests.RequestException:
pass
return ""
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:
return response.text
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 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}"
thumbnail = capture_thumbnail(space_id)
return {
"id": space_id,
"name": space_name,
"author": space_author,
"likes": space_likes,
"url": space_url,
"thumbnail": thumbnail
}
def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]:
if isinstance(spaces, str):
return [{"error": spaces}]
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
return list(executor.map(format_space, spaces))
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}
]
response = hf_client.chat_completion(messages, max_tokens=150, temperature=0.7)
return response.choices[0].message.content
def create_ui():
spaces_list = get_most_liked_spaces()
formatted_spaces = format_spaces(spaces_list)
print(f"Total spaces loaded: {len(formatted_spaces)}") # ๋๋ฒ๊น
์ถ๋ ฅ
space_choices = {f"{space['name']} by {space['author']} (Likes: {space['likes']})": space['id'] for space in formatted_spaces}
with gr.Blocks(css="#space-list { max-height: 400px; overflow-y: auto; }") as demo:
gr.Markdown("# Hugging Face Most Liked Spaces")
with gr.Row():
with gr.Column(scale=1):
with gr.Column(elem_id="space-list"):
space_radios = []
for space in formatted_spaces:
with gr.Row():
radio = gr.Radio(
choices=[f"{space['name']} by {space['author']} (Likes: {space['likes']})"],
label="",
value=None,
elem_id=f"radio-{space['id']}"
)
space_radios.append(radio)
gr.Button("๐", elem_id=f"link-{space['id']}")
summarize_btn = gr.Button("์์ฝ")
with gr.Column(scale=2):
output = gr.Textbox(label="Space ์ ๋ณด", lines=10)
app_py_content = gr.Code(language="python", label="app.py ๋ด์ฉ")
def on_select(choice):
try:
print(f"Selected: {choice}") # ๋๋ฒ๊น
์ถ๋ ฅ
selected_id = space_choices[choice[0]] # choice is now a list with one item
selected_space = next((space for space in formatted_spaces if space['id'] == selected_id), None)
if selected_space:
app_content = get_app_py_content(selected_id)
print(f"Selected space: {selected_space['name']} (ID: {selected_id})") # ๋๋ฒ๊น
์ถ๋ ฅ
return f"์ ํ๋ Space: {selected_space['name']} (ID: {selected_id})\nURL: {selected_space['url']}", app_content
else:
print(f"Space not found for ID: {selected_id}") # ๋๋ฒ๊น
์ถ๋ ฅ
return "์ ํ๋ space๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.", ""
except Exception as e:
print(f"Error in on_select: {str(e)}")
print(traceback.format_exc()) # ์์ธํ ์ค๋ฅ ์ ๋ณด ์ถ๋ ฅ
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}", ""
def on_summarize(choice):
try:
if choice:
selected_id = space_choices[choice[0]] # choice is now a list with one item
selected_space = next((space for space in formatted_spaces if space['id'] == selected_id), None)
if selected_space:
summary = summarize_space(selected_space)
print(f"Summarizing space: {selected_space['name']}") # ๋๋ฒ๊น
์ถ๋ ฅ
return summary
print("No space selected for summarization") # ๋๋ฒ๊น
์ถ๋ ฅ
return "์ ํ๋ space๊ฐ ์์ต๋๋ค. ๋จผ์ ๋ฆฌ์คํธ์์ space๋ฅผ ์ ํํด์ฃผ์ธ์."
except Exception as e:
print(f"Error in on_summarize: {str(e)}")
print(traceback.format_exc()) # ์์ธํ ์ค๋ฅ ์ ๋ณด ์ถ๋ ฅ
return f"์์ฝ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
for radio in space_radios:
radio.change(on_select, radio, [output, app_py_content])
summarize_btn.click(on_summarize, inputs=[gr.State(lambda: next((radio for radio in space_radios if radio.value), None))], outputs=[output])
# JavaScript to open links in new tabs
link_script = """
function setupLinkButtons() {
document.querySelectorAll('button[id^="link-"]').forEach(button => {
button.addEventListener('click', function() {
var spaceId = this.id.split('-')[1];
window.open('https://huggingface.co/spaces/' + spaceId, '_blank');
});
});
}
if (document.readyState === 'complete') {
setupLinkButtons();
} else {
document.addEventListener('DOMContentLoaded', setupLinkButtons);
}
"""
demo.load(None, None, None, _js=link_script)
return demo
if __name__ == "__main__":
demo = create_ui()
demo.launch() |