clementruhm commited on
Commit
7ad00dc
1 Parent(s): 4575e0b

app.py: Remove free tier

Browse files
Files changed (1) hide show
  1. app.py +20 -58
app.py CHANGED
@@ -1,7 +1,7 @@
1
  """
2
  Copyright 2023 Balacoon
3
 
4
- Voice Conversion service interactive demo
5
  """
6
 
7
  import glob
@@ -13,31 +13,9 @@ from typing import Dict
13
  import gradio as gr
14
  import soundfile as sf
15
 
16
- from vc_service_request import vc_service_request, prepare_audio
17
 
18
  script_dir = os.path.dirname(os.path.abspath(__file__))
19
- counter = 0
20
- # dict to limit max numer of usages per user without API key
21
- # clears up on demo restart, but thats ok for now
22
- users: Dict[str, int] = {}
23
- FREE_TIER = 5
24
-
25
- data, fs = sf.read(os.path.join(script_dir, "free_tier.wav"))
26
- audio_msg = prepare_audio((fs, data))
27
-
28
-
29
- def get_ip(request: gr.Request) -> str:
30
- """
31
- returns user IP from request
32
- as in https://huggingface.co/spaces/radames/gradio-request-get-client-ip/blob/main/app.py
33
- """
34
- client_ip = request.client.host
35
- local_ip = socket.gethostbyname(socket.gethostbyname(""))
36
- headers = request.kwargs['headers']
37
- if headers and 'x-forwarded-for' in headers:
38
- x_forwarded_for = headers['x-forwarded-for']
39
- client_ip = x_forwarded_for.split(' ')[0] if x_forwarded_for else ""
40
- return client_ip
41
 
42
 
43
  def main():
@@ -56,42 +34,38 @@ def main():
56
  with gr.Blocks() as demo:
57
  gr.Markdown(
58
  """
59
- <h1 align="center">Balacoon🦝 Voice Conversion</h1>
60
 
61
 
62
- Welcome to the live demo of Balacoon's Voice Conversion service.
63
- Check out our [website](https://balacoon.com/demo/#voice-conversion)
64
- to learn more.
65
- Voice Conversion allows you to transform your own voice
66
- into the voice of another person using just a single sample.
67
  For optimal results, we recommend using clean audio files in English.
68
 
69
  Here's how it works:
70
 
71
- 1. Provide your credentials (API key and secret) or leave fields blank to use free tier.
72
- 2. Begin by recording your voice.
73
  3. Select an audio sample that represents the target voice you want to convert to.
74
- 4. Click the "Convert" button and listen to the result!
75
 
76
  If providing your own audio files, please use WAVE PCM.
77
  Service works with 16kHz, 16 bit, mono audio.
78
-
79
- If you are interested to plug in Voice Conversion
80
- service into your own application, don't hesitate to get in touch with us at
81
- [contact@balacoon.com](mailto:contact@balacoon.com)
82
  """
83
  )
84
  gr.Markdown(badges)
85
  with gr.Row():
86
- apikey = gr.Textbox(label="API key", placeholder="Enter API key or leave empty for a free tier")
87
  with gr.Row():
88
- apisecret = gr.Textbox(label="API secret", placeholder="Enter API secret or leave empty for a free tier")
89
  with gr.Row():
90
  with gr.Column(variant="panel"):
91
  src_audio_mic = gr.Audio(source="microphone", label="Record your voice")
92
  src_audio_file = gr.Audio(
93
  source="upload", label="Or upload audio to convert"
94
  )
 
95
 
96
  with gr.Column(variant="panel"):
97
  tgt_audio_file = gr.Audio(
@@ -106,11 +80,11 @@ def main():
106
  )
107
 
108
  with gr.Row():
109
- convert_btn = gr.Button("Convert")
110
  with gr.Row():
111
  result_audio = gr.Audio()
112
 
113
- def voice_conversion(src_from_mic_, src_from_file_, tgt_from_file_, api_key_, api_secret_, request_: gr.Request):
114
  """
115
  helper function which checks where source come from
116
  """
@@ -119,29 +93,17 @@ def main():
119
  src_ = src_from_mic_
120
  elif src_from_file_:
121
  src_ = src_from_file_
 
 
