clementruhm commited on
Commit
8749790
·
1 Parent(s): 6c2c3a2

app.py: introduce free tier

Browse files
Files changed (1) hide show
  1. app.py +43 -6
app.py CHANGED
@@ -7,6 +7,8 @@ Voice Conversion service interactive demo
7
  import glob
8
  import logging
9
  import os
 
 
10
 
11
  import gradio as gr
12
 
@@ -14,6 +16,25 @@ from vc_service_request import vc_service_request
14
 
15
  script_dir = os.path.dirname(os.path.abspath(__file__))
16
  counter = 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  def main():
19
  logging.basicConfig(level=logging.INFO)
@@ -56,7 +77,10 @@ def main():
56
  """
57
  )
58
  gr.Markdown(badges)
59
-
 
 
 
60
  with gr.Row():
61
  with gr.Column(variant="panel"):
62
  src_audio_mic = gr.Audio(source="microphone", label="Record your voice")
@@ -81,7 +105,7 @@ def main():
81
  with gr.Row():
82
  result_audio = gr.Audio()
83
 
84
- def voice_conversion(src_from_mic_, src_from_file_, tgt_from_file_):
85
  """
86
  helper function which checks where source come from
87
  """
@@ -93,15 +117,28 @@ def main():
93
  tgt_ = tgt_from_file_
94
  if not src_ or not tgt_:
95
  logging.warning("source or target are not provided")
 
96
  return
97
- global counter
98
- logging.info(f"Making conversion request: [{counter}]")
 
 
 
 
 
 
 
 
 
 
 
99
  counter += 1
100
- return vc_service_request(src_, tgt_)
 
101
 
102
  convert_btn.click(
103
  voice_conversion,
104
- inputs=[src_audio_mic, src_audio_file, tgt_audio_file],
105
  outputs=result_audio,
106
  )
107
 
 
7
  import glob
8
  import logging
9
  import os
10
+ import socket
11
+ from typing import Dict
12
 
13
  import gradio as gr
14
 
 
16
 
17
  script_dir = os.path.dirname(os.path.abspath(__file__))
18
  counter = 0
19
+ # dict to limit max numer of usages per user without API key
20
+ # clears up on demo restart, but thats ok for now
21
+ users: Dict[str, int] = {}
22
+ FREE_TIER = 3
23
+
24
+
25
+ def get_ip(request: gr.Request) -> str:
26
+ """
27
+ returns user IP from request
28
+ as in https://huggingface.co/spaces/radames/gradio-request-get-client-ip/blob/main/app.py
29
+ """
30
+ client_ip = request.client.host
31
+ local_ip = socket.gethostbyname(socket.gethostbyname(""))
32
+ headers = request.kwargs['headers']
33
+ if headers and 'x-forwarded-for' in headers:
34
+ x_forwarded_for = headers['x-forwarded-for']
35
+ client_ip = x_forwarded_for.split(' ')[0] if x_forwarded_for else ""
36
+ return client_ip
37
+
38
 
39
  def main():
40
  logging.basicConfig(level=logging.INFO)
 
77
  """
78
  )
79
  gr.Markdown(badges)
80
+ with gr.Row():
81
+ apikey = gr.Textbox(label="API key", placeholder="Enter API key or leave empty for a free tier")
82
+ with gr.Row():
83
+ apisecret = gr.Textbox(label="API secret", placeholder="Enter API secret or leave empty for a free tier")
84
  with gr.Row():
85
  with gr.Column(variant="panel"):
86
  src_audio_mic = gr.Audio(source="microphone", label="Record your voice")
 
105
  with gr.Row():
106
  result_audio = gr.Audio()
107
 
108
+ def voice_conversion(src_from_mic_, src_from_file_, tgt_from_file_, api_key_, api_secret_, request_: gr.Request):
109
  """
110
  helper function which checks where source come from
111
  """
 
117
  tgt_ = tgt_from_file_
118
  if not src_ or not tgt_:
119
  logging.warning("source or target are not provided")
120
+ gr.Warning("Source or target audio are not provided")
121
  return
122
+ global counter, users
123
+ ip_ = get_ip(request_)
124
+ past_requests_ = users.get(ip_, 0)
125
+ free_tier_ = False
126
+ if not api_key_ or not api_secret_:
127
+ free_tier = True
128
+ api_key_ = os.environ["api_key"]
129
+ api_secret_ = os.environ["api_secret"]
130
+ if free_tier and past_requests_ >= FREE_TIER:
131
+ gr.Warning("Free tier exceeded, please reach out to get your API key")
132
+ logging.info(f"Free tier exceeded for {ip_}")
133
+ return None
134
+ logging.info(f"Total number of requests: {counter}. {past_requests_} for {ip_}")
135
  counter += 1
136
+ users[ip_] = past_requests_ + 1
137
+ return vc_service_request(src_, tgt_, api_key_, api_secret_)
138
 
139
  convert_btn.click(
140
  voice_conversion,
141
+ inputs=[src_audio_mic, src_audio_file, tgt_audio_file, apikey, apisecret],
142
  outputs=result_audio,
143
  )
144