def translate_api():
    start_time = datetime.datetime.now()
    text = request.json['text']
    languages = request.json['languages']
    savetext = 'savetext' in request.json and request.json['savetext'] == True

    if languages == 'eng-cat':
        openNMT = openNMT_engcat
    else:
        openNMT = openNMT_cateng


#    print("Input:" + text)
    tokenizer = TextTokenizer()
    sentences, translate = tokenizer.tokenize(text)

    results = _launch_translate_threads(openNMT, text, sentences, translate)
    translated = tokenizer.sentence_from_tokens(sentences, translate, results)

    if savetext:
        saved_filename = os.path.join(SAVED_TEXTS, "source.txt")
        with open(saved_filename, "a") as text_file:
            t = text.replace('\n', '')
            text_file.write(f'{languages}\t{t}\n')

    time_used = datetime.datetime.now() - start_time
    words = len(text.split(' '))
    usage = Usage()
    usage.log(languages, words, time_used)
    result = {}
    result['text'] = text
    result['translated'] = translated
    result['time'] = str(time_used)
    return json_answer(result)
def upload_file():
    file = request.files['file']
    email = request.values['email']
    model_name = request.values['model_name']

    if file.filename == '':
        result = {}
        result['error'] = "No s'ha especificat el fitxer"
        return json_answer(result, 404)

    if email == '':
        result = {}
        result['error'] = "No s'ha especificat el correu"
        return json_answer(result, 404)

    if file and _allowed_file(file.filename):
        filename = uuid.uuid4().hex
        fullname = os.path.join(UPLOAD_FOLDER, filename)
        file.save(fullname)

        save_file_to_process(fullname, email, model_name)
        logging.debug("Saved file {0}".format(fullname))
        result = []
        usage = Usage()
        usage.log(model_name, 0, datetime.timedelta(), 'file')
        return json_answer(result)

    result = {}
    result['error'] = "Error desconegut"
    return json_answer(result, 500)
Exemple #3
0
 def prepare(self, argv):
     self.help_message = self.help_message % self.command_name
     BaseCommand.prepare(self, argv)
     notebook_id = self.vars.get('notebook', None)
     if notebook_id is None:
         raise Usage(self.help_message)
     self.notebook_id = self.build_available_notebook_id(notebook_id)
Exemple #4
0
def recognize_api():

    if 'file' not in request.files:
        return 'No file part'

    file = request.files['file']

    if file.filename == '':
        return 'No filename'

    time = datetime.datetime.now()
    try:
        with tempfile.NamedTemporaryFile() as temp_file:
            recognizer = Recognizer()
            filename = temp_file.name
            file.save(filename)
            text = recognizer.get_text(filename)
            used = datetime.datetime.now() - time
            usage = Usage()
            usage.log()
            return "Text reconegut: [{0}] - temps usat {1}".format(text, used)
    except ValueError as e:
        return (
            "No és un fitxer d'àudio en format PCM WAV, AIFF/AIFF-C o FLAC ",
            400)
def stats():
    requested = request.args.get('date')
    date_requested = datetime.datetime.strptime(requested, '%Y-%m-%d')
    usage = Usage()
    result = usage.get_stats(date_requested)

    return json_answer(result)
Exemple #6
0
class BaseCommand(object):
    def __init__(self):
        self.api_url = None
        self.http_header = {'Connection': 'close'}
        self.cookies = {}
        self.help_message = ""
        self.longopts = []
        self.vars = {}
        self.command_name = "unknown"

    def set_command_name(self, command_name):
        self.command_name = command_name

    def prepare(self, argv):
        try:
            longopts = ["help"] + ["%s=" % v for v in self.longopts]
            opts, args = getopt.getopt(argv, "h", longopts=longopts)
        except getopt.error, msg:
            raise Usage(self.help_message)

        longopts = ["--%s" % v for v in self.longopts]
        # option processing
        for option, value in opts:
            if option in ("-h", "--help"):
                raise Usage(self.help_message)
            if option in longopts:
                #Convert --option_name to option_name
                self.vars[option[2:]] = value
