Bils commited on
Commit
60b6e41
Β·
verified Β·
1 Parent(s): 0abc339

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -16
app.py CHANGED
@@ -11,9 +11,8 @@ from transformers import (
11
  from scipy.io.wavfile import write
12
  import tempfile
13
  from dotenv import load_dotenv
14
- import spaces # Assumes Hugging Face Spaces library supports `@spaces.GPU`
15
 
16
- # Load environment variables (e.g., Hugging Face token)
17
  load_dotenv()
18
  hf_token = os.getenv("HF_TOKEN")
19
 
@@ -21,7 +20,7 @@ hf_token = os.getenv("HF_TOKEN")
21
  # ---------------------------------------------------------------------
22
  # Load Llama 3 Pipeline with Zero GPU (Encapsulated)
23
  # ---------------------------------------------------------------------
24
- @spaces.GPU(duration=300) # Adjust GPU allocation duration
25
  def generate_script(user_prompt: str, model_id: str, token: str):
26
  try:
27
  tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=token)
@@ -35,9 +34,10 @@ def generate_script(user_prompt: str, model_id: str, token: str):
35
  llama_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer)
36
 
37
  system_prompt = (
38
- "You are a top-tier radio imaging producer using Llama 3. "
39
- "Take the user's concept and craft a short, creative promo script."
40
- )
 
41
  combined_prompt = f"{system_prompt}\nUser concept: {user_prompt}\nRefined script:"
42
  result = llama_pipeline(combined_prompt, max_new_tokens=200, do_sample=True, temperature=0.9)
43
  return result[0]["generated_text"].split("Refined script:")[-1].strip()
@@ -85,23 +85,63 @@ def interface_generate_audio(script, audio_length):
85
  # Interface
86
  # ---------------------------------------------------------------------
87
  with gr.Blocks() as demo:
88
- gr.Markdown("# 🎧 AI Radio Imaging with Llama 3 + MusicGen (Zero GPU)")
89
-
 
 
 
 
 
 
 
90
  with gr.Row():
91
  user_prompt = gr.Textbox(
92
- label="Enter your promo idea",
93
- placeholder="E.g., A 15-second hype jingle for a morning talk show.",
 
 
94
  )
95
  llama_model_id = gr.Textbox(
96
- label="Llama 3 Model ID", value="meta-llama/Meta-Llama-3-8B-Instruct"
 
 
97
  )
98
- audio_length = gr.Slider(label="Audio Length (tokens)", minimum=128, maximum=1024, step=64, value=512)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
- generate_script_button = gr.Button("Generate Script")
101
- script_output = gr.Textbox(label="Generated Script")
102
- generate_audio_button = gr.Button("Generate Audio")
103
- audio_output = gr.Audio(label="Generated Audio", type="filepath")
 
 
 
104
 
 
105
  generate_script_button.click(
106
  fn=interface_generate_script,
107
  inputs=[user_prompt, llama_model_id],
 
11
  from scipy.io.wavfile import write
12
  import tempfile
13
  from dotenv import load_dotenv
14
+ import spaces
15
 
 
16
  load_dotenv()
17
  hf_token = os.getenv("HF_TOKEN")
18
 
 
20
  # ---------------------------------------------------------------------
21
  # Load Llama 3 Pipeline with Zero GPU (Encapsulated)
22
  # ---------------------------------------------------------------------
23
+ @spaces.GPU(duration=300)
24
  def generate_script(user_prompt: str, model_id: str, token: str):
25
  try:
26
  tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=token)
 
34
  llama_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer)
35
 
36
  system_prompt = (
37
+ "You are an expert radio imaging producer specializing in sound design and music. "
38
+ "Take the user's concept and craft a concise, creative promo script with a strong focus on auditory elements and musical appeal."
39
+ )
40
+
41
  combined_prompt = f"{system_prompt}\nUser concept: {user_prompt}\nRefined script:"
42
  result = llama_pipeline(combined_prompt, max_new_tokens=200, do_sample=True, temperature=0.9)
43
  return result[0]["generated_text"].split("Refined script:")[-1].strip()
 
85
  # Interface
86
  # ---------------------------------------------------------------------
87
  with gr.Blocks() as demo:
88
+ # Header
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
+ """)
94
+
95
+ # Script Generation Section
96
+ gr.Markdown("## ✍️ Step 1: Generate Your Promo Script")
97
  with gr.Row():
98
  user_prompt = gr.Textbox(
99
+ label="🎀 Enter Promo Idea",
100
+ placeholder="E.g., A 15-second energetic jingle for a morning talk show.",
101
+ lines=2,
102
+ info="Describe your promo idea clearly to generate a creative script."
103
  )
104
  llama_model_id = gr.Textbox(
105
+ label="πŸŽ›οΈ Llama 3 Model ID",
106
+ value="meta-llama/Meta-Llama-3-8B-Instruct",
107
+ info="Enter the Hugging Face model ID for Llama 3."
108
  )
109
+ generate_script_button = gr.Button("Generate Script ✨")
110
+ script_output = gr.Textbox(
111
+ label="πŸ“œ Generated Promo Script",
112
+ lines=4,
113
+ interactive=False,
114
+ info="Your generated promo script will appear here."
115
+ )
116
+
117
+ # Audio Generation Section
118
+ gr.Markdown("## 🎧 Step 2: Generate Audio from Your Script")
119
+ with gr.Row():
120
+ audio_length = gr.Slider(
121
+ label="🎡 Audio Length (tokens)",
122
+ minimum=128,
123
+ maximum=1024,
124
+ step=64,
125
+ value=512,
126
+ info="Select the desired audio token length."
127
+ )
128
+ generate_audio_button = gr.Button("Generate Audio 🎢")
129
+ audio_output = gr.Audio(
130
+ label="🎢 Generated Audio File",
131
+ type="filepath",
132
+ interactive=False,
133
+ info="Listen to the audio file generated from your script."
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
+ </p>
142
+ """, elem_id="footer")
143
 
144
+ # Button Actions
145
  generate_script_button.click(
146
  fn=interface_generate_script,
147
  inputs=[user_prompt, llama_model_id],