Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import os
|
2 |
import requests
|
3 |
import torch
|
4 |
-
import scipy.io.wavfile
|
5 |
import streamlit as st
|
6 |
from io import BytesIO
|
7 |
from transformers import (
|
@@ -14,85 +14,55 @@ from transformers import (
|
|
14 |
from streamlit_lottie import st_lottie
|
15 |
|
16 |
# ---------------------------------------------------------------------
|
17 |
-
# 1) PAGE
|
18 |
# ---------------------------------------------------------------------
|
19 |
st.set_page_config(
|
20 |
-
page_title="Radio Imaging
|
21 |
-
page_icon="
|
22 |
layout="wide"
|
23 |
)
|
24 |
|
25 |
# ---------------------------------------------------------------------
|
26 |
-
# 2) CUSTOM CSS /
|
27 |
# ---------------------------------------------------------------------
|
28 |
CUSTOM_CSS = """
|
29 |
<style>
|
30 |
-
/* Dark background with Spotify-like vibe */
|
31 |
body {
|
32 |
background-color: #121212;
|
33 |
color: #FFFFFF;
|
34 |
font-family: "Helvetica Neue", sans-serif;
|
35 |
}
|
36 |
-
|
37 |
.block-container {
|
38 |
max-width: 1100px;
|
39 |
padding: 1rem 1.5rem;
|
40 |
}
|
41 |
-
|
42 |
h1, h2, h3 {
|
43 |
color: #1DB954;
|
44 |
-
margin-bottom: 0.5rem;
|
45 |
}
|
46 |
-
|
47 |
-
/* Rounded, bright green button on hover */
|
48 |
.stButton>button {
|
49 |
background-color: #1DB954 !important;
|
50 |
color: #FFFFFF !important;
|
51 |
border-radius: 24px;
|
52 |
-
|
53 |
-
font-size: 16px !important;
|
54 |
-
padding: 0.6rem 1.2rem !important;
|
55 |
-
transition: background-color 0.3s ease;
|
56 |
}
|
57 |
.stButton>button:hover {
|
58 |
background-color: #1ed760 !important;
|
59 |
}
|
60 |
-
|
61 |
-
/* Sidebar: black background */
|
62 |
-
.sidebar .sidebar-content {
|
63 |
-
background-color: #000000;
|
64 |
-
color: #FFFFFF;
|
65 |
-
}
|
66 |
-
|
67 |
textarea, input, select {
|
68 |
border-radius: 8px !important;
|
69 |
background-color: #282828 !important;
|
70 |
color: #FFFFFF !important;
|
71 |
-
border: 1px solid #3e3e3e;
|
72 |
}
|
73 |
-
|
74 |
-
/* Audio styling */
|
75 |
audio {
|
76 |
width: 100%;
|
77 |
margin-top: 1rem;
|
78 |
}
|
79 |
-
|
80 |
-
/* Lottie container */
|
81 |
-
.lottie-container {
|
82 |
-
display: flex;
|
83 |
-
justify-content: center;
|
84 |
-
margin-bottom: 20px;
|
85 |
-
}
|
86 |
-
|
87 |
-
/* Footer */
|
88 |
.footer-note {
|
89 |
text-align: center;
|
90 |
font-size: 14px;
|
91 |
opacity: 0.7;
|
92 |
margin-top: 2rem;
|
93 |
}
|
94 |
-
|
95 |
-
/* Hide Streamlit branding if you wish */
|
96 |
#MainMenu, footer {visibility: hidden;}
|
97 |
</style>
|
98 |
"""
|
@@ -112,18 +82,11 @@ LOTTIE_URL = "https://assets3.lottiefiles.com/temp/lf20_Q6h5zV.json"
|
|
112 |
lottie_animation = load_lottie_url(LOTTIE_URL)
|
113 |
|
114 |
# ---------------------------------------------------------------------
|
115 |
-
# 4) LOAD LLAMA 3 (GATED MODEL)
|
116 |
# ---------------------------------------------------------------------
|
117 |
@st.cache_resource
|
118 |
def load_llama_pipeline(model_id: str, device: str, token: str):
|
119 |
-
|
120 |
-
Load the Llama 3 model from Hugging Face with a user token.
|
121 |
-
token: The HF access token from environment or secrets.
|
122 |
-
"""
|
123 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
124 |
-
model_id,
|
125 |
-
use_auth_token=token
|
126 |
-
)
|
127 |
model = AutoModelForCausalLM.from_pretrained(
|
128 |
model_id,
|
129 |
use_auth_token=token,
|
@@ -139,7 +102,7 @@ def load_llama_pipeline(model_id: str, device: str, token: str):
|
|
139 |
return text_gen_pipeline
|
140 |
|
141 |
# ---------------------------------------------------------------------
|
142 |
-
# 5)
|
143 |
# ---------------------------------------------------------------------
|
144 |
def generate_radio_script(user_input: str, pipeline_llama) -> str:
|
145 |
system_prompt = (
|
@@ -155,7 +118,6 @@ def generate_radio_script(user_input: str, pipeline_llama) -> str:
|
|
155 |
temperature=0.9
|
156 |
)
|
157 |
output_text = result[0]["generated_text"]
|
158 |
-
|
159 |
if "Refined script:" in output_text:
|
160 |
output_text = output_text.split("Refined script:", 1)[-1].strip()
|
161 |
output_text += "\n\n(Generated by Llama 3 - Radio Imaging)"
|
@@ -171,49 +133,22 @@ def load_musicgen_model():
|
|
171 |
return mg_model, mg_processor
|
172 |
|
173 |
# ---------------------------------------------------------------------
|
174 |
-
# 7)
|
175 |
-
# ---------------------------------------------------------------------
|
176 |
-
with st.sidebar:
|
177 |
-
st.header("🎚 Radio Library")
|
178 |
-
st.write("**My Stations**")
|
179 |
-
st.write("- Favorites")
|
180 |
-
st.write("- Recently Generated")
|
181 |
-
st.write("- Top Hits")
|
182 |
-
st.write("---")
|
183 |
-
st.write("**Settings**")
|
184 |
-
st.markdown("<br>", unsafe_allow_html=True)
|
185 |
-
|
186 |
-
# ---------------------------------------------------------------------
|
187 |
-
# 8) HEADER
|
188 |
# ---------------------------------------------------------------------
|
189 |
-
|
|
|
|
|
|
|
190 |
|
191 |
-
|
192 |
-
|
193 |
-
st.subheader("Gated Model + MusicGen Audio")
|
194 |
-
|
195 |
-
st.markdown(
|
196 |
-
"""
|
197 |
-
Create **radio imaging promos** and **jingles** with Llama 3 + MusicGen.
|
198 |
-
**Note**:
|
199 |
-
- You must have access to `"meta-llama/Meta-Llama-3-70B"` on Hugging Face.
|
200 |
-
- You must provide your HF token in the environment (e.g., HF_TOKEN).
|
201 |
-
"""
|
202 |
-
)
|
203 |
-
with col2:
|
204 |
-
if lottie_animation:
|
205 |
-
with st.container():
|
206 |
-
st_lottie(lottie_animation, height=180, loop=True, key="radio_lottie")
|
207 |
-
else:
|
208 |
-
st.write("*No animation loaded.*")
|
209 |
|
210 |
st.markdown("---")
|
211 |
|
212 |
# ---------------------------------------------------------------------
|
213 |
-
#
|
214 |
# ---------------------------------------------------------------------
|
215 |
-
st.subheader("
|
216 |
-
|
217 |
prompt = st.text_area(
|
218 |
"Example: 'A 15-second hype jingle for a morning talk show, fun and energetic.'",
|
219 |
height=120
|
@@ -223,79 +158,75 @@ col_model, col_device = st.columns(2)
|
|
223 |
with col_model:
|
224 |
llama_model_id = st.text_input(
|
225 |
"Llama 3 Model ID",
|
226 |
-
value="meta-llama/Meta-Llama-3-70B",
|
227 |
-
help="
|
228 |
)
|
229 |
with col_device:
|
230 |
device_option = st.selectbox(
|
231 |
-
"Device
|
232 |
["auto", "cpu"],
|
233 |
-
help="
|
234 |
)
|
235 |
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
st.error("No HF_TOKEN found. Please set it in your HF Space secrets or environment variables.")
|
240 |
st.stop()
|
241 |
|
242 |
-
if st.button("
|
243 |
if not prompt.strip():
|
244 |
-
st.error("Please
|
245 |
else:
|
246 |
-
with st.spinner("Generating script
|
247 |
try:
|
248 |
-
|
249 |
-
final_script = generate_radio_script(prompt,
|
250 |
-
st.session_state["final_script"] = final_script
|
251 |
st.success("Promo script generated!")
|
252 |
-
st.
|
253 |
except Exception as e:
|
254 |
st.error(f"Llama generation error: {e}")
|
255 |
|
256 |
st.markdown("---")
|
257 |
|
258 |
# ---------------------------------------------------------------------
|
259 |
-
#
|
260 |
# ---------------------------------------------------------------------
|
261 |
-
st.subheader("
|
262 |
-
|
263 |
-
audio_length = st.slider("MusicGen Max Tokens (approx track length)", 128, 1024, 512, 64)
|
264 |
|
265 |
-
if st.button("
|
266 |
if "final_script" not in st.session_state:
|
267 |
-
st.error("
|
268 |
else:
|
269 |
-
with st.spinner("
|
270 |
try:
|
271 |
mg_model, mg_processor = load_musicgen_model()
|
272 |
-
text_for_audio = st.session_state["final_script"]
|
273 |
-
|
274 |
inputs = mg_processor(
|
275 |
-
text=[
|
276 |
padding=True,
|
277 |
return_tensors="pt"
|
278 |
)
|
279 |
audio_values = mg_model.generate(**inputs, max_new_tokens=audio_length)
|
280 |
sr = mg_model.config.audio_encoder.sampling_rate
|
|
|
281 |
|
282 |
-
|
283 |
-
|
|
|
284 |
|
285 |
-
st.success("Audio generated!
|
286 |
-
st.audio(
|
287 |
except Exception as e:
|
288 |
st.error(f"MusicGen error: {e}")
|
289 |
|
290 |
# ---------------------------------------------------------------------
|
291 |
-
#
|
292 |
# ---------------------------------------------------------------------
|
293 |
st.markdown("---")
|
294 |
st.markdown(
|
295 |
"""
|
296 |
<div class="footer-note">
|
297 |
-
© 2025 Radio Imaging
|
298 |
-
Log in or provide <code>HF_TOKEN</code> and ensure access to <strong>meta-llama/Llama-3-70B-Instruct</strong>.
|
299 |
</div>
|
300 |
""",
|
301 |
unsafe_allow_html=True
|
|
|
1 |
import os
|
2 |
import requests
|
3 |
import torch
|
4 |
+
import scipy.io.wavfile as wav
|
5 |
import streamlit as st
|
6 |
from io import BytesIO
|
7 |
from transformers import (
|
|
|
14 |
from streamlit_lottie import st_lottie
|
15 |
|
16 |
# ---------------------------------------------------------------------
|
17 |
+
# 1) PAGE CONFIGURATION
|
18 |
# ---------------------------------------------------------------------
|
19 |
st.set_page_config(
|
20 |
+
page_title="AI Radio Imaging with Llama 3",
|
21 |
+
page_icon="\ud83c\udfa7",
|
22 |
layout="wide"
|
23 |
)
|
24 |
|
25 |
# ---------------------------------------------------------------------
|
26 |
+
# 2) CUSTOM CSS / UI DESIGN
|
27 |
# ---------------------------------------------------------------------
|
28 |
CUSTOM_CSS = """
|
29 |
<style>
|
|
|
30 |
body {
|
31 |
background-color: #121212;
|
32 |
color: #FFFFFF;
|
33 |
font-family: "Helvetica Neue", sans-serif;
|
34 |
}
|
|
|
35 |
.block-container {
|
36 |
max-width: 1100px;
|
37 |
padding: 1rem 1.5rem;
|
38 |
}
|
|
|
39 |
h1, h2, h3 {
|
40 |
color: #1DB954;
|
|
|
41 |
}
|
|
|
|
|
42 |
.stButton>button {
|
43 |
background-color: #1DB954 !important;
|
44 |
color: #FFFFFF !important;
|
45 |
border-radius: 24px;
|
46 |
+
padding: 0.6rem 1.2rem;
|
|
|
|
|
|
|
47 |
}
|
48 |
.stButton>button:hover {
|
49 |
background-color: #1ed760 !important;
|
50 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
textarea, input, select {
|
52 |
border-radius: 8px !important;
|
53 |
background-color: #282828 !important;
|
54 |
color: #FFFFFF !important;
|
|
|
55 |
}
|
|
|
|
|
56 |
audio {
|
57 |
width: 100%;
|
58 |
margin-top: 1rem;
|
59 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
.footer-note {
|
61 |
text-align: center;
|
62 |
font-size: 14px;
|
63 |
opacity: 0.7;
|
64 |
margin-top: 2rem;
|
65 |
}
|
|
|
|
|
66 |
#MainMenu, footer {visibility: hidden;}
|
67 |
</style>
|
68 |
"""
|
|
|
82 |
lottie_animation = load_lottie_url(LOTTIE_URL)
|
83 |
|
84 |
# ---------------------------------------------------------------------
|
85 |
+
# 4) LOAD LLAMA 3 (GATED MODEL)
|
86 |
# ---------------------------------------------------------------------
|
87 |
@st.cache_resource
|
88 |
def load_llama_pipeline(model_id: str, device: str, token: str):
|
89 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=token)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
model = AutoModelForCausalLM.from_pretrained(
|
91 |
model_id,
|
92 |
use_auth_token=token,
|
|
|
102 |
return text_gen_pipeline
|
103 |
|
104 |
# ---------------------------------------------------------------------
|
105 |
+
# 5) GENERATE RADIO SCRIPT
|
106 |
# ---------------------------------------------------------------------
|
107 |
def generate_radio_script(user_input: str, pipeline_llama) -> str:
|
108 |
system_prompt = (
|
|
|
118 |
temperature=0.9
|
119 |
)
|
120 |
output_text = result[0]["generated_text"]
|
|
|
121 |
if "Refined script:" in output_text:
|
122 |
output_text = output_text.split("Refined script:", 1)[-1].strip()
|
123 |
output_text += "\n\n(Generated by Llama 3 - Radio Imaging)"
|
|
|
133 |
return mg_model, mg_processor
|
134 |
|
135 |
# ---------------------------------------------------------------------
|
136 |
+
# 7) HEADER
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
# ---------------------------------------------------------------------
|
138 |
+
st.title("\ud83c\udfa7 AI Radio Imaging with Llama 3")
|
139 |
+
st.subheader("Create engaging radio promos with Llama 3 + MusicGen")
|
140 |
+
st.markdown("""Create **radio imaging promos** and **jingles** easily. Ensure you have access to
|
141 |
+
**meta-llama/Meta-Llama-3-70B** on Hugging Face and provide your token below.""")
|
142 |
|
143 |
+
if lottie_animation:
|
144 |
+
st_lottie(lottie_animation, height=180, loop=True, key="radio_lottie")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
st.markdown("---")
|
147 |
|
148 |
# ---------------------------------------------------------------------
|
149 |
+
# 8) USER INPUT
|
150 |
# ---------------------------------------------------------------------
|
151 |
+
st.subheader("\ud83c\udfa4 Step 1: Describe Your Promo Idea")
|
|
|
152 |
prompt = st.text_area(
|
153 |
"Example: 'A 15-second hype jingle for a morning talk show, fun and energetic.'",
|
154 |
height=120
|
|
|
158 |
with col_model:
|
159 |
llama_model_id = st.text_input(
|
160 |
"Llama 3 Model ID",
|
161 |
+
value="meta-llama/Meta-Llama-3-70B",
|
162 |
+
help="Enter the exact model ID from Hugging Face."
|
163 |
)
|
164 |
with col_device:
|
165 |
device_option = st.selectbox(
|
166 |
+
"Device",
|
167 |
["auto", "cpu"],
|
168 |
+
help="Choose GPU (auto) or CPU."
|
169 |
)
|
170 |
|
171 |
+
hf_token = os.getenv("HF_TOKEN")
|
172 |
+
if not hf_token:
|
173 |
+
st.error("No HF_TOKEN found. Please set it in your environment.")
|
|
|
174 |
st.stop()
|
175 |
|
176 |
+
if st.button("\u270d Generate Promo Script"):
|
177 |
if not prompt.strip():
|
178 |
+
st.error("Please provide a concept first.")
|
179 |
else:
|
180 |
+
with st.spinner("Generating script..."):
|
181 |
try:
|
182 |
+
llama_pipeline = load_llama_pipeline(llama_model_id, device_option, hf_token)
|
183 |
+
final_script = generate_radio_script(prompt, llama_pipeline)
|
|
|
184 |
st.success("Promo script generated!")
|
185 |
+
st.text_area("Generated Script", value=final_script, height=200)
|
186 |
except Exception as e:
|
187 |
st.error(f"Llama generation error: {e}")
|
188 |
|
189 |
st.markdown("---")
|
190 |
|
191 |
# ---------------------------------------------------------------------
|
192 |
+
# 9) GENERATE AUDIO WITH MUSICGEN
|
193 |
# ---------------------------------------------------------------------
|
194 |
+
st.subheader("\ud83c\udfb5 Step 2: Generate Audio")
|
195 |
+
audio_length = st.slider("Track Length (tokens)", 128, 1024, 512, 64)
|
|
|
196 |
|
197 |
+
if st.button("\ud83c\udfa7 Create Audio"):
|
198 |
if "final_script" not in st.session_state:
|
199 |
+
st.error("Please generate a script first.")
|
200 |
else:
|
201 |
+
with st.spinner("Generating audio..."):
|
202 |
try:
|
203 |
mg_model, mg_processor = load_musicgen_model()
|
|
|
|
|
204 |
inputs = mg_processor(
|
205 |
+
text=[st.session_state["final_script"]],
|
206 |
padding=True,
|
207 |
return_tensors="pt"
|
208 |
)
|
209 |
audio_values = mg_model.generate(**inputs, max_new_tokens=audio_length)
|
210 |
sr = mg_model.config.audio_encoder.sampling_rate
|
211 |
+
output_file = "radio_jingle.wav"
|
212 |
|
213 |
+
audio_data = audio_values[0, 0].cpu().numpy()
|
214 |
+
normalized_audio = (audio_data / max(abs(audio_data)) * 32767).astype("int16")
|
215 |
+
wav.write(output_file, rate=sr, data=normalized_audio)
|
216 |
|
217 |
+
st.success("Audio generated! Play it below:")
|
218 |
+
st.audio(output_file)
|
219 |
except Exception as e:
|
220 |
st.error(f"MusicGen error: {e}")
|
221 |
|
222 |
# ---------------------------------------------------------------------
|
223 |
+
# 10) FOOTER
|
224 |
# ---------------------------------------------------------------------
|
225 |
st.markdown("---")
|
226 |
st.markdown(
|
227 |
"""
|
228 |
<div class="footer-note">
|
229 |
+
© 2025 AI Radio Imaging – Built with Hugging Face & Streamlit
|
|
|
230 |
</div>
|
231 |
""",
|
232 |
unsafe_allow_html=True
|