asv7j commited on
Commit
90397d7
1 Parent(s): 2026913

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -216
app.py CHANGED
@@ -1,226 +1,25 @@
1
- import argparse
2
- import operator
3
- import sys
4
-
5
  from libretranslate.app import create_app
6
  from libretranslate.default_values import DEFAULT_ARGUMENTS as DEFARGS
 
7
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- def get_args():
10
- parser = argparse.ArgumentParser(
11
- description="LibreTranslate - Free and Open Source Translation API"
12
- )
13
- parser.add_argument(
14
- "--host", type=str, help="Hostname (%(default)s)", default=DEFARGS['HOST']
15
- )
16
- parser.add_argument("--port", type=int, help="Port (%(default)s)", default=DEFARGS['PORT'])
17
- parser.add_argument(
18
- "--char-limit",
19
- default=DEFARGS['CHAR_LIMIT'],
20
- type=int,
21
- metavar="<number of characters>",
22
- help="Set character limit (%(default)s)",
23
- )
24
- parser.add_argument(
25
- "--req-limit",
26
- default=DEFARGS['REQ_LIMIT'],
27
- type=int,
28
- metavar="<number>",
29
- help="Set the default maximum number of requests per minute per client (%(default)s)",
30
- )
31
- parser.add_argument(
32
- "--req-limit-storage",
33
- default=DEFARGS['REQ_LIMIT_STORAGE'],
34
- type=str,
35
- metavar="<Storage URI>",
36
- help="Storage URI to use for request limit data storage. See https://flask-limiter.readthedocs.io/en/stable/configuration.html. (%(default)s)",
37
- )
38
- parser.add_argument(
39
- "--hourly-req-limit",
40
- default=DEFARGS['HOURLY_REQ_LIMIT'],
41
- type=int,
42
- metavar="<number>",
43
- help="Set the default maximum number of requests per hour per client, in addition to req-limit. (%(default)s)",
44
- )
45
- parser.add_argument(
46
- "--hourly-req-limit-decay",
47
- default=DEFARGS['HOURLY_REQ_LIMIT_DECAY'],
48
- type=int,
49
- metavar="<number>",
50
- help="When used in combination with hourly-req-limit, adds additional hourly restrictions that logaritmically decrease for each additional hour. (%(default)s)",
51
- )
52
- parser.add_argument(
53
- "--daily-req-limit",
54
- default=DEFARGS['DAILY_REQ_LIMIT'],
55
- type=int,
56
- metavar="<number>",
57
- help="Set the default maximum number of requests per day per client, in addition to req-limit. (%(default)s)",
58
- )
59
- parser.add_argument(
60
- "--req-flood-threshold",
61
- default=DEFARGS['REQ_FLOOD_THRESHOLD'],
62
- type=int,
63
- metavar="<number>",
64
- help="Set the maximum number of request limit offences that a client can exceed before being banned. (%(default)s)",
65
- )
66
- parser.add_argument(
67
- "--req-time-cost",
68
- default=DEFARGS['REQ_TIME_COST'],
69
- type=int,
70
- metavar="<number>",
71
- help="Considers a time cost (in seconds) for request limiting purposes. If a request takes 10 seconds and this value is set to 5, the request cost is either 2 or the actual request cost (whichever is greater). (%(default)s)",
72
- )
73
- parser.add_argument(
74
- "--batch-limit",
75
- default=DEFARGS['BATCH_LIMIT'],
76
- type=int,
77
- metavar="<number of texts>",
78
- help="Set maximum number of texts to translate in a batch request (%(default)s)",
79
- )
80
- parser.add_argument(
81
- "--ga-id",
82
- type=str,
83
- default=DEFARGS['GA_ID'],
84
- metavar="<GA ID>",
85
- help="Enable Google Analytics on the API client page by providing an ID (%(default)s)",
86
- )
87
- parser.add_argument(
88
- "--debug", default=DEFARGS['DEBUG'], action="store_true", help="Enable debug environment"
89
- )
90
- parser.add_argument(
91
- "--ssl", default=DEFARGS['SSL'], action="store_true", help="Whether to enable SSL"
92
- )
93
- parser.add_argument(
94
- "--frontend-language-source",
95
- type=str,
96
- default=DEFARGS['FRONTEND_LANGUAGE_SOURCE'],
97
- metavar="<language code>",
98
- help="Set frontend default language - source (%(default)s)",
99
- )
100
- parser.add_argument(
101
- "--frontend-language-target",
102
- type=str,
103
- default=DEFARGS['FRONTEND_LANGUAGE_TARGET'],
104
- metavar="<language code>",
105
- help="Set frontend default language - target (%(default)s)",
106
- )
107
- parser.add_argument(
108
- "--frontend-timeout",
109
- type=int,
110
- default=DEFARGS['FRONTEND_TIMEOUT'],
111
- metavar="<milliseconds>",
112
- help="Set frontend translation timeout (%(default)s)",
113
- )
114
- parser.add_argument(
115
- "--api-keys",
116
- default=DEFARGS['API_KEYS'],
117
- action="store_true",
118
- help="Enable API keys database for per-user rate limits lookup",
119
- )
120
- parser.add_argument(
121
- "--api-keys-db-path",
122
- default=DEFARGS['API_KEYS_DB_PATH'],
123
- type=str,
124
- help="Use a specific path inside the container for the local database. Can be absolute or relative (%(default)s)",
125
- )
126
- parser.add_argument(
127
- "--api-keys-remote",
128
- default=DEFARGS['API_KEYS_REMOTE'],
129
- type=str,
130
- help="Use this remote endpoint to query for valid API keys instead of using the local database",
131
- )
132
- parser.add_argument(
133
- "--get-api-key-link",
134
- default=DEFARGS['GET_API_KEY_LINK'],
135
- type=str,
136
- help="Show a link in the UI where to direct users to get an API key",
137
- )
138
- parser.add_argument(
139
- "--require-api-key-origin",
140
- type=str,
141
- default=DEFARGS['REQUIRE_API_KEY_ORIGIN'],
142
- help="Require use of an API key for programmatic access to the API, unless the request origin matches this domain",
143
- )
144
- parser.add_argument(
145
- "--require-api-key-secret",
146
- default=DEFARGS['REQUIRE_API_KEY_SECRET'],
147
- action="store_true",
148
- help="Require use of an API key for programmatic access to the API, unless the client also sends a secret match",
149
- )
150
- parser.add_argument(
151
- "--shared-storage",
152
- type=str,
153
- default=DEFARGS['SHARED_STORAGE'],
154
- metavar="<Storage URI>",
155
- help="Shared storage URI to use for multi-process data sharing (e.g. via gunicorn)",
156
- )
157
- parser.add_argument(
158
- "--load-only",
159
- type=operator.methodcaller("split", ","),
160
- default=DEFARGS['LOAD_ONLY'],
161
- metavar="<comma-separated language codes>",
162
- help="Set available languages (ar,de,en,es,fr,ga,hi,it,ja,ko,pt,ru,zh)",
163
- )
164
- parser.add_argument(
165
- "--alternatives-limit",
166
- default=DEFARGS['ALTERNATIVES_LIMIT'],
167
- type=int,
168
- metavar="<maximum number of alternatives translations>",
169
- help="Set the maximum number of supported alternative translations (%(default)s)",
170
- )
171
- parser.add_argument(
172
- "--threads",
173
- default=DEFARGS['THREADS'],
174
- type=int,
175
- metavar="<number of threads>",
176
- help="Set number of threads (%(default)s)",
177
- )
178
- parser.add_argument(
179
- "--suggestions", default=DEFARGS['SUGGESTIONS'], action="store_true", help="Allow user suggestions"
180
- )
181
- parser.add_argument(
182
- "--disable-files-translation", default=DEFARGS['DISABLE_FILES_TRANSLATION'], action="store_true",
183
- help="Disable files translation"
184
- )
185
- parser.add_argument(
186
- "--disable-web-ui", default=DEFARGS['DISABLE_WEB_UI'], action="store_true", help="Disable web ui"
187
- )
188
- parser.add_argument(
189
- "--update-models", default=DEFARGS['UPDATE_MODELS'], action="store_true", help="Update language models at startup"
190
- )
191
- parser.add_argument(
192
- "--force-update-models", default=DEFARGS['FORCE_UPDATE_MODELS'], action="store_true", help="Install/Reinstall language models at startup"
193
- )
194
- parser.add_argument(
195
- "--metrics",
196
- default=DEFARGS['METRICS'],
197
- action="store_true",
198
- help="Enable the /metrics endpoint for exporting Prometheus usage metrics",
199
- )
200
- parser.add_argument(
201
- "--metrics-auth-token",
202
- default=DEFARGS['METRICS_AUTH_TOKEN'],
203
- type=str,
204
- help="Protect the /metrics endpoint by allowing only clients that have a valid Authorization Bearer token (%(default)s)",
205
- )
206
- parser.add_argument(
207
- "--url-prefix",
208
- default=DEFARGS['URL_PREFIX'],
209
- type=str,
210
- help="Add prefix to URL: example.com:5000/url-prefix/",
211
- )
212
- args = parser.parse_args()
213
- if args.url_prefix and not args.url_prefix.startswith('/'):
214
- args.url_prefix = '/' + args.url_prefix
215
- return args
216
 
