File size: 2,289 Bytes
2bd9468
 
a6d7b81
2bd9468
9902a40
8770d52
2bd9468
ebcd803
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3377e03
a6d7b81
 
 
 
 
 
 
 
 
 
 
87c119f
3377e03
8770d52
 
a6d7b81
 
3377e03
 
ebcd803
 
a6d7b81
ebcd803
a6d7b81
ebcd803
ad7babb
a6d7b81
23708c8
3377e03
23708c8
e6812f7
872e164
11179ad
bc25881
23708c8
2bd9468
ad7babb
3382a71
a6d7b81
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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(prompt):
    # μŒμ•… 생성 API 호좜
    client = Client("https://haoheliu-audioldm2-text2audio-text2music.hf.space/")
    result = client.predict(
        prompt,
        prompt,  # μŒμ„± 생성에 μ‚¬μš©λ  ν”„λ‘¬ν”„νŠΈ
        5,  # μŒμ•…μ˜ 길이 (초)
        0,  # κ°€μ΄λ˜μŠ€ μŠ€μΌ€μΌ
        5,  # μ‹œλ“œ κ°’
        1,  # 생성할 waveform의 수
        fn_index=1  # ν•¨μˆ˜ 인덱슀
    )
    # 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 the object and generate voice)",
    examples=["dog.jpg", "cat.png", "cafe.jpg"]  # μˆ˜μ •λœ λΆ€λΆ„: 콀마 μΆ”κ°€
)


# μΈν„°νŽ˜μ΄μŠ€ μ‹€ν–‰
iface.launch()