Spaces:
Running
Running
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() |