<commit_before>import os
import uuid

from flask import Flask, request, render_template, jsonify, redirect, url_for

from utils import get_parsed_file

app = Flask(__name__)
IS_PROD = os.environ.get("IS_PROD", False)


@app.route('/parse-file', methods=['POST'])
def parse_file():
    file = request.files['0']
    filename = str(uuid.uuid4())
    tmp_filepath = os.path.join("conversations", filename)
    file.save(tmp_filepath)
    try:
        parsed_items, persons_list = get_parsed_file(tmp_filepath)
        response = {
            "success": True,
            "chat": parsed_items,
            "users": persons_list
        }
    except Exception as e:
        response = {
            "success": False,
            "error_message": str(e)
        }

    os.remove(tmp_filepath)
    return jsonify(response), 200

<commit_msg>Fix #66 Add FileType validation in parse API<commit_after>import os
import uuid

from flask import Flask, request, render_template, jsonify, redirect, url_for

from utils import get_parsed_file

app = Flask(__name__)
IS_PROD = os.environ.get("IS_PROD", False)


def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() == 'txt'


@app.route('/parse-file', methods=['POST'])
def parse_file():
    file = request.files['0']
    if not allowed_file(file.filename):
        response = {
            "success": False,
            "error_message": "Please upload a valid file!",
        }
    else:
        filename = str(uuid.uuid4())
        tmp_filepath = os.path.join("conversations", filename)
        file.save(tmp_filepath)
        try:
            parsed_items, persons_list = get_parsed_file(tmp_filepath)
            response = {
                "success": True,
                "chat": parsed_items,
                "users": persons_list
            }
        except Exception as e:
            response = {
                "success": False,
                "error_message": str(e)
            }

        os.remove(tmp_filepath)
    return jsonify(response), 200


{'fraction_non_alphanumeric': 0.11562959333659971, 'fraction_numerical': 0.006859382655560999, 'mean_word_length': 2.4147157190635453, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '383707', 'n_tokens_mistral': 625, 'n_tokens_neox': 615, 'n_words': 145}