import gradio as gr from transformers import pipeline from gradio_client import Client # 가정: gradio_client 라이브러리가 사용 가능하다. # 이미지 인식 파이프라인 로드 image_model = pipeline("image-classification", model="google/vit-base-patch16-224") def generate_music(image_classification_result): # 음악 생성 API 호출 client = Client("https://haoheliu-audioldm2-text2audio-text2music.hf.space/") "The rnb beat of 85BPM drums. playing Violin." "Low quality." 5, # 음악의 길이 (초), 키워드 인자로 변경 0, # 가이던스 스케일, 키워드 인자로 변경 5, # 시드 값, 키워드 인자로 변경 1, # 생성할 waveform의 수, 키워드 인자로 변경 fn_index=1 # 함수 인덱스, 이미 키워드 인자로 되어 있음 ) # 이미지 분류 결과를 포함한 전체 프롬프트 생성 full_prompt = f"{base_prompt} Inspired by: {image_classification_result}." # API 호출 결과 처리 return result def generate_voice(prompt): # Tango API를 사용하여 음성 생성 client = Client("https://declare-lab-tango.hf.space/") result = client.predict( prompt, # 이미지 분류 결과를 프롬프트로 사용 100, # Steps 1, # Guidance Scale api_name="/predict" # API 엔드포인트 경로 ) # Tango API 호출 결과 처리 # 예: result에서 음성 파일 URL 또는 데이터 추출 return result def classify_and_generate_voice(uploaded_image): # 이미지 분류 predictions = image_model(uploaded_image) top_prediction = predictions[0]['label'] # 가장 확률이 높은 분류 결과 # 음성 생성 voice_result = generate_voice(top_prediction) # 음악 생성 music_result = generate_music("Generate music for: " + top_prediction) # 반환된 음성 및 음악 결과를 Gradio 인터페이스로 전달 # 예: voice_result['url'] 또는 voice_result['audio_data'] 등 return top_prediction, voice_result, music_result # Gradio 인터페이스 생성 iface = gr.Interface( fn=classify_and_generate_voice, inputs=gr.Image(type="pil"), outputs=[gr.Label(), gr.Audio(), gr.Audio()], title="msVision_3", description="이미지를 업로드하면, 사물을 인식하고 해당하는 음성 및 음악을 생성합니다.(recognizes object and generate Voice&Music)", examples=["dog.jpg", "cat.png", "cafe.jpg"] ) # 인터페이스 실행 iface.launch()