poemsforaphrodite commited on
Commit
b4fcf8b
1 Parent(s): 27dd6b6

Update openvoice_app.py

Browse files
Files changed (1) hide show
  1. openvoice_app.py +49 -30
openvoice_app.py CHANGED
@@ -2,7 +2,10 @@ import os
2
  import torch
3
  import argparse
4
  import gradio as gr
5
- import requests
 
 
 
6
  from openvoice import se_extractor
7
  from openvoice.api import BaseSpeakerTTS, ToneColorConverter
8
  from dotenv import load_dotenv
@@ -36,24 +39,40 @@ os.makedirs(output_dir, exist_ok=True)
36
  api_key = os.environ.get("ELEVENLABS_API_KEY")
37
  supported_languages = ['zh', 'en']
38
 
39
- # Function to get all voices
40
- def get_voices(api_key):
41
- url = "https://api.elevenlabs.io/v1/voices"
42
- headers = {"xi-api-key": api_key}
43
- response = requests.request("GET", url, headers=headers)
44
- return response.json()
45
 
46
- # Function to delete a voice by ID
47
- def delete_voice(api_key, voice_id):
48
- url = f"https://api.elevenlabs.io/v1/voices/{voice_id}"
49
- headers = {"xi-api-key": api_key}
50
- response = requests.request("DELETE", url, headers=headers)
51
- return response.status_code, response.text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  # Predict function with rate limiting based on IP address
54
- #@limiter.limit("100 per minute")
55
- def predict(prompt, style, audio_file_pth, voice_name):
56
- text_hint = ''
57
  if len(prompt) < 2:
58
  text_hint += "[ERROR] Please provide a longer prompt text.\n"
59
  return text_hint, None, None
@@ -69,20 +88,16 @@ def predict(prompt, style, audio_file_pth, voice_name):
69
  )
70
  # Generate audio from text
71
  audio = client.generate(text=prompt, voice=voice)
72
- save(audio, f'{output_dir}/output.wav')
73
-
74
  save_path = f'{output_dir}/output.wav'
75
- data = get_voices(api_key)
76
- # Find all voice IDs with the name provided by the user
77
- trial_voice_ids = [voice.get("voice_id") for voice in data['voices'] if voice.get("name") == voice_name]
78
-
79
- # # Delete each voice with the name provided by the user
80
- # for voice_id in trial_voice_ids:
81
- # status_code, response_text = delete_voice(api_key, voice_id)
82
- # print(f"Deleted voice ID {voice_id}: Status Code {status_code}, Response {response_text}")
83
-
84
- # if not trial_voice_ids:
85
- # print("No voices with the name provided by the user found.")
86
 
87
  return text_hint, save_path, audio_file_pth
88
 
@@ -111,6 +126,10 @@ with gr.Blocks(gr.themes.Glass()) as demo:
111
  label="Your name and Product you bought",
112
  value="Sam"
113
  )
 
 
 
 
114
  tts_button = gr.Button("Start", elem_id="send-btn", visible=True)
115
 
116
  with gr.Column():
@@ -118,7 +137,7 @@ with gr.Blocks(gr.themes.Glass()) as demo:
118
  audio_gr = gr.Audio(label="Replicated Sound", autoplay=True)
119
  ref_audio_gr = gr.Audio(label="Original Audio Used ")
120
 
121
- tts_button.click(predict, [input_text_gr, style_gr, ref_gr, voice_name_gr], outputs=[out_text_gr, audio_gr, ref_audio_gr])
122
 
123
  demo.queue()
124
  demo.launch(debug=True, show_api=False, share=args.share)
 
2
  import torch
3
  import argparse
4
  import gradio as gr
5
+ import smtplib
6
+ from email.mime.text import MIMEText
7
+ from email.mime.multipart import MIMEMultipart
8
+ from email.mime.application import MIMEApplication
9
  from openvoice import se_extractor
10
  from openvoice.api import BaseSpeakerTTS, ToneColorConverter
11
  from dotenv import load_dotenv
 
39
  api_key = os.environ.get("ELEVENLABS_API_KEY")
40
  supported_languages = ['zh', 'en']
41
 
42
+ # Gmail configuration
43
+ GMAIL_USER = os.environ.get('GMAIL_USER')
44
+ GMAIL_PASSWORD = os.environ.get('GMAIL_PASSWORD') # This should be an app password, not your regular password
 
 
 
45
 
46
+ # Function to send email with downloadable file using Gmail SMTP
47
+ def send_email_with_file(recipient_email, file_path, subject, body):
48
+ try:
49
+ msg = MIMEMultipart()
50
+ msg['From'] = GMAIL_USER
51
+ msg['To'] = recipient_email
52
+ msg['Subject'] = subject
53
+
54
+ msg.attach(MIMEText(body, 'plain'))
55
+
56
+ with open(file_path, "rb") as file:
57
+ part = MIMEApplication(file.read(), Name=os.path.basename(file_path))
58
+ part['Content-Disposition'] = f'attachment; filename="{os.path.basename(file_path)}"'
59
+ msg.attach(part)
60
+
61
+ server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
62
+ server.ehlo()
63
+ server.login(GMAIL_USER, GMAIL_PASSWORD)
64
+ server.send_message(msg)
65
+ server.close()
66
+
67
+ return True
68
+ except Exception as e:
69
+ print(f"An error occurred while sending email: {e}")
70
+ return False
71
 
72
  # Predict function with rate limiting based on IP address
73
+ @limiter.limit("100 per minute")
74
+ def predict(prompt, style, audio_file_pth, voice_name, customer_email):
75
+ text_hint = 'Your file will only be saved for 24 hours.\n'
76
  if len(prompt) < 2:
77
  text_hint += "[ERROR] Please provide a longer prompt text.\n"
78
  return text_hint, None, None
 
88
  )
89
  # Generate audio from text
90
  audio = client.generate(text=prompt, voice=voice)
 
 
91
  save_path = f'{output_dir}/output.wav'
92
+ save(audio, save_path)
93
+
94
+ # Send email with downloadable file
95
+ subject = "Your Voice Clone File"
96
+ body = "Thank you for using our Voice Clone service. Your file is attached."
97
+ if send_email_with_file(customer_email, save_path, subject, body):
98
+ text_hint += "Email sent successfully with the voice file.\n"
99
+ else:
100
+ text_hint += "Failed to send email with the voice file. Please try again later.\n"
 
 
101
 
102
  return text_hint, save_path, audio_file_pth
103
 
 
126
  label="Your name and Product you bought",
127
  value="Sam"
128
  )
129
+ customer_email_gr = gr.Textbox(
130
+ label="Your Email",
131
+ info="We'll send you a downloadable file to this email address."
132
+ )
133
  tts_button = gr.Button("Start", elem_id="send-btn", visible=True)
134
 
135
  with gr.Column():
 
137
  audio_gr = gr.Audio(label="Replicated Sound", autoplay=True)
138
  ref_audio_gr = gr.Audio(label="Original Audio Used ")
139
 
140
+ 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])
141
 
142
  demo.queue()
143
  demo.launch(debug=True, show_api=False, share=args.share)