File size: 2,111 Bytes
ab275ec
 
 
7a9fb4e
ab275ec
 
 
 
 
 
 
 
 
 
 
 
 
 
a3f03ec
 
c96a20e
ab275ec
 
 
 
 
 
 
1908308
ab275ec
 
 
130f62b
68d3472
4d4d322
55ab5a8
4d4d322
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8abf67b
 
4d4d322
 
a3f03ec
ab275ec
4d4d322
ab275ec
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
import gradio as gr
import torch
from transformers import pipeline

from transformers import AutoProcessor, MusicgenForConditionalGeneration
import scipy
import wave
import io

def generate_music(text):
    processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
    model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
    
    inputs = processor(
        text=text,
        padding=True,
        return_tensors="pt",
    )
    audio_values = model.generate(**inputs, do_sample=True
, guidance_scale=3
, max_new_tokens=1350)
    sampling_rate = model.config.audio_encoder.sampling_rate
    scipy.io.wavfile.write("musicgen_out.wav", rate=sampling_rate, data=audio_values[0, 0].numpy())
    file_path= "musicgen_out.wav"
    
    try:
        Audio = gr.Audio(file_path)
        return Audio
    except Exception as e:
        return str(e)


theme='Taithrah/Minimal'

# 创建 Gradio Blocks 界面
iface = gr.Blocks(css = ".gradio-container {background: url('file=background.jpg')}")

with iface as demo:
    # 创建一个列容器
    with gr.Row():
        # 在左侧列中添加文本输入框
        with gr.Column(scale=1):
            input_text = gr.Textbox(lines=2, placeholder="Please enter your lyric here...")
            example_demo = gr.Markdown("## Example Demo\n\nHere is an example demo for you to try.")
            company_intro = gr.Markdown("## Company Introduction\n\nHere you can add information about your company.")
        
        # 在右侧列中添加音频输出组件
        with gr.Column(scale=3):
            output_audio = gr.Audio(label="Generated Music")
            generate_button = gr.Button("Generate Music")
            generate_button.click(fn=generate_music, inputs=input_text, outputs=output_audio)
    

    # 在网页左上方加入公司logo
    # with gr.Row():
    #     gr.Image("logo.jpg", width=1)


        
# 启动界面
demo.queue(max_size=4).launch(share = True)


# demo = gr.Interface(
#   fn=generate_music,
#   inputs='text',
#   outputs='audio',
# )

# demo.queue(max_size=4).launch(share = True)