Update app-backup4.py
Browse files- app-backup4.py +55 -50
app-backup4.py
CHANGED
|
@@ -3,13 +3,9 @@ from huggingface_hub import InferenceClient
|
|
| 3 |
import os
|
| 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")
|
| 11 |
-
|
| 12 |
-
# 추론 API 클라이언트 설정
|
| 13 |
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=HF_TOKEN)
|
| 14 |
|
| 15 |
def get_headers():
|
|
@@ -17,7 +13,7 @@ def get_headers():
|
|
| 17 |
raise ValueError("Hugging Face token not found in environment variables")
|
| 18 |
return {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 19 |
|
| 20 |
-
def get_most_liked_spaces(limit: int =
|
| 21 |
url = "https://huggingface.co/api/spaces"
|
| 22 |
params = {
|
| 23 |
"sort": "likes",
|
|
@@ -29,12 +25,7 @@ def get_most_liked_spaces(limit: int = 100) -> Union[List[Dict], str]:
|
|
| 29 |
try:
|
| 30 |
response = requests.get(url, params=params, headers=get_headers())
|
| 31 |
response.raise_for_status()
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
if isinstance(data, list):
|
| 35 |
-
return data
|
| 36 |
-
else:
|
| 37 |
-
return f"Unexpected API response format: {type(data)}"
|
| 38 |
except requests.RequestException as e:
|
| 39 |
return f"API request error: {str(e)}"
|
| 40 |
except ValueError as e:
|
|
@@ -56,15 +47,29 @@ def format_space(space: Dict) -> Dict:
|
|
| 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]:
|
| 63 |
if isinstance(spaces, str):
|
| 64 |
return [{"error": spaces}]
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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"
|
|
@@ -77,56 +82,56 @@ def get_app_py_content(space_id: str) -> str:
|
|
| 77 |
except requests.RequestException:
|
| 78 |
return f"Error fetching app.py content for space: {space_id}"
|
| 79 |
|
| 80 |
-
def
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
def create_ui():
|
| 93 |
spaces_list = get_most_liked_spaces()
|
|
|
|
| 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 |
-
|
| 103 |
for space in formatted_spaces:
|
| 104 |
-
with gr.Row():
|
| 105 |
-
gr.
|
| 106 |
-
|
| 107 |
-
|
|
|
|
| 108 |
|
| 109 |
with gr.Column(scale=1):
|
| 110 |
-
info_output = gr.Textbox(label="Space 정보 및 요약", lines=
|
| 111 |
app_py_content = gr.Code(language="python", label="app.py 내용")
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 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 |
|
|
|
|
| 3 |
import os
|
| 4 |
import requests
|
| 5 |
from typing import List, Dict, Union
|
|
|
|
| 6 |
import traceback
|
| 7 |
|
|
|
|
| 8 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
|
|
|
| 9 |
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=HF_TOKEN)
|
| 10 |
|
| 11 |
def get_headers():
|
|
|
|
| 13 |
raise ValueError("Hugging Face token not found in environment variables")
|
| 14 |
return {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 15 |
|
| 16 |
+
def get_most_liked_spaces(limit: int = 300) -> Union[List[Dict], str]:
|
| 17 |
url = "https://huggingface.co/api/spaces"
|
| 18 |
params = {
|
| 19 |
"sort": "likes",
|
|
|
|
| 25 |
try:
|
| 26 |
response = requests.get(url, params=params, headers=get_headers())
|
| 27 |
response.raise_for_status()
|
| 28 |
+
return response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
except requests.RequestException as e:
|
| 30 |
return f"API request error: {str(e)}"
|
| 31 |
except ValueError as e:
|
|
|
|
| 47 |
"name": space_name,
|
| 48 |
"author": space_author,
|
| 49 |
"likes": space_likes,
|
| 50 |
+
"url": space_url,
|
| 51 |
}
|
| 52 |
|
| 53 |
def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]:
|
| 54 |
if isinstance(spaces, str):
|
| 55 |
return [{"error": spaces}]
|
| 56 |
|
| 57 |
+
return [format_space(space) for space in spaces if isinstance(space, dict)]
|
| 58 |
+
|
| 59 |
+
def summarize_space(space: Dict) -> str:
|
| 60 |
+
system_message = "당신은 Hugging Face Space의 내용을 요약하는 AI 조수입니다. 주어진 정보를 바탕으로 간결하고 명확한 요약을 제공해주세요."
|
| 61 |
+
user_message = f"다음 Hugging Face Space를 요약해주세요: {space['name']} by {space['author']}. 좋아요 수: {space['likes']}. URL: {space['url']}"
|
| 62 |
+
|
| 63 |
+
messages = [
|
| 64 |
+
{"role": "system", "content": system_message},
|
| 65 |
+
{"role": "user", "content": user_message}
|
| 66 |
+
]
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
response = hf_client.chat_completion(messages, max_tokens=400, temperature=0.7)
|
| 70 |
+
return response.choices[0].message.content
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return f"요약 생성 중 오류 발생: {str(e)}"
|
| 73 |
|
| 74 |
def get_app_py_content(space_id: str) -> str:
|
| 75 |
app_py_url = f"https://huggingface.co/spaces/{space_id}/raw/main/app.py"
|
|
|
|
| 82 |
except requests.RequestException:
|
| 83 |
return f"Error fetching app.py content for space: {space_id}"
|
| 84 |
|
| 85 |
+
def on_select(space):
|
| 86 |
+
try:
|
| 87 |
+
summary = summarize_space(space)
|
| 88 |
+
app_content = get_app_py_content(space['id'])
|
| 89 |
+
info = f"선택된 Space: {space['name']} (ID: {space['id']})\n"
|
| 90 |
+
info += f"Author: {space['author']}\n"
|
| 91 |
+
info += f"Likes: {space['likes']}\n"
|
| 92 |
+
info += f"URL: {space['url']}\n\n"
|
| 93 |
+
info += f"요약:\n{summary}"
|
| 94 |
+
return info, app_content
|
| 95 |
+
except Exception as e:
|
| 96 |
+
print(f"Error in on_select: {str(e)}")
|
| 97 |
+
print(traceback.format_exc())
|
| 98 |
+
return f"오류가 발생했습니다: {str(e)}", ""
|
| 99 |
|
| 100 |
def create_ui():
|
| 101 |
spaces_list = get_most_liked_spaces()
|
| 102 |
+
print(f"Type of spaces_list: {type(spaces_list)}")
|
| 103 |
formatted_spaces = format_spaces(spaces_list)
|
| 104 |
+
print(f"Total spaces loaded: {len(formatted_spaces)}")
|
| 105 |
+
|
| 106 |
+
css = """
|
| 107 |
+
footer {visibility: hidden;}
|
| 108 |
+
.minimal-button {min-width: 30px !important; height: 25px !important; line-height: 1 !important; font-size: 12px !important; padding: 2px 5px !important;}
|
| 109 |
+
.space-row {margin-bottom: 5px !important;}
|
| 110 |
+
"""
|
| 111 |
|
| 112 |
+
with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
|
| 113 |
gr.Markdown("# Hugging Face Most Liked Spaces")
|
| 114 |
|
| 115 |
with gr.Row():
|
| 116 |
with gr.Column(scale=1):
|
| 117 |
+
space_rows = []
|
| 118 |
for space in formatted_spaces:
|
| 119 |
+
with gr.Row(elem_classes="space-row") as space_row:
|
| 120 |
+
with gr.Column():
|
| 121 |
+
gr.Markdown(f"{space['name']} by {space['author']} (Likes: {space['likes']})", elem_classes="space-info")
|
| 122 |
+
button = gr.Button("클릭", elem_classes="minimal-button")
|
| 123 |
+
space_rows.append((space_row, button, space))
|
| 124 |
|
| 125 |
with gr.Column(scale=1):
|
| 126 |
+
info_output = gr.Textbox(label="Space 정보 및 요약", lines=16)
|
| 127 |
app_py_content = gr.Code(language="python", label="app.py 내용")
|
| 128 |
|
| 129 |
+
for _, button, space in space_rows:
|
| 130 |
+
button.click(
|
| 131 |
+
lambda s=space: on_select(s),
|
| 132 |
+
inputs=[],
|
| 133 |
+
outputs=[info_output, app_py_content]
|
| 134 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
return demo
|
| 137 |
|