Spaces:
Sleeping
Sleeping
Download app.py from yeansu/imageclass-app: direct link, hf CLI and curl.
- Browser
- Download file 1.78 kB
-
https://huggingface.co/spaces/yeansu/imageclass-app/resolve/main/app.py
- Command line
-
hf download hf://spaces/yeansu/imageclass-app/app.py
-
curl -L -o app.py https://huggingface.co/spaces/yeansu/imageclass-app/resolve/main/app.py
1.78 kB
| import gradio as gr | |
| from transformers import pipeline | |
| import spaces # νκΉ νμ΄μ€μ ZeroGPU νκ²½μ μ¬μ©νκΈ° μν νμ λͺ¨λμ λλ€. | |
| # 1. μ΄λ―Έμ§ λΆλ₯ λͺ¨λΈ λ‘λ | |
| image_classifier = pipeline("image-classification", model="google/vit-base-patch16-224") | |
| # [ν΅μ¬] ZeroGPU νκ²½μμ μ΄ ν¨μκ° μ€νλ λ GPU μμμ ν λΉλ°λλ‘ μ§μν©λλ€. | |
| def analyze_image(image): | |
| """μ΄λ―Έμ§λ₯Ό λΆμνμ¬ νλ₯ λμ λ리μ μ€λͺ μ λ°ννλ ν¨μ""" | |
| if image is None: | |
| return None, "μ΄λ―Έμ§λ₯Ό μ λ‘λν΄μ£ΌμΈμ." | |
| results = image_classifier(image) | |
| confidences = {res['label']: res['score'] for res in results} | |
| top_label = results[0]['label'] | |
| description = f"μ΄ μ΄λ―Έμ§λ λμ νλ₯ λ‘ **'{top_label}'**κ³Ό(μ) κ΄λ ¨μ΄ μμ΄ λ³΄μ λλ€." | |
| return confidences, description | |
| # νλ©΄ κ΅¬μ± (Gradio Blocks) | |
| with gr.Blocks() as demo: # Gradio 6.0 κ·κ²©μ λ§μΆ° μ΄κ³³μ μλ theme μμ±μ μ κ±°νμ΅λλ€. | |
| gr.Markdown("## πΌοΈ AI μ΄λ―Έμ§ λΆλ₯κΈ°") | |
| with gr.Row(): | |
| with gr.Column(): | |
| img_input = gr.Image(type="pil", label="μ΄λ―Έμ§ μ λ‘λ") | |
| img_btn = gr.Button("μ΄λ―Έμ§ λΆμ", variant="primary") | |
| with gr.Column(): | |
| img_label_output = gr.Label(label="Top 5 ν΄λμ€ νλ₯ ", num_top_classes=5) | |
| img_desc_output = gr.Markdown(label="AI μ΄λ―Έμ§ μ€λͺ ") | |
| img_btn.click(fn=analyze_image, inputs=img_input, outputs=[img_label_output, img_desc_output]) | |
| if __name__ == "__main__": | |
| # Gradio 6.0 μ΅μ λ¬Έλ²μ λ°λΌ theme μ΅μ μ launch() ν¨μ λ΄λΆλ‘ μ΄λμμΌ°μ΅λλ€. | |
| demo.launch(theme=gr.themes.Soft()) | |