Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files68 unmodified lines6970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315468 unmodified linesreturn np.array([])@GPUdef generate_podcast(self, num_speakers: int, script: str,speaker_1: str = None, speaker_2: str = None,speaker_3: str = None, speaker_4: str = None,cfg_scale: float = 1.3):"""Final audio generation only (no streaming)."""self.is_generating = Trueif not script.strip():raise gr.Error("Please provide a script.")if num_speakers < 1 or num_speakers > 4:raise gr.Error("Number of speakers must be 1β4.")selected = [speaker_1, speaker_2, speaker_3, speaker_4][:num_speakers]for i, sp in enumerate(selected):if not sp or sp not in self.available_voices:raise gr.Error(f"Invalid speaker {i+1} selection.")# load voicesvoice_samples = [self.read_audio(self.available_voices[sp]) for sp in selected]if any(len(v) == 0 for v in voice_samples):raise gr.Error("Failed to load one or more voice samples.")# format scriptlines = script.strip().split("\n")formatted = []for i, line in enumerate(lines):line = line.strip()if not line:continueif line.startswith("Speaker "):formatted.append(line)else:sp_id = i % num_speakersformatted.append(f"Speaker {sp_id}: {line}")formatted_script = "\n".join(formatted)# processor inputinputs = self.processor(text=[formatted_script],voice_samples=[voice_samples],padding=True,return_tensors="pt",return_attention_mask=True,)start = time.time()outputs = self.model.generate(**inputs,max_new_tokens=None,cfg_scale=cfg_scale,tokenizer=self.processor.tokenizer,generation_config={'do_sample': False},verbose=False,)# --- FIX: pull from speech_outputs ---if outputs.speech_outputs and outputs.speech_outputs[0] is not None:audio = outputs.speech_outputs[0].cpu().numpy()else:self.is_generating = Falseraise gr.Error("β No audio was generated by the model.")if audio.ndim > 1:audio = audio.squeeze()sample_rate = 24000# Save automatically to diskos.makedirs("outputs", exist_ok=True)timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")file_path = os.path.join("outputs", f"podcast_{timestamp}.wav")sf.write(file_path, audio, sample_rate)print(f"πΎ Saved podcast to {file_path}")total_dur = len(audio) / sample_ratelog = f"β Generation complete in {time.time()-start:.1f}s, {total_dur:.1f}s audio\nSaved to {file_path}"self.is_generating = Falsereturn (sample_rate, audio), log68 unmodified lines6970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521668 unmodified linesreturn np.array([])@GPUdef generate_podcast(self,num_speakers: int,script: str,speaker_1: str = None,speaker_2: str = None,speaker_3: str = None,speaker_4: str = None,cfg_scale: float = 1.3):"""Generates a podcast as a single audio file from a script and saves it.This is a non-streaming function."""try:# 1. Set generating state and validate inputsself.is_generating = Trueif not script.strip():raise gr.Error("Error: Please provide a script.")# Defend against common mistake with apostrophesscript = script.replace("β", "'")if not 1 <= num_speakers <= 4:raise gr.Error("Error: Number of speakers must be between 1 and 4.")# 2. Collect and validate selected speakersselected_speakers = [speaker_1, speaker_2, speaker_3, speaker_4][:num_speakers]for i, speaker_name in enumerate(selected_speakers):if not speaker_name or speaker_name not in self.available_voices:raise gr.Error(f"Error: Please select a valid speaker for Speaker {i+1}.")# 3. Build initial loglog = f"ποΈ Generating podcast with {num_speakers} speakers\n"log += f"π Parameters: CFG Scale={cfg_scale}\n"log += f"π Speakers: {', '.join(selected_speakers)}\n"# 4. Load voice samplesvoice_samples = []for speaker_name in selected_speakers:audio_path = self.available_voices[speaker_name]# Assuming self.read_audio is a method in your class that returns audio dataaudio_data = self.read_audio(audio_path)if len(audio_data) == 0:raise gr.Error(f"Error: Failed to load audio for {speaker_name}")voice_samples.append(audio_data)log += f"β Loaded {len(voice_samples)} voice samples\n"# 5. Parse and format the scriptlines = script.strip().split('\n')formatted_script_lines = []for line in lines:line = line.strip()if not line:continue# Check if line already has speaker format (e.g., "Speaker 1: ...")if line.startswith('Speaker ') and ':' in line:formatted_script_lines.append(line)else:# Auto-assign speakers in rotationspeaker_id = len(formatted_script_lines) % num_speakersformatted_script_lines.append(f"Speaker {speaker_id}: {line}")formatted_script = '\n'.join(formatted_script_lines)log += f"π Formatted script with {len(formatted_script_lines)} turns\n"log += "π Processing with VibeVoice...\n"# 6. Prepare inputs for the model# Assuming self.processor is an object available in your classinputs = self.processor(text=[formatted_script],voice_samples=[voice_samples],padding=True,return_tensors="pt",return_attention_mask=True,)# 7. Generate audiostart_time = time.time()# Assuming self.model is an object available in your classoutputs = self.model.generate(**inputs,max_new_tokens=None,cfg_scale=cfg_scale,tokenizer=self.processor.tokenizer,generation_config={'do_sample': False},verbose=False, # Verbose is off for cleaner logs)generation_time = time.time() - start_time# 8. Extract audio output# The generated audio is often in speech_outputs or a similar attributeif hasattr(outputs, 'speech_outputs') and outputs.speech_outputs[0] is not None:audio_tensor = outputs.speech_outputs[0]audio = audio_tensor.cpu().numpy()else:raise gr.Error("β Error: No audio was generated by the model. Please try again.")# Ensure audio is a 1D arrayif audio.ndim > 1:audio = audio.squeeze()sample_rate = 24000 # Standard sample rate for this model# 9. Save the audio fileoutput_dir = "outputs"os.makedirs(output_dir, exist_ok=True)timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")file_path = os.path.join(output_dir, f"podcast_{timestamp}.wav")# Write the NumPy array to a WAV filesf.write(file_path, audio, sample_rate)print(f"πΎ Podcast saved to {file_path}")# 10. Finalize log and returntotal_duration = len(audio) / sample_ratelog += f"β±οΈ Generation completed in {generation_time:.2f} seconds\n"log += f"π΅ Final audio duration: {total_duration:.2f} seconds\n"log += f"β Successfully saved podcast to: {file_path}\n"self.is_generating = Falsereturn (sample_rate, audio), logexcept gr.Error as e:# Handle Gradio-specific errors (for user feedback)self.is_generating = Falseerror_msg = f"β Input Error: {str(e)}"print(error_msg)# In Gradio, you would typically return an update to the UI# For a pure function, we re-raise or handle it as needed.# This return signature matches the success case but with error info.return None, error_msgexcept Exception as e:# Handle all other unexpected errorsself.is_generating = Falseerror_msg = f"β An unexpected error occurred: {str(e)}"print(error_msg)import tracebacktraceback.print_exc()return None, error_msg
app.py+131-69