Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import torch
|
|
@@ -11,7 +12,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_dotenv()
|
| 17 |
hf_token = os.getenv("HF_TOKEN")
|
|
@@ -20,7 +22,7 @@ hf_token = os.getenv("HF_TOKEN")
|
|
| 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,9 +36,9 @@ def generate_script(user_prompt: str, model_id: str, token: str):
|
|
| 34 |
llama_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 35 |
|
| 36 |
system_prompt = (
|
| 37 |
-
|
| 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)
|
|
@@ -71,7 +73,7 @@ def generate_audio(prompt: str, audio_length: int):
|
|
| 71 |
|
| 72 |
|
| 73 |
# ---------------------------------------------------------------------
|
| 74 |
-
# Gradio Interface
|
| 75 |
# ---------------------------------------------------------------------
|
| 76 |
def interface_generate_script(user_prompt, llama_model_id):
|
| 77 |
return generate_script(user_prompt, llama_model_id, hf_token)
|
|
@@ -115,23 +117,22 @@ with gr.Blocks() as demo:
|
|
| 115 |
)
|
| 116 |
|
| 117 |
# Audio Generation Section
|
| 118 |
-
gr.Markdown("## 🎧 Step 2: Generate Audio from Your Script")
|
| 119 |
-
with gr.Row():
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
)
|
| 128 |
-
generate_audio_button = gr.Button("Generate Audio 🎶")
|
| 129 |
-
audio_output = gr.Audio(
|
| 130 |
-
label="🎶 Generated Audio File",
|
| 131 |
-
type="filepath", # Removed the `info` argument
|
| 132 |
-
interactive=False
|
| 133 |
-
)
|
| 134 |
-
|
| 135 |
|
| 136 |
# Footer
|
| 137 |
gr.Markdown("""
|
|
|
|
| 1 |
+
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
import torch
|
|
|
|
| 12 |
from scipy.io.wavfile import write
|
| 13 |
import tempfile
|
| 14 |
from dotenv import load_dotenv
|
| 15 |
+
import spaces
|
| 16 |
+
|
| 17 |
|
| 18 |
load_dotenv()
|
| 19 |
hf_token = os.getenv("HF_TOKEN")
|
|
|
|
| 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)
|
|
|
|
| 36 |
llama_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 37 |
|
| 38 |
system_prompt = (
|
| 39 |
+
"You are an expert radio imaging producer specializing in sound design and music. "
|
| 40 |
+
"Take the user's concept and craft a concise, creative promo script with a strong focus on auditory elements and musical appeal."
|
| 41 |
+
)
|
| 42 |
|
| 43 |
combined_prompt = f"{system_prompt}\nUser concept: {user_prompt}\nRefined script:"
|
| 44 |
result = llama_pipeline(combined_prompt, max_new_tokens=200, do_sample=True, temperature=0.9)
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
# ---------------------------------------------------------------------
|
| 76 |
+
# Gradio Interface Functions
|
| 77 |
# ---------------------------------------------------------------------
|
| 78 |
def interface_generate_script(user_prompt, llama_model_id):
|
| 79 |
return generate_script(user_prompt, llama_model_id, hf_token)
|
|
|
|
| 117 |
)
|
| 118 |
|
| 119 |
# Audio Generation Section
|
| 120 |
+
gr.Markdown("## 🎧 Step 2: Generate Audio from Your Script")
|
| 121 |
+
with gr.Row():
|
| 122 |
+
audio_length = gr.Slider(
|
| 123 |
+
label="🎵 Audio Length (tokens)",
|
| 124 |
+
minimum=128,
|
| 125 |
+
maximum=1024,
|
| 126 |
+
step=64,
|
| 127 |
+
value=512,
|
| 128 |
+
info="Select the desired audio token length."
|
| 129 |
+
)
|
| 130 |
+
generate_audio_button = gr.Button("Generate Audio 🎶")
|
| 131 |
+
audio_output = gr.Audio(
|
| 132 |
+
label="🎶 Generated Audio File",
|
| 133 |
+
type="filepath",
|
| 134 |
+
interactive=False
|
| 135 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
# Footer
|
| 138 |
gr.Markdown("""
|