Bils commited on
Commit
66b1260
·
verified ·
1 Parent(s): bf027de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -70
app.py CHANGED
@@ -61,29 +61,18 @@ def generate_script(user_prompt: str, model_id: str, token: str, duration: int):
61
  return f"Error generating script: {e}", "", ""
62
 
63
  # ---------------------------------------------------------------------
64
- # Voice-Over Generation Function
65
  # ---------------------------------------------------------------------
66
  @spaces.GPU(duration=300)
67
  def generate_voice(script: str, speaker: str = "default"):
68
  try:
69
- # Load TTS model
70
- tts_model_path = "tts_models/en/ljspeech/tacotron2-DDC"
71
- vocoder_model_path = "vocoder_models/en/ljspeech/hifigan_v2"
72
- synthesizer = Synthesizer(tts_model_path, vocoder_model_path)
73
-
74
- # Generate audio
75
- wav = synthesizer.tts(script)
76
-
77
- # Save output to a file
78
- output_path = f"{tempfile.gettempdir()}/generated_voice.wav"
79
- synthesizer.save_wav(wav, output_path)
80
-
81
- return output_path
82
  except Exception as e:
83
- return f"Error generating voice-over: {e}"
84
 
85
  # ---------------------------------------------------------------------
86
- # Music Generation Function (using facebook/musicgen-medium)
87
  # ---------------------------------------------------------------------
88
  @spaces.GPU(duration=300)
89
  def generate_music(prompt: str, audio_length: int):
@@ -117,23 +106,15 @@ def generate_music(prompt: str, audio_length: int):
117
 
118
 
119
  # ---------------------------------------------------------------------
120
- # Audio Blending Function with Ducking
121
  # ---------------------------------------------------------------------
122
  def blend_audio(voice_path: str, music_path: str, ducking: bool):
123
  try:
124
- voice = AudioSegment.from_file(voice_path)
125
- music = AudioSegment.from_file(music_path)
126
-
127
- if ducking:
128
- music = music - 10 # Lower music volume for ducking
129
-
130
- combined = music.overlay(voice)
131
- output_path = f"{tempfile.gettempdir()}/final_promo.wav"
132
- combined.export(output_path, format="wav")
133
-
134
- return output_path
135
  except Exception as e:
136
- return f"Error blending audio: {e}"
 
137
 
138
  # ---------------------------------------------------------------------
139
  # Gradio Interface
