Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
LibreTranslate
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Riße, Matthias
LibreTranslate
Commits
2c5eba14
Commit
2c5eba14
authored
4 years ago
by
Claas Augner
Browse files
Options
Downloads
Patches
Plain Diff
feat: support batch translation
parent
72248f00
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
app/app.py
+29
-12
29 additions, 12 deletions
app/app.py
app/templates/index.html
+7
-7
7 additions, 7 deletions
app/templates/index.html
with
36 additions
and
19 deletions
app/app.py
+
29
−
12
View file @
2c5eba14
...
...
@@ -16,7 +16,7 @@ def get_remote_address():
def
create_app
(
char_limit
=-
1
,
req_limit
=-
1
,
ga_id
=
None
,
debug
=
False
,
frontend_language_source
=
"
en
"
,
frontend_language_target
=
"
en
"
):
from
app.init
import
boot
boot
()
from
app.language
import
languages
app
=
Flask
(
__name__
)
...
...
@@ -36,9 +36,9 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la
})
else
:
frontend_argos_language_source
=
next
(
iter
([
l
for
l
in
languages
if
l
.
code
==
frontend_language_source
]),
None
)
frontend_argos_language_target
=
next
(
iter
([
l
for
l
in
languages
if
l
.
code
==
frontend_language_target
]),
None
)
# Raise AttributeError to prevent app startup if user input is not valid.
if
frontend_argos_language_source
is
None
:
raise
AttributeError
(
f
"
{
frontend_language_source
}
as frontend source language is not supported.
"
)
...
...
@@ -126,17 +126,20 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la
- in: formData
name: q
schema:
type: string
example: Hello world!
oneOf:
- type: string
example: Hello world!
- type: array
example: [
'
Hello world!
'
]
required: true
description: Text to translate
description: Text
(s)
to translate
- in: formData
name: source
schema:
type: string
example: en
required: true
description: Source language code
description: Source language code
- in: formData
name: target
schema:
...
...
@@ -152,8 +155,10 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la
type: object
properties:
translatedText:
type: string
description: Translated text
oneOf:
- type: string
- type: array
description: Translated text(s)
400:
description: Invalid request
schema:
...
...
@@ -200,8 +205,16 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la
if
not
target_lang
:
abort
(
400
,
description
=
"
Invalid request: missing target parameter
"
)
batch
=
isinstance
(
q
,
list
)
if
char_limit
!=
-
1
:
q
=
q
[:
char_limit
]
if
batch
:
chars
=
sum
([
len
(
text
)
for
text
in
q
])
else
:
chars
=
len
(
q
)
if
char_limit
<
chars
:
abort
(
400
,
description
=
"
Invalid request: Request (%d) exceeds character limit (%d)
"
%
(
chars
,
char_limit
))
if
source_lang
==
'
auto
'
:
candidate_langs
=
list
(
filter
(
lambda
l
:
l
.
lang
in
language_map
,
detect_langs
(
q
)))
...
...
@@ -217,20 +230,24 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la
source_lang
=
'
en
'
else
:
source_lang
=
'
en
'
if
debug
:
print
(
"
Auto detected: %s
"
%
source_lang
)
src_lang
=
next
(
iter
([
l
for
l
in
languages
if
l
.
code
==
source_lang
]),
None
)
tgt_lang
=
next
(
iter
([
l
for
l
in
languages
if
l
.
code
==
target_lang
]),
None
)
if
src_lang
is
None
:
abort
(
400
,
description
=
"
%s is not supported
"
%
source_lang
)
if
tgt_lang
is
None
:
abort
(
400
,
description
=
"
%s is not supported
"
%
target_lang
)
translator
=
src_lang
.
get_translation
(
tgt_lang
)
try
:
if
batch
:
return
jsonify
({
"
translatedText
"
:
[
translator
.
translate
(
text
)
for
text
in
q
]
})
else
:
return
jsonify
({
"
translatedText
"
:
translator
.
translate
(
q
)
})
except
Exception
as
e
:
abort
(
500
,
description
=
"
Cannot translate text: %s
"
%
str
(
e
))
...
...
This diff is collapsed.
Click to expand it.
app/templates/index.html
+
7
−
7
View file @
2c5eba14
...
...
@@ -247,9 +247,9 @@
window
.
Prism
=
window
.
Prism
||
{};
window
.
Prism
.
manual
=
true
;
</script>
<script
src=
"https://cdnjs.cloudflare.com/ajax/libs/prism/1.22.0/prism.min.js"
></script>
<script>
// API host/endpoint
var
BaseUrl
=
window
.
location
.
protocol
+
"
//
"
+
window
.
location
.
host
;
...
...
@@ -317,7 +317,7 @@ document.addEventListener('DOMContentLoaded', function(){
return
;
}
self
.
loading
=
false
;
self
.
loading
=
false
;
}
else
{
self
.
error
=
"
Cannot load /languages
"
;
self
.
loading
=
false
;
...
...
@@ -346,8 +346,8 @@ document.addEventListener('DOMContentLoaded', function(){
if
(
this
.
charactersLimit
!==
-
1
&&
this
.
inputText
.
length
>=
this
.
charactersLimit
){
this
.
inputText
=
this
.
inputText
.
substring
(
0
,
this
.
charactersLimit
);
}
},
computed
:
{
requestCode
:
function
(){
...
...
@@ -423,7 +423,7 @@ document.addEventListener('DOMContentLoaded', function(){
var
res
=
JSON
.
parse
(
this
.
response
);
// Success!
if
(
res
.
translatedText
!==
undefined
){
self
.
translatedText
=
res
.
translatedText
;
self
.
translatedText
=
res
.
translatedText
;
self
.
loadingTranslation
=
false
;
self
.
output
=
JSON
.
stringify
(
res
,
null
,
4
);
}
else
{
...
...
@@ -450,7 +450,7 @@ document.addEventListener('DOMContentLoaded', function(){
this
.
$refs
.
translatedTextarea
.
select
();
this
.
$refs
.
translatedTextarea
.
setSelectionRange
(
0
,
9999999
);
/* For mobile devices */
document
.
execCommand
(
"
copy
"
);
if
(
this
.
copyTextLabel
===
"
Copy Text
"
){
this
.
copyTextLabel
=
"
Copied!
"
;
var
self
=
this
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment