Spaces:
Runtime error
Runtime error
Update radio_imaging_app.py
Browse files- radio_imaging_app.py +66 -118
radio_imaging_app.py
CHANGED
@@ -2,161 +2,109 @@ import streamlit as st
|
|
2 |
from transformers import AutoProcessor, MusicgenForConditionalGeneration
|
3 |
import scipy.io.wavfile
|
4 |
import openai
|
5 |
-
import time # Used for simulating progress
|
6 |
import torch
|
7 |
-
import tensorflow as tf
|
8 |
|
9 |
|
10 |
# Streamlit app setup
|
11 |
st.set_page_config(
|
12 |
-
page_icon="https://soundboard.bilsimaging.com/faviconbilsimaging.png",
|
13 |
layout="wide",
|
14 |
-
page_title=
|
15 |
-
initial_sidebar_state="expanded"
|
16 |
)
|
17 |
|
18 |
-
#
|
19 |
st.markdown("""
|
20 |
<h1 style=''>Radio Imaging Audio Generator
|
21 |
<span style='font-size: 24px; color: #FDC74A;'>Beta 0.1</span></h1>
|
22 |
""", unsafe_allow_html=True)
|
23 |
-
st.write("Welcome to the Radio Imaging & MusicGen
|
24 |
st.markdown("---")
|
25 |
|
26 |
-
#
|
27 |
-
with st.expander(
|
28 |
-
st.markdown(
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
**3. Input Your Detailed Description**
|
39 |
-
- Describe your audio idea in the text area provided. Be as detailed as possible to guide the AI effectively. This could include the mood, style, specific instruments, or any other relevant details.
|
40 |
-
|
41 |
-
**4. Generate and Review the Prompt**
|
42 |
-
- Click on **'π Generate Prompt'** to create a descriptive prompt for your audio. Review it to ensure it aligns with your vision.
|
43 |
-
|
44 |
-
**5. Generate Your Audio**
|
45 |
-
- If you're satisfied with the prompt, hit **'βΆ Generate Audio'**. This will process your request and create the audio piece based on the AI-generated description.
|
46 |
-
|
47 |
-
**6. Playback and Download**
|
48 |
-
- Once generated, you can play the audio directly within the app. If it meets your needs, feel free to download and use it in your projects.
|
49 |
-
''')
|
50 |
-
|
51 |
-
|
52 |
-
# Sidebar for user inputs
|
53 |
with st.sidebar:
|
54 |
-
openai_api_key = st.text_input("OpenAI API
|
55 |
-
st.caption("
|
56 |
-
model = st.selectbox("
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
2. **Generate Prompt**: Click 'Generate Prompt' to create a description for your radio imaging audio.
|
65 |
-
3. **Review the Prompt**: Read the output and make sure it aligns with what you have in mind.
|
66 |
-
4. **Generate Audio**: If you are satisfied with the prompt, click 'Generate Audio' to create your audio piece.
|
67 |
-
""")
|
68 |
-
|
69 |
-
# Prompt input
|
70 |
-
st.markdown("## βπ»Write your Description")
|
71 |
-
prompt = st.text_area("Enter your radio imaging draft idea prompt here", help="Describe the audio piece you want to create.")
|
72 |
-
|
73 |
-
# Instructions for users
|
74 |
-
st.info("ππ» Provide a detailed description of the audio you need, such as mood, instruments, and style. Example: A calm, soothing melody with soft piano for a morning show.")
|
75 |
|
76 |
-
# Generate Prompt
|
77 |
-
st.markdown("## π Generate Prompt")
|
78 |
-
st.info("π¨ Generating the prompt may take a few moments. Please be patient.")
|
79 |
if st.button("π Generate Prompt"):
|
80 |
if not openai_api_key.strip() or not prompt.strip():
|
81 |
-
st.error("Please provide both
|
82 |
else:
|
83 |
-
with st.spinner("Generating your prompt... Please wait
|
84 |
try:
|
|
|
85 |
full_prompt = {"role": "user", "content": f"Describe a radio imaging audio piece based on: {prompt}"}
|
86 |
response = openai.ChatCompletion.create(model=model, messages=[full_prompt], api_key=openai_api_key)
|
87 |
descriptive_text = response.choices[0].message['content'].strip()
|
88 |
|
89 |
-
# Append a
|
90 |
-
|
91 |
-
descriptive_text += copyright_notice
|
92 |
|
|
|
93 |
st.session_state['generated_prompt'] = descriptive_text
|
94 |
-
st.success("
|
95 |
st.write(descriptive_text)
|
96 |
-
|
97 |
-
# Download Button for the generated prompt
|
98 |
-
st.download_button(
|
99 |
-
label="Download Prompt",
|
100 |
-
data=descriptive_text,
|
101 |
-
file_name="generated_prompt.txt",
|
102 |
-
mime="text/plain"
|
103 |
-
)
|
104 |
-
|
105 |
except Exception as e:
|
106 |
-
st.error(f"
|
107 |
|
108 |
st.markdown("---")
|
109 |
|
110 |
-
#
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
|
|
115 |
|
|
|
116 |
if st.button("βΆ Generate Audio"):
|
117 |
if 'generated_prompt' not in st.session_state or not st.session_state['generated_prompt']:
|
118 |
st.error("Please generate and approve a prompt before creating audio.")
|
119 |
else:
|
120 |
descriptive_text = st.session_state['generated_prompt']
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
audio_filename = "Bilsimaging_radio_imaging_output.wav"
|
140 |
-
scipy.io.wavfile.write(audio_filename, rate=sampling_rate, data=audio_values[0, 0].numpy())
|
141 |
-
st.success("Your audio has been successfully created! Below is a description of your audio piece based on the GPT model's understanding:")
|
142 |
-
st.write(descriptive_text)
|
143 |
-
st.audio(audio_filename)
|
144 |
-
except Exception as e:
|
145 |
-
st.error(f"An error occurred: {e}")
|
146 |
-
finally:
|
147 |
-
progress_bar.empty() # Remove the progress bar after completion
|
148 |
-
else:
|
149 |
-
st.warning("The server is currently busy. Please try generating your audio again later.")
|
150 |
-
|
151 |
-
# ... [Rest of the code remains the same]
|
152 |
|
153 |
-
# Footer
|
154 |
st.markdown("---")
|
155 |
-
st.markdown("
|
156 |
-
|
157 |
-
|
158 |
-
st.markdown("For support β [Buy me a Coffee](https://ko-fi.com/bilsimaging).")
|
159 |
-
st.image('https://storage.ko-fi.com/cdn/brandasset/kofi_button_dark.png', width=300, caption="Project Bilsimaigng")
|
160 |
-
|
161 |
-
# Hide Streamlit branding
|
162 |
st.markdown("<style>#MainMenu {visibility: hidden;} footer {visibility: hidden;}</style>", unsafe_allow_html=True)
|
|
|
2 |
from transformers import AutoProcessor, MusicgenForConditionalGeneration
|
3 |
import scipy.io.wavfile
|
4 |
import openai
|
|
|
5 |
import torch
|
|
|
6 |
|
7 |
|
8 |
# Streamlit app setup
|
9 |
st.set_page_config(
|
10 |
+
page_icon="https://soundboard.bilsimaging.com/faviconbilsimaging.png",
|
11 |
layout="wide",
|
12 |
+
page_title="Radio Imaging Audio Generator Beta 0.1",
|
13 |
+
initial_sidebar_state="expanded",
|
14 |
)
|
15 |
|
16 |
+
# App Header
|
17 |
st.markdown("""
|
18 |
<h1 style=''>Radio Imaging Audio Generator
|
19 |
<span style='font-size: 24px; color: #FDC74A;'>Beta 0.1</span></h1>
|
20 |
""", unsafe_allow_html=True)
|
21 |
+
st.write("Welcome to the Radio Imaging & MusicGen AI audio generator. Easily create unique audio for your radio imaging projects or for music creation using cutting-edge AI technology.")
|
22 |
st.markdown("---")
|
23 |
|
24 |
+
# Instructions Section
|
25 |
+
with st.expander("π How to Use This Web App"):
|
26 |
+
st.markdown("""
|
27 |
+
1. **Enter OpenAI API Key**: Provide your API key in the sidebar to access the GPT model.
|
28 |
+
2. **Select GPT Model**: Choose the desired model, such as `gpt-3.5-turbo-16k`.
|
29 |
+
3. **Write a Description**: Provide a detailed description of your desired audio.
|
30 |
+
4. **Generate and Review the Prompt**: Generate a description and review the output.
|
31 |
+
5. **Generate Audio**: Use the description to create your audio file.
|
32 |
+
6. **Playback and Download**: Listen to or download the generated audio.
|
33 |
+
""")
|
34 |
+
|
35 |
+
# Sidebar Inputs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
with st.sidebar:
|
37 |
+
openai_api_key = st.text_input("π OpenAI API Key", type="password", help="Enter your OpenAI API key.")
|
38 |
+
st.caption("Need an API key? Get one [here](https://platform.openai.com/account/api-keys).")
|
39 |
+
model = st.selectbox("π Choose GPT Model", options=("gpt-3.5-turbo", "gpt-3.5-turbo-16k"))
|
40 |
+
|
41 |
+
# Prompt Input
|
42 |
+
st.markdown("## βπ» Write Your Description")
|
43 |
+
prompt = st.text_area(
|
44 |
+
"Describe the audio you'd like to generate.",
|
45 |
+
help="Include details like mood, instruments, style, or purpose (e.g., calm background music for a morning show)."
|
46 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
# Generate Prompt
|
|
|
|
|
49 |
if st.button("π Generate Prompt"):
|
50 |
if not openai_api_key.strip() or not prompt.strip():
|
51 |
+
st.error("Please provide both an OpenAI API key and a description.")
|
52 |
else:
|
53 |
+
with st.spinner("Generating your prompt... Please wait."):
|
54 |
try:
|
55 |
+
# Create a prompt and get response from OpenAI
|
56 |
full_prompt = {"role": "user", "content": f"Describe a radio imaging audio piece based on: {prompt}"}
|
57 |
response = openai.ChatCompletion.create(model=model, messages=[full_prompt], api_key=openai_api_key)
|
58 |
descriptive_text = response.choices[0].message['content'].strip()
|
59 |
|
60 |
+
# Append a credit line
|
61 |
+
descriptive_text += "\n\nΒ© Created using Radio Imaging Audio Generator by Bilsimaging"
|
|
|
62 |
|
63 |
+
# Save to session state
|
64 |
st.session_state['generated_prompt'] = descriptive_text
|
65 |
+
st.success("Prompt successfully generated!")
|
66 |
st.write(descriptive_text)
|
67 |
+
st.download_button("π₯ Download Prompt", descriptive_text, file_name="generated_prompt.txt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
except Exception as e:
|
69 |
+
st.error(f"Error while generating prompt: {e}")
|
70 |
|
71 |
st.markdown("---")
|
72 |
|
73 |
+
# Cache Model Loading
|
74 |
+
@st.cache_resource
|
75 |
+
def load_model():
|
76 |
+
"""Load and cache the MusicGen model and processor."""
|
77 |
+
model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
|
78 |
+
processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
|
79 |
+
return model, processor
|
80 |
|
81 |
+
# Generate Audio
|
82 |
if st.button("βΆ Generate Audio"):
|
83 |
if 'generated_prompt' not in st.session_state or not st.session_state['generated_prompt']:
|
84 |
st.error("Please generate and approve a prompt before creating audio.")
|
85 |
else:
|
86 |
descriptive_text = st.session_state['generated_prompt']
|
87 |
+
with st.spinner("Generating your audio... This might take a few moments."):
|
88 |
+
try:
|
89 |
+
# Load model and processor
|
90 |
+
musicgen_model, processor = load_model()
|
91 |
+
|
92 |
+
# Generate audio from the prompt
|
93 |
+
inputs = processor(text=[descriptive_text], padding=True, return_tensors="pt")
|
94 |
+
audio_values = musicgen_model.generate(**inputs, max_new_tokens=512)
|
95 |
+
sampling_rate = musicgen_model.config.audio_encoder.sampling_rate
|
96 |
+
|
97 |
+
# Save and display the audio
|
98 |
+
audio_filename = "Bilsimaging_radio_imaging_output.wav"
|
99 |
+
scipy.io.wavfile.write(audio_filename, rate=sampling_rate, data=audio_values[0, 0].numpy())
|
100 |
+
st.success("Audio successfully generated!")
|
101 |
+
st.audio(audio_filename)
|
102 |
+
except Exception as e:
|
103 |
+
st.error(f"Error while generating audio: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
+
# Footer Section
|
106 |
st.markdown("---")
|
107 |
+
st.markdown("""
|
108 |
+
βοΈ Made with β€οΈ by [Bilsimaging](https://bilsimaging.com). Your feedback and support help us grow!
|
109 |
+
""")
|
|
|
|
|
|
|
|
|
110 |
st.markdown("<style>#MainMenu {visibility: hidden;} footer {visibility: hidden;}</style>", unsafe_allow_html=True)
|