File size: 3,459 Bytes
dc356a4
d388d3c
a695b96
 
dc356a4
a695b96
 
d388d3c
a695b96
 
 
 
 
 
 
d388d3c
a695b96
 
 
 
 
 
 
 
 
 
 
 
 
 
d388d3c
a695b96
 
d388d3c
a695b96
d388d3c
a695b96
 
dc356a4
a695b96
 
0e33331
2f4e236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b5eb6d
2f4e236
6be0923
 
 
 
 
 
b0371fe
dc356a4
b0371fe
 
 
 
 
 
 
 
6be0923
 
b0371fe
 
 
 
dc356a4
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import gradio as gr
import pandas as pd
import os
import random

# Path to voice actors folder
voice_actors_folder = "tortoise/voices/"

# Get the list of voice actors
voice_actors = [va for va in os.listdir(voice_actors_folder) if va != "cond_latent_example" and va != ".DS_Store"]
male_voice_actors = [va for va in voice_actors if va.endswith(".M")]
female_voice_actors = [va for va in voice_actors if va.endswith(".F")]

# Function to extract unique speakers from CSV
def extract_unique_speakers(csv_file):
    df = pd.read_csv(csv_file)
    # Extract unique speakers from "Speaker" column
    unique_speakers = df["Speaker"].unique().tolist()
    return unique_speakers

# Function to get a random default voice actor for each speaker
def get_random_voice_for_speaker_fast(speaker):
    selected_voice_actors = voice_actors  # default to all voice actors

    if speaker.endswith(".M") and male_voice_actors: 
        selected_voice_actors = male_voice_actors
    elif speaker.endswith(".F") and female_voice_actors:
        selected_voice_actors = female_voice_actors
    elif speaker.endswith(".?"):
        selected_voice_actors = male_voice_actors + female_voice_actors
    
    if not selected_voice_actors:  # If list is empty, default to all voice actors
        selected_voice_actors = male_voice_actors + female_voice_actors
    
    return random.choice(selected_voice_actors)

# Extract speakers from the CSV file
speakers = extract_unique_speakers("book.csv")

# Dictionary to store selected voice actors for each speaker
selected_voices = {speaker: get_random_voice_for_speaker_fast(speaker) for speaker in speakers}

# Function to find and return a sample audio file for the selected voice actor
def get_voice_sample(voice_actor):
    voice_actor_path = os.path.join(voice_actors_folder, voice_actor)
    # Look for any .wav or .mp3 files in the voice actor's folder
    for file in os.listdir(voice_actor_path):
        if file.endswith(".wav") or file.endswith(".mp3"):
            return os.path.join(voice_actor_path, file)
    return None

# Function to return the voice sample file for a given speaker
def play_sample(speaker):
    selected_voice = selected_voices[speaker]
    sample_file = get_voice_sample(selected_voice)
    if sample_file:
        return sample_file
    else:
        return None

# Function to update only the voice for a specific speaker and return the updated audio preview
def update_voice_and_audio(speaker, selected_voice):
    selected_voices[speaker] = selected_voice
    audio_sample = play_sample(speaker)
    return audio_sample

# Creating Gradio interface using Blocks
def create_interface():
    with gr.Blocks() as demo:
        for speaker in speakers:
            with gr.Row():
                dropdown = gr.Dropdown(choices=voice_actors, label=f"Select voice actor for {speaker}", value=selected_voices[speaker])
                audio = gr.Audio(label=f"Audio preview for {speaker}", type="filepath", value=play_sample(speaker))

                # Function to update audio when a new voice actor is selected
                def update_audio(selected_voice, speaker=speaker):
                    return update_voice_and_audio(speaker, selected_voice)

                # Connect the dropdown change to the audio preview update
                dropdown.change(fn=update_audio, inputs=dropdown, outputs=audio)

    return demo

app = create_interface()

if __name__ == "__main__":
    app.launch()