122
  tgt_ = tgt_from_file_
123
  if not src_ or not tgt_:
124
  logging.warning("source or target are not provided")
125
  return
126
- global counter, users, audio_msg
127
- ip_ = get_ip(request_)
128
- past_requests_ = users.get(ip_, 0)
129
- free_tier_ = False
130
- if not api_key_ or not api_secret_:
131
- free_tier_ = True
132
- api_key_ = os.environ["api_key"]
133
- api_secret_ = os.environ["api_secret"]
134
- if free_tier_ and past_requests_ >= FREE_TIER:
135
- logging.info(f"Free tier exceeded for {ip_}")
136
- return 16000, audio_msg
137
- logging.info(f"Total number of requests: {counter}. {past_requests_} for {ip_}")
138
- counter += 1
139
- users[ip_] = past_requests_ + 1
140
- return vc_service_request(src_, tgt_, api_key_, api_secret_)
141
 
142
  convert_btn.click(
143
- voice_conversion,
144
- inputs=[src_audio_mic, src_audio_file, tgt_audio_file, apikey, apisecret],
145
  outputs=result_audio,
146
  )
147
 
 
1
  """
2
  Copyright 2023 Balacoon
3
 
4
+ Revoice Service interactive demo
5
  """
6
 
7
  import glob
 
13
  import gradio as gr
14
  import soundfile as sf
15
 
16
+ from service_request import service_request, prepare_audio
17
 
18
  script_dir = os.path.dirname(os.path.abspath(__file__))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
 
21
  def main():
 
34
  with gr.Blocks() as demo:
35
  gr.Markdown(
36
  """
37
+ <h1 align="center">Balacoon🦝 Revoice</h1>
38
 
39
 
40
+ Welcome to the live demo of Balacoon's Revoice service.
41
+ Check out our [website](https://balacoon.com/products/) to learn more.
42
+ Zero-shot speech generation allows to generate speech with any voice
43
+ given just a single sample as a reference.
 
44
  For optimal results, we recommend using clean audio files in English.
45
 
46
  Here's how it works:
47
 
48
+ 1. Provide your credentials (API key and secret).
49
+ 2. Recording or upload your voice for conversion, or provide text for synthesis.
50
  3. Select an audio sample that represents the target voice you want to convert to.
51
+ 4. Click the "Generate" button and listen to the result!
52
 
53
  If providing your own audio files, please use WAVE PCM.
54
  Service works with 16kHz, 16 bit, mono audio.
 
 
 
 
55
  """
56
  )
57
  gr.Markdown(badges)
58
  with gr.Row():
59
+ apikey = gr.Textbox(label="API key", placeholder="Enter API key")
60
  with gr.Row():
61
+ apisecret = gr.Textbox(label="API secret", placeholder="Enter API secret")
62
  with gr.Row():
63
  with gr.Column(variant="panel"):
64
  src_audio_mic = gr.Audio(source="microphone", label="Record your voice")
65
  src_audio_file = gr.Audio(
66
  source="upload", label="Or upload audio to convert"
67
  )
68
+ src_text = gr.Textbox(label="Text", placeholder="Or provide text to synthesize")
69
 
70
  with gr.Column(variant="panel"):
71
  tgt_audio_file = gr.Audio(
 
80
  )
81
 
82
  with gr.Row():
83
+ convert_btn = gr.Button("Generate")
84
  with gr.Row():
85
  result_audio = gr.Audio()
86
 
87
+ def speech_generation(src_from_mic_, src_from_file_, src_text_, tgt_from_file_, api_key_, api_secret_, request_: gr.Request):
88
  """
89
  helper function which checks where source come from
90
  """
 
93
  src_ = src_from_mic_
94
  elif src_from_file_:
95
  src_ = src_from_file_
96
+ elif src_text_:
97
+ src_ = src_text_
98
  tgt_ = tgt_from_file_
99
  if not src_ or not tgt_:
100
  logging.warning("source or target are not provided")
101
  return
102
+ return service_request(src_, tgt_, api_key_, api_secret_)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
  convert_btn.click(
105
+ speech_generation,
106
+ inputs=[src_audio_mic, src_audio_file, src_text, tgt_audio_file, apikey, apisecret],
107
  outputs=result_audio,
108
  )
109