echons commited on
Commit
3649333
1 Parent(s): 631a926

First commit

Browse files
Files changed (2) hide show
  1. app.py +82 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import MusicgenForConditionalGeneration
2
+ from transformers import AutoProcessor
3
+ import scipy
4
+
5
+ import streamlit as st
6
+
7
+ st.set_page_config(
8
+ page_title="Plant Orchestra with GenAI",
9
+ page_icon="🎵"
10
+ )
11
+
12
+ # initialise model
13
+ @st.cache_resource
14
+ def initialise_model():
15
+ try:
16
+ #processor = AutoProcessor.from_pretrained("musicgen-small")
17
+ processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
18
+ # model = MusicgenForConditionalGeneration.from_pretrained("musicgen-small")
19
+ model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
20
+ return processor, model
21
+ except Exception as e:
22
+ st.error(f"Error initializing the model: {str(e)}")
23
+ return None, None
24
+
25
+ processor, model = initialise_model()
26
+
27
+
28
+ # Generate audio with given prompt
29
+ def generate_audio(processor, model, prompt):
30
+ if processor is not None and model is not None:
31
+ try:
32
+ inputs = processor(
33
+ text=[prompt],
34
+ padding=True,
35
+ return_tensors="pt",
36
+ )
37
+ audio_values = model.generate(**inputs.to("cpu"), do_sample=True, guidance_scale=3, max_new_tokens=256)
38
+ return audio_values
39
+ except Exception as e:
40
+ st.error(f"Error generating audio: {str(e)}")
41
+ return None
42
+
43
+ # save audio file with scipy
44
+ def save_file(model, audio_values, filename):
45
+ sampling_rate = model.config.audio_encoder.sampling_rate
46
+ scipy.io.wavfile.write(filename, rate=sampling_rate, data=audio_values[0, 0].cpu().numpy())
47
+
48
+
49
+
50
+ st.title("Plant Orchestra 🌻")
51
+ st.markdown("Generate music based on your own plant orchestra.")
52
+
53
+
54
+ prompt = st.text_input(label='Prompt:', value='Sunflower temperature: 32.5C UV light intensity: 50% Soil water level: 3cm/h')
55
+ if st.button("Generate Music"):
56
+ #with st.spinner("Initialising model..."):
57
+ # processor, model = initialise_model()
58
+ if processor is not None and model is not None:
59
+ with st.spinner("Generating audio..."):
60
+ results = generate_audio(processor, model, prompt)
61
+ if results is not None:
62
+ with st.spinner("Saving audio..."):
63
+ filename = "plant_orchestra" + ".wav"
64
+ save_file(model, results, filename)
65
+ with st.spinner("Displaying audio..."):
66
+ with open(filename, "rb") as f:
67
+ generation = f.read()
68
+ st.write("Listen to the generated music:")
69
+ st.audio(generation)
70
+
71
+ # Add additional information and instructions for users
72
+ st.sidebar.subheader("How to Use:")
73
+ st.sidebar.write("1. Enter a plant condition prompt in the text input.")
74
+ st.sidebar.write("2. Click the 'Generate Music' button to create music based on the provided prompt.")
75
+ st.sidebar.write("3. You can listen to the generated music and download it.")
76
+
77
+ # Footer
78
+ st.write()
79
+ st.write()
80
+ st.write()
81
+ st.markdown("---")
82
+ st.markdown("Created with ❤️ by HS2912 W4 Group 2")
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ streamlit
2
+ transformers
3
+ scipy
4
+ torch