Bils commited on
Commit
6f08234
·
verified ·
1 Parent(s): f747707

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -16
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import gradio as gr
3
  import os
4
  import torch
@@ -14,15 +13,13 @@ import tempfile
14
  from dotenv import load_dotenv
15
  import spaces
16
 
17
-
18
  load_dotenv()
19
  hf_token = os.getenv("HF_TOKEN")
20
 
21
-
22
  # ---------------------------------------------------------------------
23
  # Load Llama 3 Pipeline with Zero GPU (Encapsulated)
24
  # ---------------------------------------------------------------------
25
- @spaces.GPU(duration=300) # GPU allocation for 300 seconds
26
  def generate_script(user_prompt: str, model_id: str, token: str):
27
  try:
28
  tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=token)
@@ -50,7 +47,6 @@ def generate_script(user_prompt: str, model_id: str, token: str):
50
  # ---------------------------------------------------------------------
51
  # Load MusicGen Model (Encapsulated)
52
  # ---------------------------------------------------------------------
53
-
54
  @spaces.GPU(duration=300)
55
  def generate_audio(prompt: str, audio_length: int):
56
  try:
@@ -60,7 +56,7 @@ def generate_audio(prompt: str, audio_length: int):
60
  musicgen_model.to("cuda")
61
  inputs = musicgen_processor(text=[prompt], padding=True, return_tensors="pt")
62
  outputs = musicgen_model.generate(**inputs, max_new_tokens=audio_length)
63
- musicgen_model.to("cpu") # Return the model to CPU
64
 
65
  sr = musicgen_model.config.audio_encoder.sampling_rate
66
  audio_data = outputs[0, 0].cpu().numpy()
@@ -69,7 +65,7 @@ def generate_audio(prompt: str, audio_length: int):
69
  output_path = f"{tempfile.gettempdir()}/generated_audio.wav"
70
  write(output_path, sr, normalized_audio)
71
 
72
- return output_path
73
  except Exception as e:
74
  return f"Error generating audio: {e}"
75
 
@@ -93,7 +89,8 @@ with gr.Blocks() as demo:
93
  gr.Markdown("""
94
  # 🎙️ AI-Powered Radio Imaging Studio 🚀
95
  ### Create stunning **radio promos** with **Llama 3** and **MusicGen**
96
- 🔥 **Zero GPU** integration for efficiency and ease!
 
97
  """)
98
 
99
  # Script Generation Section
@@ -127,20 +124,21 @@ with gr.Blocks() as demo:
127
  maximum=1024,
128
  step=64,
129
  value=512,
130
- info="Select the desired audio token length."
 
 
 
 
 
 
131
  )
132
- audio_output = gr.Audio(
133
- label="🎶 Generated Audio File",
134
- type="filepath", # Correct parameter to handle file paths
135
- interactive=False
136
- )
137
-
138
 
139
  # Footer
140
  gr.Markdown("""
141
  <br><hr>
142
  <p style="text-align: center; font-size: 0.9em;">
143
- Created with ❤️ by <a href="https://bilsimaging.com" target="_blank">bilsimaging.com</a>
 
144
  </p>
145
  """, elem_id="footer")
146
 
 
 
1
  import gradio as gr
2
  import os
3
  import torch
 
13
  from dotenv import load_dotenv
14
  import spaces
15
 
 
16
  load_dotenv()
17
  hf_token = os.getenv("HF_TOKEN")
18
 
 
19
  # ---------------------------------------------------------------------
20
  # Load Llama 3 Pipeline with Zero GPU (Encapsulated)
21
  # ---------------------------------------------------------------------
22
+ @spaces.GPU(duration=300)
23
  def generate_script(user_prompt: str, model_id: str, token: str):
24
  try:
25
  tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=token)
 
47
  # ---------------------------------------------------------------------
48
  # Load MusicGen Model (Encapsulated)
49
  # ---------------------------------------------------------------------
 
50
  @spaces.GPU(duration=300)
51
  def generate_audio(prompt: str, audio_length: int):
52
  try:
 
56
  musicgen_model.to("cuda")
57
  inputs = musicgen_processor(text=[prompt], padding=True, return_tensors="pt")
58
  outputs = musicgen_model.generate(**inputs, max_new_tokens=audio_length)
59
+ musicgen_model.to("cpu")
60
 
61
  sr = musicgen_model.config.audio_encoder.sampling_rate
62
  audio_data = outputs[0, 0].cpu().numpy()
 
65
  output_path = f"{tempfile.gettempdir()}/generated_audio.wav"
66
  write(output_path, sr, normalized_audio)
67
 
68
+ return output_path
69
  except Exception as e:
70
  return f"Error generating audio: {e}"
71
 
 
89
  gr.Markdown("""
90
  # 🎙️ AI-Powered Radio Imaging Studio 🚀
91
  ### Create stunning **radio promos** with **Llama 3** and **MusicGen**
92
+ 🔥 **Zero GPU** integration for efficiency and ease!
93
+ ❤️ A huge thanks to the **Hugging Face community** for making this possible.
94
  """)
95
 
96
  # Script Generation Section
 
124
  maximum=1024,
125
  step=64,
126
  value=512,
127
+ info="Select the desired audio token length."
128
+ )
129
+ generate_audio_button = gr.Button("Generate Audio 🎶")
130
+ audio_output = gr.Audio(
131
+ label="🎶 Generated Audio File",
132
+ type="filepath",
133
+ interactive=False
134
  )
 
 
 
 
 
 
135
 
136
  # Footer
137
  gr.Markdown("""
138
  <br><hr>
139
  <p style="text-align: center; font-size: 0.9em;">
140
+ Created with ❤️ by <a href="https://bilsimaging.com" target="_blank">bilsimaging.com</a>
141
+ Special thanks to the <strong>Hugging Face community</strong> for their incredible support and tools!
142
  </p>
143
  """, elem_id="footer")
144