@@ -145,23 +126,23 @@ with gr.Blocks() as demo:
145
  """)
146
 
147
  with gr.Tabs():
148
- # Step 1: Generate Script
149
- with gr.Tab("Step 1: Generate Script"):
150
- with gr.Row():
151
- user_prompt = gr.Textbox(label="Promo Idea", placeholder="E.g., A 30-second promo for a morning show.")
152
- llama_model_id = gr.Textbox(label="Llama Model ID", value="meta-llama/Meta-Llama-3-8B-Instruct")
153
- duration = gr.Slider(label="Duration (seconds)", minimum=15, maximum=60, step=15, value=30)
154
-
155
- generate_script_button = gr.Button("Generate Script")
156
- script_output = gr.Textbox(label="Generated Script", lines=5)
157
- sound_design_output = gr.Textbox(label="Sound Design", lines=3)
158
- music_suggestion_output = gr.Textbox(label="Music Suggestions", lines=3)
159
-
160
- generate_script_button.click(
161
- fn=lambda user_prompt, model_id, duration: generate_script(user_prompt, model_id, hf_token, duration),
162
- inputs=[user_prompt, llama_model_id, duration],
163
- outputs=[script_output, sound_design_output, music_suggestion_output],
164
- )
165
 
166
  # Step 2: Generate Voice
167
  with gr.Tab("Step 2: Generate Voice"):
@@ -178,30 +159,19 @@ with gr.Blocks() as demo:
178
  )
179
 
180
  # Step 3: Generate Music
181
- with gr.Tab("Step 3: Generate Music"):
182
- with gr.Row():
183
- audio_length = gr.Slider(label="Music Length (tokens)", minimum=128, maximum=1024, step=64, value=512)
184
- music_prompt_display = gr.Textbox(
185
- label="Music Suggestions from Step 1",
186
- lines=3,
187
- interactive=False
188
- )
189
-
190
- generate_music_button = gr.Button("Generate Music")
191
- music_output = gr.Audio(label="Generated Music", type="filepath")
192
-
193
- # Pass music suggestions from Step 1 to Step 3
194
- generate_script_button.click(
195
- fn=lambda _, __, ___: None, # Dummy function to update the display
196
- inputs=[],
197
- outputs=[music_suggestion_output],
198
- )
199
-
200
- generate_music_button.click(
201
- fn=lambda prompt, audio_length: generate_music(prompt, audio_length),
202
- inputs=[music_suggestion_output, audio_length],
203
- outputs=[music_output],
204
- )
205
 
206
  # Step 4: Blend Audio
207
  with gr.Tab("Step 4: Blend Audio"):
 
61
  return f"Error generating script: {e}", "", ""
62
 
63
  # ---------------------------------------------------------------------
64
+ # Voice-Over Generation Function (Inactive)
65
  # ---------------------------------------------------------------------
66
  @spaces.GPU(duration=300)
67
  def generate_voice(script: str, speaker: str = "default"):
68
  try:
69
+ # Placeholder for inactive state
70
+ return "Voice-over generation is currently inactive."
 
 
 
 
 
 
 
 
 
 
 
71
  except Exception as e:
72
+ return f"Error: {e}"
73
 
74
  # ---------------------------------------------------------------------
75
+ # Music Generation Function (facebook/musicgen-medium)
76
  # ---------------------------------------------------------------------
77
  @spaces.GPU(duration=300)
78
  def generate_music(prompt: str, audio_length: int):
 
106
 
107
 
108
  # ---------------------------------------------------------------------
109
+ # Audio Blending Function with Ducking (Inactive)
110
  # ---------------------------------------------------------------------
111
  def blend_audio(voice_path: str, music_path: str, ducking: bool):
112
  try:
113
+ # Placeholder for inactive state
114
+ return "Audio blending functionality is currently inactive."
 
 
 
 
 
 
 
 
 
115
  except Exception as e:
116
+ return f"Error: {e}"
117
+
118
 
119
  # ---------------------------------------------------------------------
120
  # Gradio Interface
 
126
  """)
127
 
128
  with gr.Tabs():
129
+ # Step 1: Generate Script
130
+ with gr.Tab("Step 1: Generate Script"):
131
+ with gr.Row():
132
+ user_prompt = gr.Textbox(label="Promo Idea", placeholder="E.g., A 30-second promo for a morning show.")
133
+ llama_model_id = gr.Textbox(label="Llama Model ID", value="meta-llama/Meta-Llama-3-8B-Instruct")
134
+ duration = gr.Slider(label="Duration (seconds)", minimum=15, maximum=60, step=15, value=30)
135
+
136
+ generate_script_button = gr.Button("Generate Script")
137
+ script_output = gr.Textbox(label="Generated Voice-Over Script", lines=5)
138
+ sound_design_output = gr.Textbox(label="Sound Design Suggestions", lines=3)
139
+ music_suggestion_output = gr.Textbox(label="Music Suggestions", lines=3)
140
+
141
+ generate_script_button.click(
142
+ fn=lambda user_prompt, model_id, duration: generate_script(user_prompt, model_id, hf_token, duration),
143
+ inputs=[user_prompt, llama_model_id, duration],
144
+ outputs=[script_output, sound_design_output, music_suggestion_output],
145
+ )
146
 
147
  # Step 2: Generate Voice
148
  with gr.Tab("Step 2: Generate Voice"):
 
159
  )
160
 
161
  # Step 3: Generate Music
162
+ with gr.Tab("Step 3: Generate Music"):
163
+ with gr.Row():
164
+ audio_length = gr.Slider(label="Music Length (tokens)", minimum=128, maximum=1024, step=64, value=512)
165
+
166
+ generate_music_button = gr.Button("Generate Music")
167
+ music_output = gr.Audio(label="Generated Music", type="filepath")
168
+
169
+ # Pass music suggestions from Step 1 to Step 3
170
+ generate_music_button.click(
171
+ fn=lambda music_suggestion, audio_length: generate_music(music_suggestion, audio_length),
172
+ inputs=[music_suggestion_output, audio_length],
173
+ outputs=[music_output],
174
+ )
 
 
 
 
 
 
 
 
 
 
 
175
 
176
  # Step 4: Blend Audio
177
  with gr.Tab("Step 4: Blend Audio"):