Exemple #7
0
 def prepare(self, argv):
     self.help_message = self.help_message % self.command_name
     BaseCommand.prepare(self, argv)
     interpreter_id = self.vars.get('interpreter', None)
     if interpreter_id is None:
         raise Usage(self.help_message)
     self.interpreter_id = self.build_available_interpreter_id(
         interpreter_id)
def stats():
    requested = request.args.get('date')
    date_requested = datetime.datetime.strptime(requested, '%Y-%m-%d')
    usage = Usage()
    result = usage.get_stats(date_requested)

    cnt = _get_processed_files(date_requested)
    result["files"] = cnt
    return json_answer(result)
def stats():
    requested = request.args.get('date')
    date_requested = datetime.datetime.strptime(requested, '%Y-%m-%d')
    usage = Usage()
    calls = usage.get_stats(date_requested)

    result = {}
    result['calls'] = calls
    return json_answer(json.dumps(result, indent=4, separators=(',', ': ')))
Exemple #10
0
def main():
    bot.add_cog(Input(bot))
    bot.add_cog(Output(bot))
    bot.add_cog(Administration(bot))
    bot.add_cog(Usage(bot))

    bot.run(TOKEN)

    db_cursor.close()
    db_connection.close()
Exemple #11
0
def main(argv=None):

    if argv is None:
        argv = sys.argv

    # option processing
    config_path = os.environ['HOME'] + "/.zdari.yml"
    try:
        opts, args = getopt.getopt(argv[1:], "h", ["help", "config="])
        raise_usage = len(argv) < 2
        next_argv_index = 1 if len(argv) >= 2 else 0
        for option, value in opts:
            if option in ("-h", "--help"):
                raise_usage = True
            if option in ("--config"):
                config_path = value
                remove_index = [
                    i for (i, v) in enumerate(argv) if v == '--config'
                ]
                next_argv_index = 3 if len(argv) > 3 else 0
        config = init(config_path)
        command_type = argv[next_argv_index]

        if raise_usage or command_type not in zeppelincmd.invoker.list_command_types(
        ):
            raise Usage(help_message)

        argv = argv[next_argv_index + 1:]
        command_name = argv[0] if len(argv) > 0 else ""
        if command_name not in zeppelincmd.invoker.list_commands(command_type):
            raise Usage("""Command type: %s\nSupport Commands\n%s
             """ % (command_type, '\n'.join([
                "  " + v
                for v in zeppelincmd.invoker.list_commands(command_type)
            ])))

        argv = argv[1:]
        zeppelincmd.invoker.execute(command_type, command_name, config, argv)

    except Usage, err:
        print >> sys.stderr, str(err.msg)
        return 2
Exemple #12
0
def search_request():
    source = request.args.get('source')
    target = request.args.get('target')
    project = ','.join(request.args.getlist('project'))

    search = Search(source, target, project)
    View = WebView()
    result = View.do(search)

    usage = Usage()
    usage.log()
    return result
def search_api():
    source = request.args.get('source')
    target = request.args.get('target')
    page = request.args.get('page')
    project = ','.join(request.args.getlist('project'))

    search = Search(source, target, project)
    results = SearchAPIResults()
    json = results.do(search, page)

    usage = Usage()
    usage.log()
    return json
Exemple #14
0
def start():
    argument = sys.argv[1:]
    list_item = ToDoModel()
    usage = Usage()
    if not argument:
        usage.usage_info()
    elif argument[0] == "-l":
        list_item.list_todo()
    elif argument[0] == "-a":
        list_item.add(sys.argv[2])
    elif argument[0] == "-r":
        list_item.remove(sys.argv[2])
    elif argument[0] == "-c":
        list_item.list_todo
