msVision_3 / app.py
seawolf2357's picture
Update app.py
3382a71 verified
raw
history blame
1.15 kB
import gradio as gr
from transformers import pipeline
from PIL import Image
# ์ด๋ฏธ์ง€ ์ธ์‹ ํŒŒ์ดํ”„๋ผ์ธ ๋กœ๋“œ
model = pipeline("image-classification", model="google/vit-base-patch16-224")
def classify_image(uploaded_image):
# ์—…๋กœ๋“œ๋œ ์ด๋ฏธ์ง€๊ฐ€ PIL ์ด๋ฏธ์ง€ ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ ๋ณ€ํ™˜
if not isinstance(uploaded_image, Image.Image):
# ์—…๋กœ๋“œ๋œ ์ด๋ฏธ์ง€๊ฐ€ PIL ์ด๋ฏธ์ง€ ๊ฐ์ฒด๊ฐ€ ์•„๋‹ˆ๋ฉด, ์ด ๋ถ€๋ถ„์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋กœ์ง ์ถ”๊ฐ€
# ์˜ˆ: uploaded_image = Image.open(io.BytesIO(uploaded_image))
raise ValueError("Uploaded image is not a PIL Image object.")
predictions = model(uploaded_image)
return {prediction['label']: prediction['score'] for prediction in predictions}
# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
iface = gr.Interface(fn=classify_image,
inputs=gr.Image(),
outputs=gr.Label(num_top_classes=3),
title="์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜๊ธฐ",
description="์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜๋ฉด, ์‚ฌ๋ฌผ์„ ์ธ์‹ํ•˜๊ณ  ์ตœ์ƒ์œ„ 3๊ฐœ์˜ ๋ถ„๋ฅ˜ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.")
# ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
iface.launch()