-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
721 lines (612 loc) · 25.6 KB
/
app.py
File metadata and controls
721 lines (612 loc) · 25.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
"""
API Flask para el sistema de detección de ciberacoso.
Conecta el frontend con los algoritmos de backend.
"""
from flask import Flask, request, jsonify, render_template, send_from_directory
from flask_cors import CORS
import os
import csv
import sys
import time
from datetime import datetime
from werkzeug.utils import secure_filename
# Agregar el directorio actual al path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from algorithms.analyzer import CyberbullyingAnalyzer
from algorithms.selector import AlgorithmSelector
from recommendations_engine import get_recommendations_for_analysis
##Comentario Harry
UPLOAD_FOLDER = os.path.join(os.getcwd(), 'uploads')
BASE_FILE = 'patrones_combinado.csv' # Usar el archivo combinado como base
ALLOWED_EXTENSIONS = {'csv', 'txt'}
MAX_FILES = 5
TIEMPO_MAXIMO = 86400
# Crear la instancia de Flask
app = Flask(__name__)
CORS(app) # habilitar CORS si lo necesitas
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
COMBINED_FILE = 'patrones_combinado.csv'
#hASTA AQUI TODO MENOS CONSEJOS
def generar_archivo_combinado():
"""
Verifica que el archivo combinado exista. Si no existe, lo crea desde patrones.csv.
"""
if os.path.exists(COMBINED_FILE):
print(f"[INFO] Archivo combinado ya existe: {COMBINED_FILE}")
return
print(f"[INFO] Generando archivo combinado desde patrones.csv...")
patrones = cargar_todos_los_patrones()
if not patrones:
print("[WARNING] No se encontraron patrones para generar archivo combinado")
return
fieldnames = ['id', 'frase', 'categorias', 'nivel_gravedad', 'descripcion']
severity_map = {
'Muy Alto': 90,
'Alto': 70,
'Medio': 50,
'Bajo': 20
}
with open(COMBINED_FILE, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
for p in patrones:
sev_text = p.get('severity', '')
sev_num = severity_map.get(sev_text, 50)
writer.writerow({
'id': p.get('id', ''),
'frase': p.get('pattern', ''),
'categorias': p.get('category', ''),
'nivel_gravedad': sev_num,
'descripcion': p.get('description', '')
})
print(f"[INFO] Archivo combinado generado con {len(patrones)} patrones.")
def map_severity(value):
try:
val = int(value)
except:
return value # ya viene como texto
if val >= 85:
return 'Muy Alto'
elif val >= 70:
return 'Alto'
elif val >= 50:
return 'Medio'
else:
return 'Bajo'
REQUIRED_COLUMNS_1 = {'id', 'frase', 'categorias', 'nivel_gravedad', 'descripcion'}
REQUIRED_COLUMNS_2 = {'id', 'mensaje', 'gravedad', 'categoria', 'etiqueta'}
def validar_columnas(filepath):
with open(filepath, newline='', encoding='utf-8') as f:
reader = csv.reader(f)
headers = next(reader, None)
if headers is None:
return False
headers_lower = set(h.strip().lower() for h in headers)
return (REQUIRED_COLUMNS_1 <= headers_lower) or (REQUIRED_COLUMNS_2 <= headers_lower)
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
# Patrones básicos de respaldo en caso de que el CSV no se cargue
PATRONES_RESPALDO = [
{'id': '1', 'pattern': 'eres un inútil', 'category': 'insulto directo', 'severity': 'Muy Alto', 'description': 'Insulto directo'},
{'id': '2', 'pattern': 'no sirves para nada', 'category': 'humillación', 'severity': 'Muy Alto', 'description': 'Humillación'},
{'id': '3', 'pattern': 'cállate de una vez', 'category': 'agresión verbal', 'severity': 'Alto', 'description': 'Orden agresiva'},
{'id': '4', 'pattern': 'idiota', 'category': 'insulto directo', 'severity': 'Alto', 'description': 'Insulto común'},
{'id': '5', 'pattern': 'estúpido', 'category': 'insulto cognitivo', 'severity': 'Alto', 'description': 'Ataque a inteligencia'},
{'id': '6', 'pattern': 'tonto', 'category': 'insulto leve', 'severity': 'Medio', 'description': 'Insulto menor'},
{'id': '7', 'pattern': 'feo', 'category': 'insulto físico', 'severity': 'Medio', 'description': 'Ataque apariencia'},
{'id': '8', 'pattern': 'gordo', 'category': 'insulto físico', 'severity': 'Medio', 'description': 'Comentario peso'},
{'id': '9', 'pattern': 'nadie te quiere', 'category': 'exclusión social', 'severity': 'Alto', 'description': 'Exclusión social'},
{'id': '10', 'pattern': 'eres un perdedor', 'category': 'desprecio', 'severity': 'Alto', 'description': 'Desprecio general'}
]
def cargar_todos_los_patrones():
patrones_dict = {} # clave: texto normalizado, valor: patrón con más gravedad
print(f"[DEBUG] Intentando cargar patrones...")
print(f"[DEBUG] BASE_FILE: {BASE_FILE}")
print(f"[DEBUG] Directorio actual: {os.getcwd()}")
print(f"[DEBUG] BASE_FILE existe: {os.path.exists(BASE_FILE)}")
def procesar_row(row):
mensaje = (row.get('frase') or row.get('mensaje') or '').strip().lower()
if not mensaje:
return
gravedad_valor = row.get('nivel_gravedad') or row.get('gravedad') or '0'
try:
gravedad_num = int(gravedad_valor)
except:
gravedad_num = 0
nuevo_patron = {
'id': row.get('id', ''),
'pattern': row.get('frase') or row.get('mensaje', ''),
'category': row.get('categorias') or row.get('categoria', ''),
'severity': map_severity(gravedad_valor),
'severity_num': gravedad_num, # campo auxiliar para comparar
'description': row.get('descripcion') or row.get('etiqueta', '')
}
# Mantener solo el de mayor gravedad
if mensaje not in patrones_dict or gravedad_num > patrones_dict[mensaje]['severity_num']:
patrones_dict[mensaje] = nuevo_patron
# --- Procesar archivo base (ahora patrones_combinado.csv) ---
if os.path.exists(BASE_FILE):
print(f"[DEBUG] Cargando archivo base: {BASE_FILE}")
try:
with open(BASE_FILE, 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
count = 0
for row in reader:
procesar_row(row)
count += 1
print(f"[DEBUG] Procesadas {count} líneas del archivo base")
except Exception as e:
print(f"[ERROR] Error leyendo archivo base: {e}")
else:
print(f"[WARNING] Archivo base {BASE_FILE} no encontrado")
# --- Procesar archivos subidos adicionales ---
if os.path.exists(UPLOAD_FOLDER):
print(f"[DEBUG] Revisando archivos adicionales en {UPLOAD_FOLDER}")
for archivo in os.listdir(UPLOAD_FOLDER):
ruta = os.path.join(UPLOAD_FOLDER, archivo)
if os.path.isfile(ruta) and allowed_file(archivo):
try:
with open(ruta, 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
count = 0
for row in reader:
procesar_row(row)
count += 1
print(f"[DEBUG] Procesadas {count} líneas de {archivo}")
except Exception as e:
print(f"[ERROR] Error leyendo {archivo}: {e}")
# Convertir dict a lista y eliminar campo auxiliar
patrones_final = []
for pat in patrones_dict.values():
pat.pop('severity_num', None)
patrones_final.append(pat)
print(f"[DEBUG] Total patrones finales: {len(patrones_final)}")
# Si no se cargaron patrones, usar los de respaldo
if len(patrones_final) == 0:
print("[WARNING] No se cargaron patrones desde archivos, usando patrones de respaldo")
patrones_final = PATRONES_RESPALDO.copy()
print(f"[DEBUG] Usando {len(patrones_final)} patrones de respaldo")
return patrones_final
def limpiar_archivos():
ahora = time.time()
for archivo in os.listdir(UPLOAD_FOLDER):
ruta = os.path.join(UPLOAD_FOLDER, archivo)
if os.path.isfile(ruta):
edad = ahora - os.path.getctime(ruta)
if edad > TIEMPO_MAXIMO:
os.remove(ruta)
# --- API para listar archivos ---
@app.route('/api/list-files', methods=['GET'])
def list_files():
try:
files = []
# Incluir el archivo base como protegido
base_path = os.path.join(os.getcwd(), BASE_FILE)
if os.path.exists(base_path):
size = os.path.getsize(base_path) // 1024
files.append({'name': BASE_FILE, 'size': size, 'protected': True})
# Incluir archivos de uploads
for archivo in os.listdir(UPLOAD_FOLDER):
ruta = os.path.join(UPLOAD_FOLDER, archivo)
if os.path.isfile(ruta):
size = os.path.getsize(ruta) // 1024
files.append({'name': archivo, 'size': size, 'protected': False})
return jsonify({'success': True, 'files': files})
except Exception as e:
return jsonify({'success': False, 'error': f'No se pudieron listar los archivos: {str(e)}'}), 500
# --- API para subir archivos ---
@app.route('/api/upload-patterns', methods=['POST'])
def upload_patterns():
try:
limpiar_archivos()
# validar cantidad
archivos_existentes = os.listdir(UPLOAD_FOLDER)
if len(archivos_existentes) >= MAX_FILES:
return jsonify({'success': False, 'error': 'Se alcanzó el máximo de archivos permitidos.'}), 400
if 'file' not in request.files:
return jsonify({'success': False, 'error': 'No se envió ningún archivo'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'success': False, 'error': 'Nombre de archivo vacío'}), 400
# proteger nombre base
if file.filename.lower() == BASE_FILE.lower():
return jsonify({'success': False, 'error': 'No se puede subir un archivo con el nombre reservado patrones.csv'}), 400
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
filepath = os.path.join(UPLOAD_FOLDER, filename)
if os.path.exists(filepath):
return jsonify({'success': False, 'error': 'El archivo ya existe en el servidor.'}), 400
# guardar provisional
file.save(filepath)
# validar encabezados
try:
with open(filepath, 'r', encoding='utf-8') as f:
header = f.readline().strip().lower().split(',')
columnas = ['id', 'frase', 'categorias', 'nivel_gravedad', 'descripcion']
for col in columnas:
if col not in [h.strip() for h in header]:
os.remove(filepath)
return jsonify({'success': False, 'error': f'Falta columna requerida: {col}'}), 400
except Exception as e:
os.remove(filepath)
return jsonify({'success': False, 'error': f'Archivo inválido: {str(e)}'}), 400
# validar columnasgenerar_archivo_combinado()
generar_archivo_combinado()
analyzer.load_patterns()
return jsonify({'success': True, 'message': f'{filename} subido correctamente.'})
else:
return jsonify({'success': False, 'error': 'Formato no permitido (solo .csv o .txt)'}), 400
except Exception as e:
return jsonify({'success': False, 'error': f'Error al subir archivo: {str(e)}'}), 500
# --- API para eliminar archivo ---
@app.route('/api/delete-file', methods=['POST'])
def delete_file():
try:
data = request.get_json()
if not data or 'filename' not in data:
return jsonify({'success': False, 'error': 'Archivo no especificado'}), 400
filename = data['filename']
if filename.lower() == BASE_FILE.lower():
return jsonify({'success': False, 'error': 'No se puede eliminar el archivo base'}), 400
filepath = os.path.join(UPLOAD_FOLDER, filename)
if os.path.exists(filepath):
os.remove(filepath)
generar_archivo_combinado()
analyzer.load_patterns()
return jsonify({'success': True, 'message': f'{filename} eliminado correctamente'})
else:
return jsonify({'success': False, 'error': 'El archivo no existe'}), 400
except Exception as e:
return jsonify({'success': False, 'error': f'Error al eliminar archivo: {str(e)}'}), 500
@app.route('/api/delete-pattern', methods=['POST'])
def delete_pattern():
try:
data = request.get_json()
if not data or 'id' not in data:
return jsonify({'success': False, 'error': 'ID no especificado'}), 400
pattern_id = str(data['id'])
# proteger los datos primarios
if pattern_id.isdigit() and int(pattern_id) <= 115:
return jsonify({'success': False, 'error': 'Este patrón está protegido y no se puede eliminar.'}), 403
# leer y filtrar el CSV
nuevos_registros = []
eliminado = False
headers = ['id','frase','categorias','nivel_gravedad','descripcion']
if os.path.exists(BASE_FILE):
with open(BASE_FILE, 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
headers = reader.fieldnames
for row in reader:
if row['id'] != pattern_id:
nuevos_registros.append(row)
else:
eliminado = True
if eliminado:
with open(BASE_FILE, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=headers)
writer.writeheader()
writer.writerows(nuevos_registros)
generar_archivo_combinado()
analyzer.load_patterns()
return jsonify({'success': True, 'message': f'Patrón con ID {pattern_id} eliminado correctamente'})
else:
return jsonify({'success': False, 'error': 'No se encontró el patrón a eliminar'}), 404
except Exception as e:
return jsonify({'success': False, 'error': f'Error al eliminar patrón: {str(e)}'}), 500
# Configuración
app.config['SECRET_KEY'] = 'safetext-cyberbullying-detection-2025'
# Inicializar el analizador
print("[INIT] Inicializando aplicación...")
print(f"[INIT] Directorio de trabajo: {os.getcwd()}")
print(f"[INIT] Archivos en directorio: {os.listdir('.')}")
selector = AlgorithmSelector()
print("[INIT] Generando archivo combinado...")
generar_archivo_combinado()
print("[INIT] Inicializando analizador...")
analyzer = CyberbullyingAnalyzer(patterns_file=COMBINED_FILE)
print("[INIT] Cargando patrones...")
analyzer.load_patterns()
print(f"[INIT] Patrones cargados en analyzer: {len(analyzer.patterns) if hasattr(analyzer, 'patterns') else 'No disponible'}")
print("[INIT] Inicialización completada.")
# Cargar patrones desde el archivo combinado
@app.route('/')
def index():
"""Redirige a la página principal del frontend."""
return send_from_directory('templates', 'index.html')
@app.route('/index.html')
def index_page():
"""Sirve la página principal."""
return send_from_directory('templates', 'index.html')
@app.route('/config.html')
def config_page():
"""Sirve la página de configuración."""
return send_from_directory('templates', 'config.html')
#Comentario XD
@app.route('/resultados.html')
def results_page():
"""Sirve la página de resultados."""
return send_from_directory('templates', 'resultados.html')
@app.route('/templates/<path:filename>')
def serve_templates(filename):
"""Sirve archivos de templates."""
return send_from_directory('templates', filename)
@app.route('/static/<path:filename>')
def serve_static(filename):
"""Sirve archivos estáticos."""
return send_from_directory('static', filename)
@app.route('/api/analyze', methods=['POST'])
def analyze_text():
"""
Analiza un texto para detectar ciberacoso.
"""
try:
data = request.get_json()
if not data or 'text' not in data:
return jsonify({
'success': False,
'error': 'Texto no proporcionado'
}), 400
text = data['text']
if not text.strip():
return jsonify({
'success': False,
'error': 'El texto no puede estar vacío'
}), 400
# ✅ Usar directamente el analizador sin recargar patrones
result = analyzer.analyze_text(text)
return jsonify({
'success': True,
'result': result
})
except Exception as e:
return jsonify({
'success': False,
'error': f'Error en el análisis: {str(e)}'
}), 500
@app.route('/api/analyze-pattern', methods=['POST'])
def analyze_pattern():
"""
Analiza las características de un patrón específico.
Request JSON:
{
"pattern": "patrón a analizar"
}
Response JSON:
{
"success": true,
"analysis": { ... análisis del patrón ... }
}
"""
try:
data = request.get_json()
if not data or 'pattern' not in data:
return jsonify({
'success': False,
'error': 'Patrón no proporcionado'
}), 400
pattern = data['pattern']
# Analizar el patrón
analysis = selector.analyze_pattern(pattern)
return jsonify({
'success': True,
'analysis': analysis
})
except Exception as e:
return jsonify({
'success': False,
'error': f'Error en el análisis del patrón: {str(e)}'
}), 500
@app.route('/api/search-pattern', methods=['POST'])
def search_pattern():
"""
Busca un patrón específico en un texto.
Request JSON:
{
"text": "Texto donde buscar",
"pattern": "Patrón a buscar"
}
Response JSON:
{
"success": true,
"result": { ... resultado de la búsqueda ... }
}
"""
try:
data = request.get_json()
if not data or 'text' not in data or 'pattern' not in data:
return jsonify({
'success': False,
'error': 'Texto y patrón son requeridos'
}), 400
text = data['text']
pattern = data['pattern']
if not text.strip() or not pattern.strip():
return jsonify({
'success': False,
'error': 'El texto y el patrón no pueden estar vacíos'
}), 400
# Buscar el patrón
result = selector.search_pattern(text, pattern)
return jsonify({
'success': True,
'result': result
})
except Exception as e:
return jsonify({
'success': False,
'error': f'Error en la búsqueda: {str(e)}'
}), 500
@app.route('/api/debug', methods=['GET'])
def debug_info():
"""Endpoint de debugging para ver el estado del sistema"""
try:
import os
debug_data = {
'working_directory': os.getcwd(),
'files_in_directory': os.listdir('.'),
'base_file_exists': os.path.exists(BASE_FILE),
'base_file_path': BASE_FILE,
'upload_folder_exists': os.path.exists(UPLOAD_FOLDER),
'combined_file_exists': os.path.exists(COMBINED_FILE),
'patterns_count': len(cargar_todos_los_patrones()),
'analyzer_patterns_count': len(analyzer.patterns) if hasattr(analyzer, 'patterns') else 'No disponible'
}
# Intentar leer las primeras líneas del archivo base
if os.path.exists(BASE_FILE):
try:
with open(BASE_FILE, 'r', encoding='utf-8') as f:
debug_data['base_file_first_lines'] = f.readlines()[:3]
except Exception as e:
debug_data['base_file_read_error'] = str(e)
return jsonify({'success': True, 'debug': debug_data})
except Exception as e:
return jsonify({'success': False, 'error': str(e)}), 500
@app.route('/api/patterns', methods=['GET'])
def get_patterns():
try:
print("[API] Llamando a get_patterns")
patterns = cargar_todos_los_patrones()
print(f"[API] Patrones obtenidos: {len(patterns)}")
if len(patterns) > 0:
print(f"[API] Primer patrón: {patterns[0]}")
return jsonify({'success': True, 'patterns': patterns})
except Exception as e:
print(f"[API ERROR] Error al obtener patrones: {str(e)}")
return jsonify({'success': False, 'error': f'Error al obtener patrones: {str(e)}'}), 500
@app.route('/api/patterns/reload', methods=['POST'])
def reload_patterns():
"""
Recarga los patrones desde el archivo CSV.
Response JSON:
{
"success": true,
"message": "Patrones recargados exitosamente",
"total_patterns": número_de_patrones
}
"""
try:
generar_archivo_combinado()
analyzer.load_patterns()
return jsonify({
'success': True,
'message': 'Patrones recargados exitosamente',
'total_patterns': len(analyzer.patterns)
})
except Exception as e:
return jsonify({
'success': False,
'error': f'Error al recargar patrones: {str(e)}'
}), 500
@app.route('/api/test', methods=['GET'])
def test_api():
"""
Endpoint de prueba para verificar que la API funciona.
"""
return jsonify({
'success': True,
'message': 'API de SafeText funcionando correctamente',
'version': '1.0.0',
'algorithms': ['KMP', 'Boyer-Moore'],
'total_patterns': len(analyzer.patterns)
})
@app.route('/api/recommendations', methods=['POST'])
def get_recommendations():
"""
Endpoint para obtener recomendaciones personalizadas basadas en el análisis
"""
try:
data = request.get_json()
if not data:
return jsonify({
'success': False,
'error': 'No se proporcionaron datos de análisis'
}), 400
# Validar que tenga la estructura básica del análisis
if 'is_cyberbullying' not in data:
return jsonify({
'success': False,
'error': 'Datos de análisis incompletos'
}), 400
# Generar recomendaciones usando el motor de recomendaciones
recommendations_result = get_recommendations_for_analysis(data)
if recommendations_result['success']:
return jsonify({
'success': True,
'recommendation': recommendations_result['recommendation'],
'generated_at': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
})
else:
return jsonify({
'success': False,
'error': recommendations_result.get('error', 'Error generando recomendaciones'),
'fallback_recommendation': recommendations_result.get('recommendation')
}), 500
except Exception as e:
print(f"Error en /api/recommendations: {e}")
return jsonify({
'success': False,
'error': f'Error del servidor: {str(e)}'
}), 500
@app.errorhandler(404)
def not_found(error):
"""Maneja errores 404."""
return jsonify({
'success': False,
'error': 'Endpoint no encontrado'
}), 404
@app.errorhandler(500)
def internal_error(error):
"""Maneja errores 500."""
return jsonify({
'success': False,
'error': 'Error interno del servidor'
}), 500
@app.route('/api/add-pattern', methods=['POST'])
def add_pattern():
"""
Añade un nuevo patrón al archivo base y recarga los patrones en memoria.
Espera un JSON con:
{
"id": "11",
"frase": "nuevo patrón",
"categorias": "Insulto",
"nivel_gravedad": 50,
"descripcion": "Descripción del patrón"
}
"""
try:
data = request.get_json()
# Validar campos requeridos
required = ['id', 'frase', 'categorias', 'nivel_gravedad', 'descripcion']
for campo in required:
if campo not in data or data[campo] == '':
return jsonify({'success': False, 'error': f'Campo faltante o vacío: {campo}'}), 400
# Añadir al archivo base
with open(BASE_FILE, 'a', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow([
data['id'],
data['frase'],
data['categorias'],
data['nivel_gravedad'],
data['descripcion']
])
# Recargar en memoria
generar_archivo_combinado()
analyzer.load_patterns() # actualizar archivo combinado
return jsonify({'success': True, 'message': 'Patrón añadido correctamente', 'total_patterns': len(analyzer.patterns)})
except Exception as e:
return jsonify({'success': False, 'error': f'Error al añadir patrón: {str(e)}'}), 500
if __name__ == '__main__':
print("=" * 60)
print("SafeText - Sistema de Detección de Ciberacoso")
print("API Backend con algoritmos KMP y Boyer-Moore")
print("Desarrollado por Harry Guajan y Joel Tinitana")
print("EPN - Estructura de Datos y Algoritmos II")
print("=" * 60)
# Cargar patrones al iniciar
print(f"Patrones cargados: {len(analyzer.patterns)}")
# Iniciar servidor
print("Iniciando servidor en http://localhost:5000")
app.run(debug=True, host='0.0.0.0', port=5000)