Exemple #15
0
def main():
    problem_dataset_dir = os.path.join('Problems', 'Problem2')
    solution_dir = os.path.join("Problems", "Problem2Solution")

    data_reader = DataReader(problem_dataset_dir)
    training_data, testing_data = data_reader.get_data()
    codons_table = data_reader.get_rna_codon_table()

    for sample in training_data:
        dna_string = sample[0][0]
        amino_acid = sample[0][1]
        output = sample[1]
        dna = DNA(dna_string)
        dna.set_codon_table(codons_table)
        candidates = dna.get_dna_to_amino_acid_candidates(amino_acid)

        if set(candidates) != set(output):
            raise Exception("Output not matched!\nExpecting: " + str(output) +
                            "\nFound: " + str(candidates))

    print("Passed training data..\n\n")

    writer = DataWriter(solution_dir)
    usage = Usage()

    for sample in testing_data:
        usage.start()
        dna_string = sample[0][0]
        amino_acid = sample[0][1]
        dna = DNA(dna_string)
        dna.set_codon_table(codons_table)
        candidates = dna.get_dna_to_amino_acid_candidates(amino_acid)
        usage.end()

        writer.write_data((dna_string, amino_acid), candidates,
                          usage.get_execution_time(), usage.get_memory_usage())
        print("DNA:\n" + dna_string)
        print("Protein\n" + amino_acid)

        print("\n\nOutput")
        print("=====")

        print(str(len(candidates)))
        for substring in candidates:
            print(substring)

        print("\n\nExecution Time: " + str(usage.get_execution_time()) + " s")
        print("Memory Usage: " + str(usage.get_memory_usage()) + " MB")
Exemple #16
0
    def get_data(self):
        gpu = GPU()
        usage = Usage(3)

        data = []
        data_list = [
            usage.cpu_csv(),
            usage.disk_csv(),
            usage.mem_ram_csv(),
            usage.mem_swap_csv(),
            gpu.csv()
        ]
        for i in data_list:
            data += i.split(',')

        return data
    def get_json(self, date_requested):
        dao = ProjectMetaDataDao()
        dao.open('statistics.db3')
        total_words = 0
        projects = dao.get_all()
        for project in projects:
            total_words += project.words

        usage = Usage()
        calls = usage.get_stats(date_requested)

        results = {}
        results['total_words'] = total_words
        results['projects'] = len(projects)
        results['searches'] = calls
        return json.dumps(results, indent=4, separators=(',', ': '))
def main():
    problem_dataset_dir = os.path.join('Problems', 'Problem11')
    solution_dir = os.path.join("Problems", "Problem11Solution")

    data_reader = DataReader(problem_dataset_dir)
    test_cases, output = data_reader.get_data()
    scoring_matrix = data_reader.get_BLOSUM62_data()

    for train_i in range(0, len(output)):
        alpha_dna, beta_dna = test_cases[train_i]
        case_output = output[train_i]
        strings_algorithms = StringsAlgorithms(alpha_dna, beta_dna)
        align = strings_algorithms.alignment(_type='global',
                                             scoring_matrix=scoring_matrix)

        if align != case_output:
            raise Exception("Output not matched!\nExpecting: " +
                            str(case_output) + "\nFound: " + str(align))

    print("Passed training data..")

    writer = DataWriter(solution_dir)
    usage = Usage()

    for test_i in range(len(test_cases) - len(output), len(test_cases)):
        usage.start()
        alpha_dna, beta_dna = test_cases[test_i]
        strings_algorithms = StringsAlgorithms(alpha_dna, beta_dna)
        align = strings_algorithms.alignment(_type='global',
                                             scoring_matrix=scoring_matrix)
        usage.end()

        writer.write_data(test_i + 1, align, usage.get_execution_time(),
                          usage.get_memory_usage())
        print("\n\nInput:\n" + alpha_dna + "\n" + beta_dna + "\n")

        print("\n\nOutput")
        print("=====")

        print(align[0])
        print(align[1][0])
        print(align[1][1])

        print("\n")
        print("======")
        print("Execution Time: " + str(usage.get_execution_time()) + " s")
        print("Memory Used: " + str(usage.get_memory_usage()) + " MB")
