Skip to content

Commit

Permalink
вроде все должно работать
Browse files Browse the repository at this point in the history
  • Loading branch information
13MrBlackCat13 committed Sep 4, 2024
1 parent f7fc1d2 commit c9915f4
Show file tree
Hide file tree
Showing 9 changed files with 17,910 additions and 24 deletions.
3 changes: 2 additions & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ python-dotenv==1.0.0
gunicorn==20.1.0
scipy
cython
flask_mail
flask_mail
bs4
70 changes: 58 additions & 12 deletions backend/src/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from src.services.notification_service import notification_service
from src.services.auto_retrain_service import auto_retrain_service
from src.services.data_processor import create_directory_structure, process_dataset, process_email
from flask import current_app
from werkzeug.utils import secure_filename

api_bp = Blueprint('api', __name__)
classifier = EmailClassifier()
Expand Down Expand Up @@ -210,26 +212,54 @@ def index():
def sort():
return render_template('sort.html')


@api_bp.route('/get_emails', methods=['GET'])
def get_emails():
raw_data_dir = 'data/raw'
raw_data_dir = current_app.config.get('DATA_DIR', 'data/raw')
emails = []
for category in os.listdir(raw_data_dir):

# Проверяем папку Unsorted
unsorted_path = os.path.join(raw_data_dir, 'Unsorted')
if os.path.exists(unsorted_path):
for filename in os.listdir(unsorted_path):
file_path = os.path.join(unsorted_path, filename)
try:
if os.path.isfile(file_path):
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read()
emails.append({
'filename': filename,
'category': 'Unsorted',
'content': content[:200] + '...' # Preview of content
})
except Exception as e:
current_app.logger.error(f"Error reading file {file_path}: {str(e)}")

# Проверяем остальные категории
categories = ['Входящие', 'Рассылки', 'Социальные сети', 'Чеки_Квитанции', 'Новости', 'Доставка', 'Госписьма',
'Учёба', 'Игры', 'Spam/Мошенничество', 'Spam/Обычный']

for category in categories:
category_path = os.path.join(raw_data_dir, category)
if os.path.isdir(category_path):
if os.path.exists(category_path):
for filename in os.listdir(category_path):
file_path = os.path.join(category_path, filename)
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read()
emails.append({
'filename': filename,
'category': category,
'content': content[:200] + '...' # Preview of content
})
try:
if os.path.isfile(file_path):
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read()
emails.append({
'filename': filename,
'category': category,
'content': content[:200] + '...' # Preview of content
})
except Exception as e:
current_app.logger.error(f"Error reading file {file_path}: {str(e)}")

return jsonify(emails)

@api_bp.route('/classify_email', methods=['POST'])
def classify_email():
def classify_email_general():
data = request.json
if not data or 'content' not in data:
return jsonify({"error": "No content provided"}), 400
Expand All @@ -253,4 +283,20 @@ def move_email():
return jsonify({"error": "Source file does not exist"}), 400

os.rename(from_path, to_path)
return jsonify({"message": "Email moved successfully"})
return jsonify({"message": "Email moved successfully"})

@api_bp.route('/upload_email', methods=['POST'])
def upload_email():
if 'file' not in request.files:
return jsonify({"error": "No file part"}), 400
file = request.files['file']
if file.filename == '':
return jsonify({"error": "No selected file"}), 400
if file:
filename = secure_filename(file.filename)
unsorted_path = os.path.join(current_app.config.get('DATA_DIR', 'data/raw'), 'Unsorted')
if not os.path.exists(unsorted_path):
os.makedirs(unsorted_path)
file_path = os.path.join(unsorted_path, filename)
file.save(file_path)
return jsonify({"message": "File uploaded successfully"}), 200
2 changes: 1 addition & 1 deletion backend/src/services/data_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def create_directory_structure():
directories = [
'data/raw', 'data/processed',
'data/raw', 'data/processed', 'data/raw/Unsorted',
'models', 'logs',
'data/raw/Входящие', 'data/raw/Рассылки', 'data/raw/Социальные сети',
'data/raw/Чеки_Квитанции', 'data/raw/Новости', 'data/raw/Доставка',
Expand Down
Loading

0 comments on commit c9915f4

Please sign in to comment.