Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
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)
|