imageclass-app / app.py
yeansu lee
deploy3
520f384
Raw History Blame Contribute Delete
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")
@spaces.GPU # [핡심] 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())