Bils commited on
Commit
a15d204
·
verified ·
1 Parent(s): e143767

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -29
app.py CHANGED
@@ -1,7 +1,9 @@
1
- import streamlit as st
2
  import requests
3
  import torch
4
  import scipy.io.wavfile
 
 
5
  from transformers import (
6
  AutoTokenizer,
7
  AutoModelForCausalLM,
@@ -9,26 +11,8 @@ from transformers import (
9
  AutoProcessor,
10
  MusicgenForConditionalGeneration
11
  )
12
- from io import BytesIO
13
  from streamlit_lottie import st_lottie
14
 
15
-
16
- import os
17
- from transformers import AutoTokenizer, AutoModelForCausalLM
18
-
19
- my_token = os.getenv("HF_TOKEN")
20
-
21
- tokenizer = AutoTokenizer.from_pretrained(
22
- "meta-llama/Llama-3-70B-Instruct",
23
- use_auth_token=my_token
24
- )
25
- model = AutoModelForCausalLM.from_pretrained(
26
- "meta-llama/Llama-3-70B-Instruct",
27
- use_auth_token=my_token,
28
- torch_dtype=torch.float16,
29
- device_map="auto"
30
- )
31
-
32
  # ---------------------------------------------------------------------
33
  # 1) PAGE CONFIG
34
  # ---------------------------------------------------------------------
@@ -131,17 +115,20 @@ lottie_animation = load_lottie_url(LOTTIE_URL)
131
  # 4) LOAD LLAMA 3 (GATED MODEL) - WITH use_auth_token
132
  # ---------------------------------------------------------------------
133
  @st.cache_resource
134
- def load_llama_pipeline(model_id: str, device: str):
135
  """
136
- Load the Llama 3 model from Hugging Face.
137
- Requires huggingface-cli login if model is gated.
138
  """
139
- tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=True)
 
 
 
140
  model = AutoModelForCausalLM.from_pretrained(
141
  model_id,
 
142
  torch_dtype=torch.float16 if device == "auto" else torch.float32,
143
- device_map=device,
144
- use_auth_token=True
145
  )
146
  text_gen_pipeline = pipeline(
147
  "text-generation",
@@ -208,8 +195,9 @@ with col1:
208
  st.markdown(
209
  """
210
  Create **radio imaging promos** and **jingles** with Llama 3 + MusicGen.
211
- **Note**: You must have access to `"meta-llama/Llama-3-70B-Instruct"` on Hugging Face,
212
- and be logged in via `huggingface-cli login`.
 
213
  """
214
  )
215
  with col2:
@@ -245,13 +233,19 @@ with col_device:
245
  help="If you have GPU, 'auto' tries to use it; CPU might be slow."
246
  )
247
 
 
 
 
 
 
 
248
  if st.button("📝 Generate Promo Script"):
249
  if not prompt.strip():
250
  st.error("Please type some concept first.")
251
  else:
252
  with st.spinner("Generating script with Llama 3..."):
253
  try:
254
- llm_pipeline = load_llama_pipeline(llama_model_id, device_option)
255
  final_script = generate_radio_script(prompt, llm_pipeline)
256
  st.session_state["final_script"] = final_script
257
  st.success("Promo script generated!")
@@ -301,7 +295,7 @@ st.markdown(
301
  """
302
  <div class="footer-note">
303
  © 2025 Radio Imaging with Llama 3 – Built using Hugging Face & Streamlit. <br>
304
- Log in via <code>huggingface-cli</code> and ensure access to <strong>meta-llama/Llama-3-70B-Instruct</strong>.
305
  </div>
306
  """,
307
  unsafe_allow_html=True
 
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 (
8
  AutoTokenizer,
9
  AutoModelForCausalLM,
 
11
  AutoProcessor,
12
  MusicgenForConditionalGeneration
13
  )
 
14
  from streamlit_lottie import st_lottie
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  # ---------------------------------------------------------------------
17
  # 1) PAGE CONFIG
18
  # ---------------------------------------------------------------------
 
115
  # 4) LOAD LLAMA 3 (GATED MODEL) - WITH use_auth_token
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,
130
  torch_dtype=torch.float16 if device == "auto" else torch.float32,
131
+ device_map=device
 
132
  )
133
  text_gen_pipeline = pipeline(
134
  "text-generation",
 
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/Llama-3-70B-Instruct"` on Hugging Face.
200
+ - You must provide your HF token in the environment (e.g., HF_TOKEN).
201
  """
202
  )
203
  with col2:
 
233
  help="If you have GPU, 'auto' tries to use it; CPU might be slow."
234
  )
235
 
236
+ # Grab your token from environment
237
+ my_token = os.getenv("HF_TOKEN")
238
+ if not my_token:
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("📝 Generate Promo Script"):
243
  if not prompt.strip():
244
  st.error("Please type some concept first.")
245
  else:
246
  with st.spinner("Generating script with Llama 3..."):
247
  try:
248
+ llm_pipeline = load_llama_pipeline(llama_model_id, device_option, my_token)
249
  final_script = generate_radio_script(prompt, llm_pipeline)
250
  st.session_state["final_script"] = final_script
251
  st.success("Promo script generated!")
 
295
  """
296
  <div class="footer-note">
297
  © 2025 Radio Imaging with Llama 3 – Built using Hugging Face & Streamlit. <br>
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