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()