Bils commited on
Commit
2e54420
β€’
1 Parent(s): 728b1a0

Update radio_imaging_app.py

Browse files
Files changed (1) hide show
  1. 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='Radio Imaging Audio Generator Beta 0.1',
15
- initial_sidebar_state="expanded"
16
  )
17
 
18
- # Main Description and Header
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 Ai audio generator. This web application allows you to easily create unique audio for your radio imaging projects or any music creators using AI technology.")
24
  st.markdown("---")
25
 
26
- # How to Use the App - Instructions
27
- with st.expander('πŸ“˜ How to Use This Web App?'):
28
- st.markdown('''
29
- To get started with creating your unique audio pieces, follow these simple steps:
30
-
31
- **1. Enter OpenAI API Key**
32
- - In the sidebar, input your **OpenAI API key**. This is essential to access the GPT model for generating audio descriptions.
33
- - Don't have an API key? Get one for free [here](https://platform.openai.com/account/api-keys).
34
-
35
- **2. Select GPT Model**
36
- - Choose the desired GPT model from the dropdown in the sidebar. We recommend using **'gpt-3.5-turbo-16k'** for more detailed and rich descriptions.
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 key", type="password", help="Enter your OpenAI API key here.")
55
- st.caption("*If you don't have an OpenAI API key, get it [here](https://platform.openai.com/account/api-keys).*")
56
- model = st.selectbox("OpenAI chat model", ("gpt-3.5-turbo", "gpt-3.5-turbo-16k"), help="Select the desired GPT model.")
57
- st.markdown("Check out our video tutorials on [YouTube](https://www.youtube.com/channel/UCdDH7T3oa8YMPFV5e79skaA) for helpful guides on using this app!")
58
- st.markdown('''Made with ❀️ by [Bilsimaging](https://bilsimaging.com)''', unsafe_allow_html=True)
59
-
60
- # Guidelines for generating prompt and audio
61
- st.markdown("""
62
- ### πŸ’‘ Steps to Generate Your Audio:
63
- 1. **Write your Detailed Description**
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 Button with user confirmation and patience message
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 the OpenAI API key and a description for your radio imaging.")
82
  else:
83
- with st.spinner("Generating your prompt... Please wait, this might take a few moments."):
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 copyright notice or tag
90
- copyright_notice = "\n\nΒ© Created through Radio Imaging Audio Generator by Bilsimaging [WEBSITE](https://bilsimaging.com)"
91
- descriptive_text += copyright_notice
92
 
 
93
  st.session_state['generated_prompt'] = descriptive_text
94
- st.success("Your prompt has been successfully generated! Review the prompt below:")
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"An error occurred: {e}")
107
 
108
  st.markdown("---")
109
 
110
- # ... [Earlier parts of the script remain unchanged]
111
-
112
- # Generate Audio Button with Progress Bar and Load Management
113
- st.markdown("## 🎢 Generate Audio")
114
- st.info("🚨 Please be patient as generating audio can take some time. This might take a moment due to resource limits. Feel free to notify me if you encounter any issues.")
 
 
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
- # Placeholder for server load check
123
- server_ready_for_audio_generation = True # Replace with actual server load check logic
124
-
125
- if server_ready_for_audio_generation:
126
- with st.spinner("Generating your audio... Please wait, this might take a few moments."):
127
- progress_bar = st.progress(0)
128
- for i in range(100):
129
- time.sleep(0.1) # Simulate processing
130
- progress_bar.progress(i + 1)
131
-
132
- try:
133
- processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
134
- musicgen_model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
135
- inputs = processor(text=[descriptive_text], padding=True, return_tensors="pt")
136
- audio_values = musicgen_model.generate(**inputs, max_new_tokens=512)
137
- sampling_rate = musicgen_model.config.audio_encoder.sampling_rate
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 and Support Section
154
  st.markdown("---")
155
- st.markdown("## 🌐 Project Continuation and User Involvement")
156
- st.markdown("βœ”οΈ This app is the next step in our project, following the Custom GPTs Radio Imaging and MusicGen AI. <br>It's tailored for radio producers and music creators, offering new levels of creativity and efficiency by Bilsimaging. [Try our GPTs](https://chat.openai.com/g/g-65x53n87E-radio-imaging-musicgen-ai).", unsafe_allow_html=True)
157
- st.markdown("If you appreciate my deployment and wish to support me, please consider a donation. Your support helps me continue providing value. Thank you for joining me on this journey! - Bilel Aroua")
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)