File size: 5,953 Bytes
b72ab63
 
 
 
156d04e
02cd175
72ff919
 
160e4b0
e2aeb29
160e4b0
 
72ff919
b72ab63
02cd175
b72ab63
 
 
160e4b0
 
72ff919
4c58375
b72ab63
 
 
09274b3
b72ab63
160e4b0
156d04e
 
 
 
 
09274b3
156d04e
b4fcf8b
 
156d04e
 
 
 
 
 
 
 
 
 
 
b4fcf8b
156d04e
 
 
 
 
 
 
76c52c3
156d04e
 
 
 
 
 
 
 
 
 
 
 
76c52c3
156d04e
 
76c52c3
 
 
156d04e
 
76c52c3
156d04e
b4fcf8b
 
 
09274b3
e2aeb29
b4fcf8b
 
b72ab63
396e7de
 
b72ab63
396e7de
 
72ff919
 
 
821e73d
72ff919
 
 
160e4b0
72ff919
a65bab4
b4fcf8b
 
 
 
 
 
 
 
 
09274b3
396e7de
b72ab63
156d04e
 
02cd175
9c0d38a
b72ab63
 
 
0804336
b72ab63
e2aeb29
b72ab63
 
 
 
160e4b0
b72ab63
 
 
 
0804336
b72ab63
4c5de34
b72ab63
821e73d
47d598f
 
821e73d
b4fcf8b
 
 
 
4300ea8
b72ab63
 
 
85be729
57e1a6e
b72ab63
b4fcf8b
b72ab63
4c5de34
 
02cd175
4c5de34
 
 
 
 
 
76c52c3
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import os
import torch
import argparse
import gradio as gr
from mailersend import emails
from dotenv import load_dotenv
from openai import OpenAI
from elevenlabs.client import ElevenLabs
from elevenlabs import play, save
import time

# Load environment variables
load_dotenv()

# Argument parsing
parser = argparse.ArgumentParser()
parser.add_argument("--share", action='store_true', default=False, help="make link public")
args = parser.parse_args()

# Initialize ElevenLabs client
client = ElevenLabs(api_key=os.environ.get("ELEVENLABS_API_KEY"))
device = 'cuda' if torch.cuda.is_available() else 'cpu'
output_dir = 'outputs'
os.makedirs(output_dir, exist_ok=True)

api_key = os.environ.get("ELEVENLABS_API_KEY")
supported_languages = ['zh', 'en']

# MailerSend configuration
MAILERSEND_API_KEY = "mlsn.f2b1bdff316b16becadbd8dc5db50a31bb0ef084388d6dd0a8ecc9860e552474"
MAILERSEND_DOMAIN = "trial-x2p034709y9gzdrn.mlsender.net"
MAILERSEND_SENDER_EMAIL = f"noreply@{MAILERSEND_DOMAIN}"
MAILERSEND_SENDER_NAME = "Voice Clone App"  # You can change this to your preferred sender name

# Function to send email with downloadable file using MailerSend
def send_email_with_file(recipient_email, file_path, subject, body):
    try:
        mailer = emails.NewApiClient(MAILERSEND_API_KEY)

        from_email = MAILERSEND_SENDER_EMAIL
        from_name = MAILERSEND_SENDER_NAME
        to_email = recipient_email
        to_name = "Recipient"

        html = f"<p>{body}</p>"
        text = body

        # Prepare the attachment
        with open(file_path, "rb") as file:
            attachment_content = file.read()

        attachments = [
            {
                "filename": os.path.basename(file_path),
                "content": attachment_content,
                "disposition": "attachment"
            }
        ]

        # Send the email
        response = mailer.send(
            from_email=from_email,
            from_name=from_name,
            to_email=to_email,
            to_name=to_name,
            subject=subject,
            html=html,
            text=text,
            attachments=attachments
        )

        if response.http_status_code == 202:
            print("Email sent successfully")
            return True
        else:
            print(f"Failed to send email. Status code: {response.http_status_code}")
            print(f"Response: {response.json()}")
            return False

    except Exception as e:
        print(f"An error occurred while sending email: {e}")
        return False

# Predict function
def predict(prompt, style, audio_file_pth, voice_name, customer_email):
    text_hint = 'Your file will only be saved for 24 hours.\n'
    if len(prompt) < 2:
        text_hint += "[ERROR] Please provide a longer prompt text.\n"
        return text_hint, None, None
    if len(prompt) > 200:
        text_hint += "[ERROR] Text length limited to 200 characters. Please try shorter text.\n"
        return text_hint, None, None
    
    print(audio_file_pth)
    voice = client.clone(
        name=voice_name,
        description="A trial voice model for testing",
        files=[audio_file_pth],
    )
    # Generate audio from text
    audio = client.generate(text=prompt, voice=voice)
    save_path = f'{output_dir}/output.wav'
    save(audio, save_path)
    
    # Send email with downloadable file
    subject = "Your Voice Clone File"
    body = "Thank you for using our Voice Clone service. Your file is attached."
    if send_email_with_file(customer_email, save_path, subject, body):
        text_hint += "Email sent successfully with the voice file.\n"
    else:
        text_hint += "Failed to send email with the voice file. Please try again later.\n"

    return text_hint, save_path, audio_file_pth

# Gradio interface setup remains unchanged
# ...
# Gradio interface setup
with gr.Blocks(gr.themes.Glass()) as demo:
    with gr.Row():
        with gr.Column():
            input_text_gr = gr.Textbox(
                label="Create This",
                info="One or two sentences at a time is better. Up to 200 text characters.",
                value="He hoped there would be stew for dinner, turnips and carrots and bruised potatoes and fat mutton pieces to be ladled out in thick, peppered, flour-fattened sauce.",
            )
            style_gr = gr.Dropdown(
                label="Style",
                choices=['default', 'whispering', 'cheerful', 'terrified', 'angry', 'sad', 'friendly'],
                info="Please upload a reference audio file that is at least 1 minute long. For best results, ensure the audio is clear. You can use Adobe Podcast Enhance(https://podcast.adobe.com/enhance) to improve the audio quality before uploading.",
                max_choices=1,
                value="default",
            )
            ref_gr = gr.Audio(
                label="Original Audio",
                type="filepath",
                sources=["upload"],  # Allow only upload
            )
            voice_name_gr = gr.Textbox(
                label="Your name and Product you bought",
                value="Sam"
            )
            customer_email_gr = gr.Textbox(
                label="Your Email",
                info="We'll send you a downloadable file to this email address."
            )
            tts_button = gr.Button("Start", elem_id="send-btn", visible=True)

        with gr.Column():
            out_text_gr = gr.Text(label="Info")
            audio_gr = gr.Audio(label="Replicated Sound", autoplay=True)
            ref_audio_gr = gr.Audio(label="Original Audio Used ")

            tts_button.click(predict, [input_text_gr, style_gr, ref_gr, voice_name_gr, customer_email_gr], outputs=[out_text_gr, audio_gr, ref_audio_gr])

    demo.queue()
    demo.launch(debug=True, show_api=False, share=args.share)

# Hide Gradio footer and record button
css = """
footer {visibility: hidden}
audio .btn-container {display: none}
"""

demo.add_css(css)