bdjwhdwjb commited on
Commit
2391729
·
verified ·
1 Parent(s): 99a580e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -0
app.py CHANGED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
+ import urllib.parse
5
+
6
+ # Set Streamlit page configuration
7
+ st.set_page_config(
8
+ page_title="Text-to-Music Generator 🎵",
9
+ page_icon="🎵",
10
+ layout="wide",
11
+ initial_sidebar_state="expanded",
12
+ )
13
+
14
+ # Add custom CSS for styling
15
+ st.markdown("""
16
+ <style>
17
+ body {
18
+ background-color: #eaf6fb;
19
+ color: #003366;
20
+ font-family: 'Arial', sans-serif;
21
+ }
22
+ .stButton>button {
23
+ background-color: #4dabf5;
24
+ color: white;
25
+ font-weight: bold;
26
+ border-radius: 12px;
27
+ padding: 10px 20px;
28
+ }
29
+ .stButton>button:hover {
30
+ background-color: #007bb5;
31
+ }
32
+ .stTextArea textarea {
33
+ border: 2px solid #4dabf5;
34
+ border-radius: 8px;
35
+ }
36
+ iframe {
37
+ border: 2px solid #4dabf5;
38
+ border-radius: 8px;
39
+ }
40
+ .title {
41
+ text-align: center;
42
+ font-size: 36px;
43
+ font-weight: bold;
44
+ color: #003366;
45
+ }
46
+ .description {
47
+ text-align: center;
48
+ font-size: 18px;
49
+ color: #005792;
50
+ }
51
+ </style>
52
+ """, unsafe_allow_html=True)
53
+
54
+ # Initialize the Hugging Face model and tokenizer
55
+ @st.cache_resource
56
+ def load_model():
57
+ tokenizer = AutoTokenizer.from_pretrained('sander-wood/text-to-music')
58
+ model = AutoModelForSeq2SeqLM.from_pretrained('sander-wood/text-to-music')
59
+ return tokenizer, model
60
+
61
+ # Load model and tokenizer
62
+ tokenizer, model = load_model()
63
+
64
+ # Streamlit App UI
65
+ st.markdown("<div class='title'>🎵 Text-to-Music Generator 🎵</div>", unsafe_allow_html=True)
66
+ st.markdown("""
67
+ <div class='description'>
68
+ Enter a textual description, and the model will generate music in ABC notation.
69
+ You can use tools like [abc2midi](http://abc.sourceforge.net/abcMIDI/) to convert the notation into a playable file.
70
+ </div>
71
+ """, unsafe_allow_html=True)
72
+
73
+ # Input Fields
74
+ with st.container():
75
+ text_input = st.text_area(
76
+ "Enter a description for the music:",
77
+ placeholder="e.g., This is a traditional Irish dance music.",
78
+ )
79
+ max_length = st.slider(
80
+ "Maximum Length of Generated Music:", min_value=128, max_value=2048, value=1024, step=128
81
+ )
82
+ top_p = st.slider(
83
+ "Top-p (Nucleus Sampling):", min_value=0.1, max_value=1.0, value=0.9, step=0.05
84
+ )
85
+ temperature = st.slider(
86
+ "Temperature (Sampling Diversity):", min_value=0.1, max_value=2.0, value=1.0, step=0.1
87
+ )
88
+
89
+ # Generate Music Button
90
+ if st.button("Generate Music 🎶"):
91
+ if not text_input.strip():
92
+ st.error("Please enter a valid description!")
93
+ else:
94
+ st.info("Generating music... This might take a few seconds.")
95
+
96
+ try:
97
+ # Tokenize input
98
+ input_ids = tokenizer(text_input, return_tensors='pt', truncation=True, max_length=max_length)['input_ids']
99
+
100
+ # Generate music using efficient beam search sampling
101
+ generated_ids = model.generate(
102
+ input_ids,
103
+ max_length=max_length,
104
+ do_sample=True,
105
+ top_p=top_p,
106
+ temperature=temperature,
107
+ eos_token_id=tokenizer.eos_token_id,
108
+ )
109
+
110
+ # Decode generated music
111
+ tune = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
112
+ tune = "X:1\n" + tune
113
+
114
+ st.success("Music generated successfully!")
115
+
116
+ # Display raw generated music in the app
117
+ st.text_area("Generated Music (ABC Notation):", value=tune, height=300)
118
+
119
+ # Encode tune for URL and embed ABCJS Editor
120
+ encoded_tune = urllib.parse.quote(tune)
121
+ editor_url = f"https://www.abcjs.net/abcjs-editor?abc={encoded_tune}"
122
+ st.markdown(f"""
123
+ ### ABCJS Editor Preview
124
+ You can edit or play the music below:
125
+ <iframe src="{editor_url}" width="100%" height="500" style="border:none;"></iframe>
126
+ """, unsafe_allow_html=True)
127
+ except Exception as e:
128
+ st.error(f"An error occurred: {e}")