File size: 1,246 Bytes
2bd9468
 
9902a40
c417d64
2bd9468
9902a40
891d8e1
2bd9468
9902a40
3382a71
 
c417d64
 
 
 
 
 
3382a71
9902a40
891d8e1
2bd9468
9902a40
2bd9468
c1a4cc8
891d8e1
2bd9468
3ec95cb
2bd9468
3382a71
 
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
import gradio as gr
from transformers import pipeline
from PIL import Image
import io

# ์ด๋ฏธ์ง€ ์ธ์‹ ํŒŒ์ดํ”„๋ผ์ธ ๋กœ๋“œ
model = pipeline("image-classification", model="google/vit-base-patch16-224")

def classify_image(uploaded_image):
    # ์—…๋กœ๋“œ๋œ ์ด๋ฏธ์ง€๊ฐ€ PIL ์ด๋ฏธ์ง€ ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ ๋ณ€ํ™˜
    if not isinstance(uploaded_image, Image.Image):
        try:
            # ์—…๋กœ๋“œ๋œ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๋ฅผ PIL ์ด๋ฏธ์ง€ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜
            uploaded_image = Image.open(io.BytesIO(uploaded_image))
        except Exception as e:
            # ๋ณ€ํ™˜ ์ค‘ ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ
            raise ValueError("Cannot convert the uploaded image to a PIL Image object: " + str(e))
    
    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()