from flask import Flask, jsonify, request
import urllib, string
from lid_utils import *

app = Flask(__name__)
@app.route('/')
def hello():
    return 'This is a language id checker server. Go to /help to see more.'

@app.route('/help')
def explanation():
    explanation_dict = {
                        'How to use': 'Go to /lid and send a request with \'url\' parameter that contains a dir path with wav, or a wav path. '
    }
    return jsonify(explanation_dict)

lang_cls = LangClassifier(input_loader=None, model=model)
@app.route('/lid', methods=['POST'])
def lid():
    if request.method == 'POST':
        url = request.form.get('url', None).strip()
    else:
        return 'Please send a POST request with \'url\' parameter.', 500
    print(url)
    if not os.path.exists(url):
        return 'ERROR, file not found !', 404

    try:
        if os.path.isdir(url):
            input_loader = InputLoader(url)
            assert input_loader.wav_cnt > 0, "wav count is 0"
            lang_cls.input_loader = input_loader
            response_dict = jsonify(lang_cls.check_all_spkrs())
        else:
            _, text_lab, result_dict = lang_cls.analyze_wav(url)
            response_dict = jsonify({'text_lab': text_lab, 'result_dict': result_dict})
        return response_dict, 200
    except:
        return 'ERROR, something went wrong.', 500

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=12346, use_reloader=False, threaded=True, debug=False)
from flask import Flask, jsonify, request
import urllib, string
from lid_utils import *

app = Flask(__name__)
@app.route('/')
def hello():
    return 'This is a language id checker server. Go to /help to see more.'

@app.route('/help')
def explanation():
    explanation_dict = {
                        'How to use': 'Go to /lid and send a request with \'url\' parameter that contains a dir path with wav, or a wav path. '
    }
    return jsonify(explanation_dict)

lang_cls = LangClassifier(input_loader=None, model=model)
@app.route('/lid', methods=['POST'])
def lid():
    if request.method == 'POST':
        url = request.form.get('url', None).strip()
    else:
        return 'Please send a POST request with \'url\' parameter.', 500
    print(url)
    if not os.path.exists(url):
        return 'ERROR, file not found !', 404

    try:
        if os.path.isdir(url):
            input_loader = InputLoader(url)
            assert input_loader.wav_cnt > 0, "wav count is 0"
            lang_cls.input_loader = input_loader
            response_dict = jsonify(lang_cls.check_all_spkrs())
        else:
            _, text_lab, result_dict = lang_cls.analyze_wav(url)
            response_dict = jsonify({'text_lab': text_lab, 'result_dict': result_dict})
        return response_dict, 200
    except:
        return 'ERROR, something went wrong.', 500

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=12346, use_reloader=False, threaded=True, debug=False)