Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 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)
|
| 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 |
-
|
| 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,23 +85,63 @@ def interface_generate_audio(script, audio_length):
|
|
| 85 |
# Interface
|
| 86 |
# ---------------------------------------------------------------------
|
| 87 |
with gr.Blocks() as demo:
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
with gr.Row():
|
| 91 |
user_prompt = gr.Textbox(
|
| 92 |
-
label="Enter
|
| 93 |
-
placeholder="E.g., A 15-second
|
|
|
|
|
|
|
| 94 |
)
|
| 95 |
llama_model_id = gr.Textbox(
|
| 96 |
-
label="Llama 3 Model ID",
|
|
|
|
|
|
|
| 97 |
)
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
| 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],
|