mirror of
https://github.com/LibreTranslate/LibreTranslate.git
synced 2024-10-31 22:08:59 +00:00
First commit
This commit is contained in:
parent
64547a8acf
commit
1062ef2713
9 changed files with 296 additions and 0 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -127,3 +127,5 @@ dmypy.json
|
||||||
|
|
||||||
# Pyre type checker
|
# Pyre type checker
|
||||||
.pyre/
|
.pyre/
|
||||||
|
installed_models/
|
||||||
|
|
||||||
|
|
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
[submodule "models"]
|
||||||
|
path = models
|
||||||
|
url = https://github.com/uav4geo/LibreTranslate-Models
|
30
app/init.py
Normal file
30
app/init.py
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
import os
|
||||||
|
from argostranslate import translate
|
||||||
|
import os, glob, shutil, zipfile
|
||||||
|
|
||||||
|
INSTALLED_MODELS_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "installed_models"))
|
||||||
|
|
||||||
|
os.environ["ARGOS_TRANSLATE_PACKAGES_DIR"] = INSTALLED_MODELS_DIR
|
||||||
|
|
||||||
|
def boot():
|
||||||
|
check_and_install_models()
|
||||||
|
|
||||||
|
def check_and_install_models(force=False):
|
||||||
|
if os.path.exists(INSTALLED_MODELS_DIR) and not force:
|
||||||
|
return
|
||||||
|
|
||||||
|
if os.path.exists(INSTALLED_MODELS_DIR):
|
||||||
|
print("Removing old %s" % INSTALLED_MODELS_DIR)
|
||||||
|
shutil.rmtree(INSTALLED_MODELS_DIR)
|
||||||
|
|
||||||
|
print("Creating %s" % INSTALLED_MODELS_DIR)
|
||||||
|
os.makedirs(INSTALLED_MODELS_DIR, exist_ok=True)
|
||||||
|
|
||||||
|
|
||||||
|
for f in glob.glob("models/**.argosmodel"):
|
||||||
|
print("Installing %s..." % f)
|
||||||
|
with zipfile.ZipFile(f, 'r') as zip:
|
||||||
|
zip.extractall(path=INSTALLED_MODELS_DIR)
|
||||||
|
|
||||||
|
print("Installed %s language models!" % (len(translate.load_installed_languages())))
|
||||||
|
|
3
app/language.py
Normal file
3
app/language.py
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
from argostranslate import translate
|
||||||
|
|
||||||
|
languages = translate.load_installed_languages()
|
4
install_models.py
Normal file
4
install_models.py
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
from app.init import check_and_install_models
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
check_and_install_models(force=True)
|
66
main.py
Normal file
66
main.py
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
import argparse
|
||||||
|
from flask import Flask, render_template, jsonify, request, abort, send_from_directory
|
||||||
|
from app.language import languages
|
||||||
|
from app.init import boot
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(description='LibreTranslate - Free and Open Source Translation API')
|
||||||
|
parser.add_argument('host', type=str,
|
||||||
|
help='Hostname', default="127.0.0.1")
|
||||||
|
parser.add_argument('port', type=int,
|
||||||
|
help='Port', default=5000)
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
boot()
|
||||||
|
app = Flask(__name__)
|
||||||
|
app.config['TEMPLATES_AUTO_RELOAD'] = True
|
||||||
|
|
||||||
|
|
||||||
|
@app.errorhandler(400)
|
||||||
|
def invalid_api(e):
|
||||||
|
return jsonify({"error": str(e.description)}), 400
|
||||||
|
|
||||||
|
@app.errorhandler(500)
|
||||||
|
def server_error(e):
|
||||||
|
return jsonify({"error": str(e.description)}), 500
|
||||||
|
|
||||||
|
@app.route("/")
|
||||||
|
def index():
|
||||||
|
return send_from_directory('static', 'index.html')
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/languages")
|
||||||
|
def langs():
|
||||||
|
return jsonify([{'code': l.code, 'name': l.name } for l in languages])
|
||||||
|
|
||||||
|
@app.route("/translate", methods=['POST'])
|
||||||
|
def translate():
|
||||||
|
|
||||||
|
q = request.values.get("q")
|
||||||
|
source_lang = request.values.get("source")
|
||||||
|
target_lang = request.values.get("target")
|
||||||
|
|
||||||
|
if not q:
|
||||||
|
abort(400, description="Invalid request: missing q parameter")
|
||||||
|
if not source_lang:
|
||||||
|
abort(400, description="Invalid request: missing source parameter")
|
||||||
|
if not target_lang:
|
||||||
|
abort(400, description="Invalid request: missing target parameter")
|
||||||
|
|
||||||
|
|
||||||
|
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:
|
||||||
|
return jsonify({"translatedText": translator.translate(q) })
|
||||||
|
except Exception as e:
|
||||||
|
abort(500, description="Cannot translate text: %s" % str(e))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.run(host=args.host)
|
1
models
Submodule
1
models
Submodule
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 0492a7619847d74f3ec6e7e2b6707e00103eebd0
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
argostranslate==1.0.3
|
||||||
|
|
185
static/index.html
Normal file
185
static/index.html
Normal file
|
@ -0,0 +1,185 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>LibreTranslate - Free and Open Source Translation API</title>
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
|
||||||
|
<!-- Compiled and minified CSS -->
|
||||||
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
|
||||||
|
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
|
||||||
|
<style type="text/css">
|
||||||
|
textarea.materialize-textarea{height: 6rem;}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<nav class="blue lighten-1" role="navigation">
|
||||||
|
<div class="nav-wrapper container"><a id="logo-container" href="#" class="brand-logo"><i class="material-icons">translate</i> LibreTranslate</a>
|
||||||
|
<ul class="right hide-on-med-and-down">
|
||||||
|
<li><a href="https://github.com/uav4geo/LibreTranslate">Documentation</a></li>
|
||||||
|
<li><a href="https://github.com/uav4geo/LibreTranslate">GitHub</a></li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<ul id="nav-mobile" class="sidenav">
|
||||||
|
<li><a href="https://github.com/uav4geo/LibreTranslate">Documentation</a></li>
|
||||||
|
<li><a href="https://github.com/uav4geo/LibreTranslate">GitHub</a></li>
|
||||||
|
</ul>
|
||||||
|
<a href="#" data-target="nav-mobile" class="sidenav-trigger"><i class="material-icons">menu</i></a>
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
|
<div id="app">
|
||||||
|
<div class="section no-pad-bot">
|
||||||
|
<div class="container">
|
||||||
|
<div class="row">
|
||||||
|
<h3 class="header center">Translation API</h3>
|
||||||
|
<div class="card horizontal">
|
||||||
|
<div class="card-stacked">
|
||||||
|
<div class="card-content">
|
||||||
|
<form class="col s12">
|
||||||
|
<div class="row">
|
||||||
|
<div class="input-field col s6">
|
||||||
|
<select v-model="sourceLang" ref="sourceLangDropdown">
|
||||||
|
<template v-for="option in langs">
|
||||||
|
<option :value="option.code">{{ option.name }}</option>
|
||||||
|
</template>
|
||||||
|
</select>
|
||||||
|
<label>Source</label>
|
||||||
|
</div>
|
||||||
|
<div class="input-field col s6">
|
||||||
|
<select v-model="targetLang" ref="targetLangDropdown">
|
||||||
|
<template v-for="option in langs">
|
||||||
|
<option :value="option.code">{{ option.name }}</option>
|
||||||
|
</template>
|
||||||
|
</select>
|
||||||
|
<label>Target</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="input-field col s6">
|
||||||
|
<textarea id="textarea1" rows="5" class="materialize-textarea"></textarea>
|
||||||
|
<label for="textarea1">Input Text</label>
|
||||||
|
</div>
|
||||||
|
<div class="input-field col s6">
|
||||||
|
<div class="progress">
|
||||||
|
<div class="indeterminate"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<textarea id="textarea1" rows="5" class="materialize-textarea"></textarea>
|
||||||
|
<label for="textarea1">Translated Text</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="section no-pad-bot" id="index-banner">
|
||||||
|
<div class="container">
|
||||||
|
<div class="row center">
|
||||||
|
<div class="col s12 m12">
|
||||||
|
<div class="card horizontal">
|
||||||
|
<div class="card-stacked">
|
||||||
|
<div class="card-content">
|
||||||
|
<div class="row center">
|
||||||
|
<div class="col s12 m6">
|
||||||
|
<p><pre>{{ sourceLang }}</pre></p>
|
||||||
|
</div>
|
||||||
|
<div class="col s12 m6">
|
||||||
|
<div class="progress">
|
||||||
|
<div class="indeterminate"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<footer class="page-footer blue darken-3">
|
||||||
|
<div class="container">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col l6 s12">
|
||||||
|
<h5 class="white-text">LibreTranslate</h5>
|
||||||
|
<p class="grey-text text-lighten-4">Free and Open Source Translation API, licensed under the <a class="grey-text text-lighten-4" href="https://www.gnu.org/licenses/agpl-3.0.en.html">AGPLv3</a></p>
|
||||||
|
<p class="grey-text text-lighten-4">
|
||||||
|
Made with ❤ by <a class="grey-text text-lighten-3" href="https://uav4geo.com">UAV4GEO</a> and powered by <a class="grey-text text-lighten-3" href="https://github.com/argosopentech/argos-translate/">Argos Translate</a>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div class="col l4 offset-l2 s12">
|
||||||
|
<!-- <h5 class="white-text">Links</h5>
|
||||||
|
<ul>
|
||||||
|
<li><a class="grey-text text-lighten-3" href="#!">Link 1</a></li>
|
||||||
|
<li><a class="grey-text text-lighten-3" href="#!">Link 2</a></li>
|
||||||
|
<li><a class="grey-text text-lighten-3" href="#!">Link 3</a></li>
|
||||||
|
<li><a class="grey-text text-lighten-3" href="#!">Link 4</a></li>
|
||||||
|
</ul> -->
|
||||||
|
<div class="container">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="footer-copyright center">
|
||||||
|
</div>
|
||||||
|
</footer>
|
||||||
|
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
|
||||||
|
<script>
|
||||||
|
// API host/endpoint
|
||||||
|
var BaseUrl = '';
|
||||||
|
|
||||||
|
document.addEventListener('DOMContentLoaded', function(){
|
||||||
|
// var elems = document.querySelectorAll('select');
|
||||||
|
// var instances = M.FormSelect.init(elems);
|
||||||
|
|
||||||
|
var app = new Vue({
|
||||||
|
el: '#app',
|
||||||
|
data: {
|
||||||
|
loading: true,
|
||||||
|
error: "",
|
||||||
|
langs: [],
|
||||||
|
sourceLang: "",
|
||||||
|
targetLang: ""
|
||||||
|
},
|
||||||
|
mounted: function(){
|
||||||
|
var self = this;
|
||||||
|
var request = new XMLHttpRequest();
|
||||||
|
request.open('GET', BaseUrl + '/languages', true);
|
||||||
|
|
||||||
|
request.onload = function() {
|
||||||
|
if (this.status >= 200 && this.status < 400) {
|
||||||
|
// Success!
|
||||||
|
self.langs = JSON.parse(this.response);
|
||||||
|
self.sourceLang = self.langs[0].code;
|
||||||
|
self.targetLang = self.langs[1].code;
|
||||||
|
} else {
|
||||||
|
self.error = "Cannot load /languages";
|
||||||
|
self.loading = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
request.onerror = function() {
|
||||||
|
self.error = "Error while calling /languages";
|
||||||
|
self.loading = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
request.send();
|
||||||
|
},
|
||||||
|
updated: function(){
|
||||||
|
M.FormSelect.init(this.$refs.sourceLangDropdown);
|
||||||
|
M.FormSelect.init(this.$refs.targetLangDropdown);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in a new issue