Spaces:
Running
Running
File size: 8,089 Bytes
fd7325b |
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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
import gradio as gr
import requests
from huggingface_hub import HfApi
import pandas as pd
from datetime import datetime
import json
# Hugging Face API ์ด๊ธฐํ
api = HfApi()
def search_models(
search_query="",
task_filter="all",
language_filter="all",
license_filter="all",
sort_by="downloads",
max_results=20
):
"""Hugging Face Hub์์ ๋ชจ๋ธ ๊ฒ์"""
try:
# ๊ฒ์ ํ๋ผ๋ฏธํฐ ์ค์
search_params = {
"limit": max_results,
"sort": sort_by,
"direction": -1, # ๋ด๋ฆผ์ฐจ์
}
# ํ์คํฌ ํํฐ
if task_filter != "all":
search_params["pipeline_tag"] = task_filter
# ์ธ์ด ํํฐ
if language_filter != "all":
search_params["language"] = language_filter
# ๋ผ์ด์ ์ค ํํฐ
if license_filter != "all":
search_params["license"] = license_filter
# ๊ฒ์ ์ฟผ๋ฆฌ
if search_query.strip():
search_params["search"] = search_query
# API ํธ์ถ
models = api.list_models(**search_params)
# ๊ฒฐ๊ณผ ์ฒ๋ฆฌ
results = []
for model in models:
# ๋ชจ๋ธ ์ ๋ณด ์ถ์ถ
model_info = {
"Model Name": model.modelId,
"Task": getattr(model, 'pipeline_tag', 'Unknown'),
"Downloads": f"{getattr(model, 'downloads', 0):,}",
"Likes": f"{getattr(model, 'likes', 0):,}",
"Updated": getattr(model, 'lastModified', 'Unknown'),
"License": getattr(model, 'license', 'Unknown'),
"Link": f"https://huggingface.co/{model.modelId}"
}
results.append(model_info)
if not results:
return pd.DataFrame([{"Message": "๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค. ๋ค๋ฅธ ๊ฒ์์ด๋ ํํฐ๋ฅผ ์๋ํด๋ณด์ธ์."}])
# DataFrame์ผ๋ก ๋ณํ
df = pd.DataFrame(results)
return df
except Exception as e:
error_df = pd.DataFrame([{"Error": f"๊ฒ์ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"}])
return error_df
def get_model_details(model_name):
"""ํน์ ๋ชจ๋ธ์ ์์ธ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ"""
if not model_name:
return "๋ชจ๋ธ๋ช
์ ์
๋ ฅํด์ฃผ์ธ์."
try:
# ๋ชจ๋ธ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
model_info = api.model_info(model_name)
details = f"""
# ๐ค {model_name}
## ๊ธฐ๋ณธ ์ ๋ณด
- **ํ์คํฌ**: {getattr(model_info, 'pipeline_tag', 'Unknown')}
- **๋ผ์ด์ ์ค**: {getattr(model_info, 'license', 'Unknown')}
- **๋ค์ด๋ก๋**: {getattr(model_info, 'downloads', 0):,}ํ
- **์ข์์**: {getattr(model_info, 'likes', 0):,}๊ฐ
- **์ต์ข
์
๋ฐ์ดํธ**: {getattr(model_info, 'lastModified', 'Unknown')}
## ์ง์ ์ธ์ด
{', '.join(getattr(model_info, 'language', ['Unknown']))}
## ํ๊ทธ
{', '.join(getattr(model_info, 'tags', ['None']))}
## ๋ชจ๋ธ ๋งํฌ
๐ [Hugging Face์์ ๋ณด๊ธฐ](https://huggingface.co/{model_name})
## ์ฌ์ฉ ์์
```python
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("{model_name}")
model = AutoModel.from_pretrained("{model_name}")
```
"""
return details
except Exception as e:
return f"โ ์ค๋ฅ: {str(e)}\n\n๋ชจ๋ธ๋ช
์ ์ ํํ ์
๋ ฅํ๋์ง ํ์ธํด์ฃผ์ธ์."
# Gradio ์ธํฐํ์ด์ค
with gr.Blocks(title="๐ HF Model Finder", theme=gr.themes.Soft()) as demo:
gr.Markdown("# ๐ Hugging Face Model Finder")
gr.Markdown("ํ๊น
ํ์ด์ค์ ์๋ง์ ๋ชจ๋ธ ์ค์์ ๋น์ ์ด ์ํ๋ ์กฐ๊ฑด์ ๋ง๋ ๋ชจ๋ธ์ ์ฝ๊ฒ ์ฐพ์๋ณด์ธ์!")
with gr.Tabs():
# ํญ 1: ๋ชจ๋ธ ๊ฒ์
with gr.Tab("๐ ๋ชจ๋ธ ๊ฒ์"):
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### ๊ฒ์ ์กฐ๊ฑด")
search_query = gr.Textbox(
label="๊ฒ์์ด",
placeholder="์: korean, sentiment, bert",
value=""
)
task_filter = gr.Dropdown(
label="ํ์คํฌ",
choices=[
"all", "text-classification", "token-classification",
"question-answering", "fill-mask", "summarization",
"translation", "text-generation", "conversational"
],
value="all"
)
language_filter = gr.Dropdown(
label="์ธ์ด",
choices=["all", "ko", "en", "multilingual", "zh", "ja", "es", "fr"],
value="all"
)
license_filter = gr.Dropdown(
label="๋ผ์ด์ ์ค",
choices=["all", "apache-2.0", "mit", "cc-by-4.0", "cc-by-nc-4.0"],
value="all"
)
sort_by = gr.Dropdown(
label="์ ๋ ฌ ๊ธฐ์ค",
choices=["downloads", "likes", "lastModified"],
value="downloads"
)
max_results = gr.Slider(
label="์ต๋ ๊ฒฐ๊ณผ ์",
minimum=5,
maximum=50,
value=20,
step=5
)
search_btn = gr.Button("๐ ๊ฒ์ํ๊ธฐ", variant="primary")
with gr.Column(scale=2):
gr.Markdown("### ๊ฒ์ ๊ฒฐ๊ณผ")
results_table = gr.Dataframe(
headers=["Model Name", "Task", "Downloads", "Likes", "Updated", "License"],
interactive=False,
wrap=True
)
# ํญ 2: ๋ชจ๋ธ ์์ธ ์ ๋ณด
with gr.Tab("๐ ๋ชจ๋ธ ์์ธ ์ ๋ณด"):
with gr.Row():
with gr.Column(scale=1):
model_name_input = gr.Textbox(
label="๋ชจ๋ธ๋ช
",
placeholder="์: klue/bert-base",
info="Hugging Face์ ์ ํํ ๋ชจ๋ธ๋ช
์ ์
๋ ฅํ์ธ์"
)
detail_btn = gr.Button("๐ ์์ธ ์ ๋ณด ๋ณด๊ธฐ", variant="primary")
with gr.Column(scale=2):
model_details = gr.Markdown("๋ชจ๋ธ๋ช
์ ์
๋ ฅํ๊ณ ๋ฒํผ์ ํด๋ฆญํ์ธ์.")
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
search_btn.click(
fn=search_models,
inputs=[search_query, task_filter, language_filter, license_filter, sort_by, max_results],
outputs=results_table
)
detail_btn.click(
fn=get_model_details,
inputs=model_name_input,
outputs=model_details
)
# ์ฌ์ฉ๋ฒ ์๋ด
gr.Markdown("""
### ๐ก ์ฌ์ฉ ํ
**๊ฒ์ ํญ:**
- ๊ฒ์์ด์ ํค์๋๋ฅผ ์
๋ ฅํ๊ฑฐ๋ ํํฐ๋ฅผ ์กฐํฉํด์ ์ฌ์ฉํ์ธ์
- ๋ค์ด๋ก๋ ์๊ฐ ๋ง์ ๋ชจ๋ธ์ผ์๋ก ์์ ์ ์ด๊ณ ๋ฌธ์ํ๊ฐ ์ ๋์ด ์์ด์
- ๋ผ์ด์ ์ค๋ฅผ ํ์ธํด์ ์์
์ ์ฌ์ฉ ๊ฐ๋ฅ ์ฌ๋ถ๋ฅผ ์ฒดํฌํ์ธ์
**์์ธ ์ ๋ณด ํญ:**
- ๊ฒ์ ๊ฒฐ๊ณผ์์ ๊ด์ฌ ์๋ ๋ชจ๋ธ๋ช
์ ๋ณต์ฌํด์ ๋ถ์ฌ๋ฃ์ผ์ธ์
- ์ฌ์ฉ ์์ ์ฝ๋๋ ์๋์ผ๋ก ์์ฑ๋ฉ๋๋ค
**์ถ์ฒ ๊ฒ์ ์กฐํฉ:**
- ํ๊ตญ์ด ๊ฐ์ ๋ถ์: language=ko, task=text-classification, ๊ฒ์์ด=sentiment
- ์์ด ์ง์์๋ต: language=en, task=question-answering
- ์์
์ ์ฌ์ฉ ๊ฐ๋ฅ: license=apache-2.0 ๋๋ mit
""")
if __name__ == "__main__":
demo.launch() |