def main():
    problem_dataset_dir = os.path.join('Problems', 'Problem10')
    solution_dir = os.path.join("Problems", "Problem10Solution")

    data_reader = DataReader(problem_dataset_dir)
    test_cases, output = data_reader.get_data()

    for train_i in range(0, len(output)):
        alpha_dna, beta_dna = test_cases[train_i]
        case_output = output[train_i]
        strings_algorithms = StringsAlgorithms(alpha_dna, beta_dna)
        lcs = strings_algorithms.lcs('dp')
        print(len(lcs))

        if len(case_output) != len(lcs):
            raise Exception("Output not matched!\nExpecting: " +
                            str(case_output) + "\nFound: " + str(lcs))

    print("Passed training data..")

    writer = DataWriter(solution_dir)
    usage = Usage()

    for test_i in range(len(test_cases) - len(output), len(test_cases)):
        usage.start()
        alpha_dna, beta_dna = test_cases[test_i]
        strings_algorithms = StringsAlgorithms(alpha_dna, beta_dna)
        lcs = strings_algorithms.lcs('dp')
        print(len(lcs))
        usage.end()

        writer.write_data(test_i + 1, lcs, usage.get_execution_time(),
                          usage.get_memory_usage())
        print("\n\nInput:\n" + alpha_dna + "\n" + beta_dna + "\n")

        print("\n\nOutput")
        print("=====")

        print(lcs)

        print("\n")
        print("======")
        print("Execution Time: " + str(usage.get_execution_time()) + " s")
        print("Memory Used: " + str(usage.get_memory_usage()) + " MB")
Exemple #20
0
    def __init__(self, path, repository, user):
        self.path = path
        self.gpu = GPU()
        self.usage = Usage(15)
        self.headers = [
            self.usage.cpu_header, self.usage.disk_header,
            self.usage.mem_ram_header, self.usage.mem_swap_header,
            self.gpu.header
        ]
        self.values = [
            self.usage.cpu_csv, self.usage.disk_csv, self.usage.mem_ram_csv,
            self.usage.mem_swap_csv, self.gpu.csv
        ]
        self.logger = Logger(os.path.join(self.path, repository), delay=0)
        self.logger.set_header(self.headers)
        self.logger.set_csv(self.values)

        self.saver = Saver()
        self.saver.get_repository(path, repository, user)
Exemple #21
0
def main():
    problem_dataset_dir = os.path.join('Problems', 'Problem17')
    solution_dir = os.path.join("Problems", "Problem17Solution")

    data_reader = DataReader(problem_dataset_dir)
    test_cases, output = data_reader.get_data()

    for train_i in range(0, len(output)):
        dna = test_cases[train_i][0]
        k = test_cases[train_i][1][0]
        score_matrix = test_cases[train_i][1][1]
        case_output = output[train_i]
        most_probable_k_mer = DNA(dna).get_most_probable_k_mer(int(k), score_matrix)

        if most_probable_k_mer != case_output:
            raise Exception("Output not matched!\nExpecting: " + str(case_output) + "\nFound: " + str(most_probable_k_mer))

    print("Passed training data..")

    writer = DataWriter(solution_dir)
    usage = Usage()

    for test_i in range(len(test_cases) - len(output) - 1, len(test_cases)):
        usage.start()
        dna = test_cases[test_i][0]
        k = test_cases[test_i][1][0]
        score_matrix = test_cases[test_i][1][1]
        most_probable_k_mer = DNA(dna).get_most_probable_k_mer(int(k), score_matrix)
        usage.end()

        writer.write_data(test_i + 1, most_probable_k_mer, usage.get_execution_time(), usage.get_memory_usage())
        print("\n\nInput:\n" + str(dna) + "\n" + str(k) + "\n" + str(score_matrix))

        print("\n\nOutput")
        print("=====")

        print(most_probable_k_mer)

        print("\n")
        print("======")
        print("Execution Time: " + str(usage.get_execution_time()) + " s")
        print("Memory Used: " + str(usage.get_memory_usage()) + " MB")
Exemple #22
0
def main():
    problem_dataset_dir = os.path.join('Problems', 'Problem13')
    solution_dir = os.path.join("Problems", "Problem13Solution")

    data_reader = DataReader(problem_dataset_dir)
    test_cases, output = data_reader.get_data()

    for train_i in range(0, len(output)):
        (k, genome) = test_cases[train_i]
        case_output = output[train_i]
        dna = DNA(genome)
        k_mers = dna.get_k_mers(int(k))

        if sorted(case_output) != sorted(k_mers):
            raise Exception("Output not matched!\nExpecting: " +
                            str(case_output) + "\nFound: " + str(k_mers))

    print("Passed training data..")

    writer = DataWriter(solution_dir)
    usage = Usage()

    for test_i in range(len(test_cases) - len(output), len(test_cases)):
        usage.start()
        (k, genome) = test_cases[test_i]
        dna = DNA(genome)
        k_mers = dna.get_k_mers(int(k))
        usage.end()

        writer.write_data(test_i + 1, k_mers, usage.get_execution_time(),
                          usage.get_memory_usage())
        print("\n\nInput:\n" + k + "\n" + genome + "\n")

        print("\n\nOutput")
        print("=====")

        print(k_mers)

        print("\n")
        print("======")
        print("Execution Time: " + str(usage.get_execution_time()) + " s")
        print("Memory Used: " + str(usage.get_memory_usage()) + " MB")
