Spaces:
Runtime error
Runtime error
import gradio as gr | |
from transformers import pipeline | |
from PIL import Image | |
from pydub import AudioSegment | |
from pydub.playback import play | |
import io | |
# ์ด๋ฏธ์ง ์ธ์ ํ์ดํ๋ผ์ธ ๋ก๋ | |
model = pipeline("image-classification", model="google/vit-base-patch16-224") | |
# ์นดํ ๊ณ ๋ฆฌ์ ๋ฐ๋ฅธ ์ฌ์ด๋ ํ์ผ ๊ฒฝ๋ก๋ฅผ ์ ์ | |
sound_files = { | |
"dog": "dog_bark.mp3", | |
"cat": "cat_meow.mp3", | |
# ... ๊ฐ ์นดํ ๊ณ ๋ฆฌ์ ๋ํ ์ฌ์ด๋ ํ์ผ ๊ฒฝ๋ก ์ถ๊ฐ | |
} | |
def classify_image(uploaded_image): | |
predictions = model(uploaded_image) | |
# ๊ฐ์ฅ ํ๋ฅ ์ด ๋์ ์์ธก ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์ด | |
top_prediction = predictions[0]['label'] | |
# ์์ธก ๊ฒฐ๊ณผ์ ํด๋นํ๋ ์ฌ์ด๋ ํ์ผ์ ์ฌ์ | |
if top_prediction in sound_files: | |
sound_path = sound_files[top_prediction] | |
sound = AudioSegment.from_file(sound_path) | |
play(sound) | |
return {prediction['label']: prediction['score'] for prediction in predictions} | |
# Gradio ์ธํฐํ์ด์ค ์์ฑ | |
iface = gr.Interface(fn=classify_image, | |
inputs=gr.Image(type="pil"), | |
outputs=gr.Label(num_top_classes=3), | |
title="์ด๋ฏธ์ง ๋ถ๋ฅ ๋ฐ ์ฌ์ด๋ ์ฌ์", | |
description="์ด๋ฏธ์ง๋ฅผ ์ ๋ก๋ํ๋ฉด, ์ฌ๋ฌผ์ ์ธ์ํ๊ณ ํด๋นํ๋ ์ํฅ์ ์ฌ์ํฉ๋๋ค.") | |
# ์ธํฐํ์ด์ค ์คํ | |
iface.launch() | |