217
  def main():
218
- args = get_args()
 
219
  app = create_app(args)
220
-
221
- # No need to check for '--wsgi' or sys.argv here
222
- # Directly run the app using a WSGI server
223
- from waitress import serve
224
  serve(app, host=args.host, port=args.port)
225
 
226
  if __name__ == "__main__":
 
 
 
 
 
1
  from libretranslate.app import create_app
2
  from libretranslate.default_values import DEFAULT_ARGUMENTS as DEFARGS
3
+ from waitress import serve
4
 
5
+ def create_args():
6
+ """Return default arguments compatible with Hugging Face Spaces."""
7
+ class Args:
8
+ def __init__(self):
9
+ self.host = "0.0.0.0" # Listen on all interfaces
10
+ self.port = 7860 # Default port for Hugging Face Spaces
11
+ self.debug = False
12
+ self.ssl = False
13
+ self.url_prefix = ""
14
+ # Set any other defaults you need
15
 
16
+ return Args()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  def main():
19
+ """Main entry point for the application."""
20
+ args = create_args()
21
  app = create_app(args)
22
+ print(f"Running on http://{args.host}:{args.port}{args.url_prefix}")
 
 
 
23
  serve(app, host=args.host, port=args.port)
24
 
25
  if __name__ == "__main__":