Exemple #23
0
def main(argv=None):
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(argv[1:], 'h', ['help'])
        except getopt.GetoptError as e:
            raise Usage(e)
        input_fc = "in_memory/temp"
        output_file = os.path.join(config.config_dir,
                                   "test_spagedi_export.txt")
        for opt, arg in opts:
            if opt in ('-h', '--help'):
                print __doc__
                return 0

        # Prompt the user: what test are we doing?
        defaults = Bunch({
            'spagedi_file_path':
            os.path.join(config.config_dir, "spagedi_data.txt"),
        })
        sequence = spagedi_user_inputs(spagedi_tree(), [], defaults)
        sauce = prepare({
            'standalone': True,
            'sequence': sequence,
            'input_fc': input_fc,
            'spagedi_file_path': defaults.spagedi_file_path,
            'output_file': output_file,
            'analysis_type': '',
            'order_by': 'Individual_ID',
            'show_spagedi_output': True,
        })

        # Fire up Spagedi and crunch some numbers
        spagedi = SpagediWrapper(**sauce)
        parameters = spagedi.getParameterInfo()
        return spagedi.execute(parameters, None)

    except Usage as e:
        print >> sys.stderr, e.msg
        print >> sys.stderr, "for help use --help"
        return 2
def main():
    problem_dataset_dir = os.path.join('Problems', 'Problem16')
    solution_dir = os.path.join("Problems", "Problem16Solution")

    data_reader = DataReader(problem_dataset_dir)
    test_cases, output = data_reader.get_data()

    for train_i in range(0, len(output)):
        k_mers = test_cases[train_i]
        graph = Graph()
        adj_list = graph.get_debruijn_graph(_type='k_mers', k_mers=k_mers)
        case_output = output[train_i]

        if sorted(adj_list.items()) != sorted(case_output.items()):
            raise Exception("Output not matched!\nExpecting: " +
                            str(sorted(case_output.items())) + "\nFound: " +
                            str(sorted(adj_list.items())))

    print("Passed training data..")

    writer = DataWriter(solution_dir)
    usage = Usage()

    for test_i in range(len(test_cases) - len(output), len(test_cases)):
        usage.start()
        k_mers = test_cases[test_i]
        adj_list = Graph().get_debruijn_graph(_type='k_mers', k_mers=k_mers)
        usage.end()

        writer.write_data(test_i + 1, adj_list, usage.get_execution_time(),
                          usage.get_memory_usage())
        print("\n\nInput:\n" + str(k_mers) + "\n")

        print("\n\nOutput")
        print("=====")

        print(adj_list)

        print("\n")
        print("======")
        print("Execution Time: " + str(usage.get_execution_time()) + " s")
        print("Memory Used: " + str(usage.get_memory_usage()) + " MB")
def main():
    problem_dataset_dir = os.path.join('Problems', 'Problem1')
    solution_dir = os.path.join("Problems", "Problem1Solution")

    data_reader = DataReader(problem_dataset_dir)
    training_data, testing_data = data_reader.get_data()
    codons_table = data_reader.get_rna_codon_table()

    for sample in training_data:
        rna_string = sample[0]
        output = sample[1]
        rna = RNA(rna_string)
        rna.set_codons_table(codons_table)
        amino_acid = rna.to_amino_acid()

        if amino_acid != output:
            raise Exception("Output not matched!\nExpecting: " + output +
                            "\nFound: " + amino_acid)

    print("Passed training data..\n\n")

    writer = DataWriter(solution_dir)
    usage = Usage()

    for sample in testing_data:
        usage.start()
        rna_string = sample[0]
        rna = RNA(rna_string)
        rna.set_codons_table(codons_table)
        amino_acid = rna.to_amino_acid()
        usage.end()

        writer.write_data(rna_string, amino_acid, usage.get_execution_time(),
                          usage.get_memory_usage())

        print("RNA:\n" + rna_string)
        print("Protein:\n" + amino_acid)
        print("\n\nExecution Time: " + str(usage.get_execution_time()) + " s")
        print("Memory Usage: " + str(usage.get_memory_usage()) + " MB")
