File size: 2,854 Bytes
3649333
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53df8a9
 
 
 
3649333
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
from transformers import MusicgenForConditionalGeneration
from transformers import AutoProcessor
import scipy

import streamlit as st

st.set_page_config(
    page_title="Plant Orchestra with GenAI",
    page_icon="🎵"
)

# initialise model
@st.cache_resource
def initialise_model():
    try:
        #processor = AutoProcessor.from_pretrained("musicgen-small")
        processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
        # model = MusicgenForConditionalGeneration.from_pretrained("musicgen-small")
        model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
        return processor, model
    except Exception as e:
        st.error(f"Error initializing the model: {str(e)}")
        return None, None
    
processor, model = initialise_model()
    

# Generate audio with given prompt
def generate_audio(processor, model, prompt):
    if processor is not None and model is not None:
        try:
            inputs = processor(
                text=[prompt],
                padding=True,
                return_tensors="pt",
            )
            audio_values = model.generate(**inputs.to("cpu"), do_sample=True, guidance_scale=3, max_new_tokens=256)
            return audio_values
        except Exception as e:
            st.error(f"Error generating audio: {str(e)}")
    return None

# save audio file with scipy
def save_file(model, audio_values, filename):
    sampling_rate = model.config.audio_encoder.sampling_rate
    scipy.io.wavfile.write(filename, rate=sampling_rate, data=audio_values[0, 0].cpu().numpy())



st.title("Plant Orchestra 🌻")
st.markdown("Generate music based on your own plant orchestra.")


prompt = st.text_input(label='Prompt:', value='Sunflower temperature: 32.5C UV light intensity: 50% Soil water level: 3cm/h')
if st.button("Generate Music"):
    #with st.spinner("Initialising model..."):
    #    processor, model = initialise_model()
    if processor is not None and model is not None:
        with st.spinner("Generating audio..."):
            results = generate_audio(processor, model, prompt)
        if results is not None:
            sampling_rate = model.config.audio_encoder.sampling_rate
            st.write("Listen to the generated music:")
            st.audio(sample_rate=sampling_rate, data=results[0, 0].cpu().numpy(), format="audio/wav")
            
            
# Add additional information and instructions for users
st.sidebar.subheader("How to Use:")
st.sidebar.write("1. Enter a plant condition prompt in the text input.")
st.sidebar.write("2. Click the 'Generate Music' button to create music based on the provided prompt.")
st.sidebar.write("3. You can listen to the generated music and download it.")

# Footer
st.write()
st.write()
st.write()
st.markdown("---")
st.markdown("Created with ❤️ by HS2912 W4 Group 2")