Exemple #26
0
def voice_api():
    text = request.args.get('text')
    token = request.args.get('token')
    voice = request.args.get('voice') or "ona"

    if voice not in ["ona", "pau"]:
        return ("Bad Request", 400)

    if token is None or getMD5(text) != token.lower():
        return ("Forbidden", 403)

    txt2wave = '/usr/bin/text2wave'
    lame = '/usr/bin/lame'

    with tempfile.NamedTemporaryFile() as encoded_file,\
         tempfile.NamedTemporaryFile() as wave_file,\
         tempfile.NamedTemporaryFile() as mp3_file:

        text = _normalize(text)
        f = open(encoded_file.name, 'wb')
        f.write(text.encode('ISO-8859-15', 'ignore'))
        f.close()

        cmd = '{0} -o {1} {2} -eval "({3})"'.\
              format(txt2wave, wave_file.name, encoded_file.name, festival_voices[voice])
        p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
        p.wait()

        cmd = '{0} -f {1} {2}'.\
              format(lame, wave_file.name, mp3_file.name)
        p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
        p.wait()

        usage = Usage()
        usage.log()
        return send_file(mp3_file.name,
                         mimetype="audio/mp3",
                         as_attachment=False,
                         attachment_filename=mp3_file.name)
def apertium_translate_process(values):
    global translate_calls, translate_chars

    translate_calls += 1
    start_time = datetime.datetime.now()

    text = None
    text = values['q']
    translate_chars += len(text)
    langpair = values['langpair']
    savetext = 'savetext' in values and values['savetext'] == True

    languages = _convert_apertium_languages_aliases_to_iso639_3(langpair)

    if savetext:
        saved_filename = os.path.join(SAVED_TEXTS, "source.txt")
        with open(saved_filename, "a") as text_file:
            t = text.replace('\n', '')
            text_file.write(f'{languages}\t{t}\n')

    openNMT = openNMTs[languages]
    translated = openNMT.translate_parallel(text)

    result = {}
    result = _get_bias_message_if_needed(languages, text, result)

    time_used = datetime.datetime.now() - start_time
    words = len(text.split(' '))
    usage = Usage()
    usage.log(languages, words, time_used, 'form')

    responseData = {}
    responseData['translatedText'] = translated
    result['responseStatus'] = 200
    result['responseData'] = responseData
    result['time'] = str(time_used)
    return json_answer(result)
    for train_i in range(0, len(output)):
        case = test_cases[train_i]
        case_output = output[train_i]
        dna = DNA(case)
        reverse_complement = dna.reverse_complement()

        if reverse_complement != case_output:
            raise Exception("Output not matched!\nExpecting: " +
                            str(case_output) + "\nFound: " +
                            reverse_complement)

    print("Passed training data..")

    writer = DataWriter(solution_dir)
    usage = Usage()

    for test_i in range(len(test_cases) - len(output), len(test_cases)):
        usage.start()
        case = test_cases[test_i]
        dna = DNA(case)
        reverse_complement = dna.reverse_complement()
        usage.end()

        writer.write_data(test_i + 1, reverse_complement,
                          usage.get_execution_time(), usage.get_memory_usage())
        print("\n\nInput:\n" + case)

        print("\n\nOutput")
        print("=====")
Exemple #29
0
 def test_using_inbuilt_roundmethod(self):
     self.assertEqual(round(Usage(31, 9) / 3, 3),
                      Usage(first=10.333, second=3.0))
Exemple #30
0
 def test_onlyfirst_list(self):
     self.assertEqual(Usage.sum([a, b, b]), Usage(first=131, second=294))