File size: 25,111 Bytes
2ac7a3f 4f0de92 2ac7a3f 4f0de92 2ac7a3f 41ee13b 2ac7a3f a531b8b e6d4827 4f0de92 2ac7a3f e6d4827 4f0de92 e6d4827 2ac7a3f 4f0de92 a531b8b 2ac7a3f 4f0de92 2ac7a3f a9f1889 2ac7a3f e6d4827 2ac7a3f 4f0de92 2ac7a3f a531b8b e6d4827 4f0de92 e6d4827 2ac7a3f 4f0de92 e6d4827 2ac7a3f 4f0de92 2ac7a3f 4f0de92 e6d4827 4f0de92 e6d4827 a531b8b e6d4827 a531b8b e6d4827 a531b8b e6d4827 a531b8b e6d4827 a531b8b e6d4827 a531b8b e6d4827 a531b8b 2ac7a3f 41ee13b 88129b8 41ee13b 4f0de92 88129b8 41ee13b 4f0de92 9d876a4 88129b8 9d876a4 88129b8 9d876a4 88129b8 9d876a4 88129b8 9d876a4 88129b8 41ee13b 4f0de92 41ee13b 88129b8 41ee13b 2ac7a3f 4f0de92 41ee13b 88129b8 2ac7a3f 88129b8 41ee13b 88129b8 41ee13b 88129b8 41ee13b 88129b8 41ee13b 88129b8 2ac7a3f 41ee13b 88129b8 41ee13b 88129b8 41ee13b 88129b8 41ee13b 88129b8 41ee13b 4f0de92 41ee13b 2ac7a3f 88129b8 e6d4827 2ac7a3f e6d4827 a531b8b e6d4827 a531b8b e6d4827 a531b8b e6d4827 2ac7a3f a531b8b 2ac7a3f e6d4827 2ac7a3f e6d4827 2ac7a3f e6d4827 2ac7a3f e6d4827 4f0de92 2ac7a3f 4f0de92 e6d4827 4f0de92 e6d4827 2ac7a3f e6d4827 2ac7a3f e5683be 1a9e071 e5683be 1a9e071 e5683be 1a9e071 e5683be e6d4827 4f0de92 |
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 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 |
from flask import Flask, render_template, request, jsonify, session, send_file
from werkzeug.utils import secure_filename
import os
from google import genai
from google.genai import types
import io
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.lib.colors import navy, black
from datetime import datetime
import secrets
import re
import sqlite3
import threading
import time
import uuid
app = Flask(__name__)
app.secret_key = secrets.token_hex(16)
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['MAX_CONTENT_LENGTH'] = 50 * 1024 * 1024 # 50MB max
app.config['DATABASE'] = 'tasks.db'
# Créer les dossiers nécessaires
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
# Configuration Gemini
GEMINI_API_KEY = os.environ.get('GEMINI_API_KEY', 'YOUR_API_KEY_HERE')
client = genai.Client(api_key=GEMINI_API_KEY)
MAX_VIDEO_TOKENS = 600000
ALLOWED_EXTENSIONS = {
'pdf': 'application/pdf',
'mp3': 'audio/mp3',
'mp4': 'video/mp4',
'wav': 'audio/wav',
'txt': 'text/plain',
'doc': 'application/msword',
'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
}
SUMMARY_TYPES = {
'court': 'Fais un résumé très court (2-3 paragraphes maximum) des points clés essentiels en français.',
'moyen': 'Fais un résumé détaillé structuré avec les points principaux et sous-points importants en français.',
'detaille': 'Fais un résumé exhaustif et détaillé avec tous les points importants, citations clés et analyse approfondie en français'
}
# =======================
# Base de données
# =======================
def init_db():
"""Initialise la base de données"""
conn = sqlite3.connect(app.config['DATABASE'])
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS tasks (
task_id TEXT PRIMARY KEY,
user_session TEXT,
filename TEXT,
summary_type TEXT,
status TEXT,
progress INTEGER,
summary TEXT,
error TEXT,
created_at TEXT,
completed_at TEXT,
source_type TEXT,
source_path TEXT
)
''')
conn.commit()
conn.close()
def get_db_connection():
"""Retourne une connexion à la base de données"""
conn = sqlite3.connect(app.config['DATABASE'])
conn.row_factory = sqlite3.Row
return conn
def create_task(user_session, filename, summary_type, source_type, source_path):
"""Crée une nouvelle tâche dans la base de données"""
task_id = str(uuid.uuid4())
conn = get_db_connection()
conn.execute('''
INSERT INTO tasks (task_id, user_session, filename, summary_type, status,
progress, created_at, source_type, source_path)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (task_id, user_session, filename, summary_type, 'pending', 0,
datetime.now().isoformat(), source_type, source_path))
conn.commit()
conn.close()
return task_id
def update_task_status(task_id, status, progress=None, summary=None, error=None):
"""Met à jour le statut d'une tâche"""
conn = get_db_connection()
if status == 'completed' or status == 'failed':
if progress is None:
progress = 100 if status == 'completed' else 0
conn.execute('''
UPDATE tasks
SET status = ?, progress = ?, summary = ?, error = ?, completed_at = ?
WHERE task_id = ?
''', (status, progress, summary, error, datetime.now().isoformat(), task_id))
else:
query = 'UPDATE tasks SET status = ?'
params = [status]
if progress is not None:
query += ', progress = ?'
params.append(progress)
query += ' WHERE task_id = ?'
params.append(task_id)
conn.execute(query, params)
conn.commit()
conn.close()
def get_task(task_id):
"""Récupère une tâche par son ID"""
conn = get_db_connection()
task = conn.execute('SELECT * FROM tasks WHERE task_id = ?', (task_id,)).fetchone()
conn.close()
return task
def get_user_tasks(user_session):
"""Récupère toutes les tâches d'un utilisateur"""
conn = get_db_connection()
tasks = conn.execute(
'SELECT * FROM tasks WHERE user_session = ? ORDER BY created_at DESC',
(user_session,)
).fetchall()
conn.close()
return tasks
def delete_old_tasks():
"""Supprime les tâches de plus de 7 jours"""
conn = get_db_connection()
seven_days_ago = datetime.now().timestamp() - (7 * 24 * 60 * 60)
conn.execute('''
DELETE FROM tasks
WHERE datetime(created_at) < datetime(?, 'unixepoch')
''', (seven_days_ago,))
conn.commit()
conn.close()
# =======================
# Fonctions utilitaires
# =======================
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def get_mime_type(filename):
ext = filename.rsplit('.', 1)[1].lower()
return ALLOWED_EXTENSIONS.get(ext, 'application/octet-stream')
def is_youtube_url(url):
"""Vérifie si l'URL est une URL YouTube valide"""
youtube_regex = r'(https?://)?(www\.)?(youtube\.com/watch\?v=|youtu\.be/)[\w-]+'
return bool(re.match(youtube_regex, url))
def get_session_id():
"""Récupère ou crée un ID de session"""
if 'session_id' not in session:
session['session_id'] = str(uuid.uuid4())
return session['session_id']
# =======================
# Traitement en arrière-plan
# =======================
def process_file_background(task_id, file_path, filename, summary_type):
"""Traite un fichier en arrière-plan"""
try:
update_task_status(task_id, 'processing', progress=10)
mime_type = get_mime_type(filename)
prompt = SUMMARY_TYPES.get(summary_type, SUMMARY_TYPES['moyen'])
update_task_status(task_id, 'processing', progress=30)
# Upload le fichier
uploaded_file = client.files.upload(file=file_path)
update_task_status(task_id, 'processing', progress=60)
# Génère le résumé
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=[uploaded_file, prompt]
)
summary = response.text
update_task_status(task_id, 'completed', progress=100, summary=summary)
except Exception as e:
update_task_status(task_id, 'failed', error=str(e))
finally:
# Nettoie le fichier
if os.path.exists(file_path):
try:
os.remove(file_path)
except:
pass
def process_youtube_background(task_id, youtube_url, summary_type):
"""Traite une vidéo YouTube en arrière-plan"""
try:
update_task_status(task_id, 'processing', progress=10)
prompt = SUMMARY_TYPES.get(summary_type, SUMMARY_TYPES['moyen'])
update_task_status(task_id, 'processing', progress=30)
# Vérifier les informations du modèle
model_info = client.models.get(model="gemini-2.5-flash")
context_window = model_info.input_token_limit
update_task_status(task_id, 'processing', progress=50)
# Générer le contenu
response = client.models.generate_content(
model='gemini-2.5-flash',
contents=types.Content(
parts=[
types.Part(
file_data=types.FileData(file_uri=youtube_url)
),
types.Part(text=prompt)
]
)
)
summary = response.text
update_task_status(task_id, 'completed', progress=100, summary=summary)
except Exception as e:
error_msg = str(e)
if 'token' in error_msg.lower() or 'too large' in error_msg.lower():
error_msg = f"La vidéo est trop longue (dépasse la limite de {MAX_VIDEO_TOKENS:,} tokens). Veuillez utiliser une vidéo plus courte."
update_task_status(task_id, 'failed', error=error_msg)
# =======================
# Fonction PDF
# =======================
def create_pdf(summary_text, original_filename, summary_type):
"""
Crée un PDF du résumé avec un formatage amélioré qui interprète
la structure (titres, sous-titres, listes) du texte avec support markdown.
"""
buffer = io.BytesIO()
# Fonction pour ajouter le pied de page avec pagination
def add_page_number(canvas, doc):
canvas.saveState()
page_num = canvas.getPageNumber()
text = f"Page {page_num}"
canvas.setFont('Helvetica', 9)
canvas.setFillColor(black)
canvas.drawRightString(letter[0] - inch, 0.75 * inch, text)
# Ajouter le nom du fichier en bas à gauche
canvas.drawString(inch, 0.75 * inch, clean_filename[:50])
canvas.restoreState()
doc = SimpleDocTemplate(buffer, pagesize=letter,
leftMargin=inch, rightMargin=inch,
topMargin=inch, bottomMargin=inch)
# 1. Définir des styles de paragraphes personnalisés
styles = getSampleStyleSheet()
# Check if styles already exist before adding them
if 'MainTitle' not in styles:
styles.add(ParagraphStyle(
name='MainTitle',
parent=styles['h1'],
fontSize=20,
spaceAfter=20,
textColor=navy,
fontName='Helvetica-Bold',
alignment=1 # Centré
))
if 'SectionTitle' not in styles:
styles.add(ParagraphStyle(
name='SectionTitle',
parent=styles['h2'],
fontSize=14,
spaceAfter=12,
spaceBefore=12,
textColor=navy,
fontName='Helvetica-Bold'
))
if 'SubSectionTitle' not in styles:
styles.add(ParagraphStyle(
name='SubSectionTitle',
parent=styles['Normal'],
fontSize=12,
spaceAfter=8,
spaceBefore=8,
fontName='Helvetica-Bold'
))
if 'BodyText' not in styles:
styles.add(ParagraphStyle(
name='BodyText',
parent=styles['Normal'],
spaceAfter=10,
leading=16,
alignment=4, # Justifié
fontSize=11
))
if 'ListItem' not in styles:
styles.add(ParagraphStyle(
name='ListItem',
parent=styles['Normal'],
leftIndent=20,
spaceAfter=6,
leading=15,
fontSize=11
))
if 'BulletItem' not in styles:
styles.add(ParagraphStyle(
name='BulletItem',
parent=styles['Normal'],
leftIndent=25,
spaceAfter=6,
leading=15,
fontSize=11,
bulletIndent=10
))
if 'MetaInfo' not in styles:
styles.add(ParagraphStyle(
name='MetaInfo',
parent=styles['Normal'],
fontSize=10,
textColor=black,
spaceAfter=5,
alignment=1 # Centré
))
story = []
# 2. Nettoyer le nom du fichier pour éviter les doubles extensions ".pdf.pdf"
if original_filename and original_filename.lower().endswith('.pdf'):
clean_filename = original_filename[:-4]
else:
clean_filename = original_filename or "Document"
# 3. Ajouter le titre principal et les métadonnées du document
title = Paragraph(f"Résumé : {clean_filename}", styles['MainTitle'])
story.append(title)
info_text = f"Type de résumé : {summary_type.capitalize()} | Date : {datetime.now().strftime('%d/%m/%Y %H:%M')}"
info = Paragraph(info_text, styles['MetaInfo'])
story.append(info)
story.append(Spacer(1, 0.4 * inch))
# 4. Fonction pour convertir le markdown en HTML ReportLab
def convert_markdown_to_html(text):
"""Convertit le markdown simple en HTML pour ReportLab"""
# Échapper les caractères XML spéciaux d'abord
text = text.replace('&', '&').replace('<', '<').replace('>', '>')
# Convertir ***texte*** en gras+italique
text = re.sub(r'\*\*\*(.+?)\*\*\*', r'<b><i>\1</i></b>', text)
# Convertir **texte** en gras
text = re.sub(r'\*\*(.+?)\*\*', r'<b>\1</b>', text)
# Convertir *texte* en italique
text = re.sub(r'\*(.+?)\*', r'<i>\1</i>', text)
return text
# 5. Parcourir le résumé ligne par ligne et appliquer le style approprié
lines = summary_text.split('\n')
for i, line in enumerate(lines):
stripped_line = line.strip()
# Ignorer les lignes vides
if not stripped_line:
# Ajouter un petit espacement pour les lignes vides entre paragraphes
if i > 0 and i < len(lines) - 1:
story.append(Spacer(1, 0.1 * inch))
continue
# Gérer les lignes de séparation (---, ___, ***)
if stripped_line in ['---', '___', '***', '---', '___']:
story.append(Spacer(1, 0.15 * inch))
continue
# Détecter les titres de section : # Titre ou ## Titre
match_header = re.match(r'^(#{1,3})\s+(.+)$', stripped_line)
if match_header:
level = len(match_header.group(1))
content = convert_markdown_to_html(match_header.group(2))
if level == 1:
p = Paragraph(content, styles['SectionTitle'])
else:
p = Paragraph(content, styles['SubSectionTitle'])
story.append(p)
continue
# Détecter les titres en gras seul : **Titre Complet**
match_bold_title = re.match(r'^\*\*([^*]+)\*\*$', stripped_line)
if match_bold_title and len(match_bold_title.group(1)) < 100:
content = match_bold_title.group(1).strip()
# Si c'est court, c'est probablement un titre
if len(content.split()) < 15:
p = Paragraph(content, styles['SectionTitle'])
story.append(p)
continue
# Détecter les listes à puces : * item ou - item
match_bullet = re.match(r'^[\*\-]\s+(.+)$', stripped_line)
if match_bullet:
content = convert_markdown_to_html(match_bullet.group(1))
p = Paragraph(f"• {content}", styles['BulletItem'])
story.append(p)
continue
# Détecter les listes numérotées : 1. item ou 1) item
match_numbered = re.match(r'^(\d+)[\.\)]\s+(.+)$', stripped_line)
if match_numbered:
num = match_numbered.group(1)
content = convert_markdown_to_html(match_numbered.group(2))
p = Paragraph(f"{num}. {content}", styles['ListItem'])
story.append(p)
continue
# Pour tout le reste, utiliser le style de corps de texte avec conversion markdown
content = convert_markdown_to_html(stripped_line)
p = Paragraph(content, styles['BodyText'])
story.append(p)
# Construire le document PDF avec pagination
doc.build(story, onFirstPage=add_page_number, onLaterPages=add_page_number)
# Remettre le curseur au début du buffer pour la lecture
buffer.seek(0)
return buffer
# =======================
# Routes
# =======================
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload_file():
user_session = get_session_id()
youtube_url = request.form.get('youtube_url', '').strip()
summary_type = request.form.get('summary_type', 'moyen')
# Traitement YouTube
if youtube_url:
if not is_youtube_url(youtube_url):
return jsonify({'error': 'URL YouTube invalide'}), 400
# Créer la tâche
task_id = create_task(
user_session=user_session,
filename='Vidéo YouTube',
summary_type=summary_type,
source_type='youtube',
source_path=youtube_url
)
# Lancer le traitement en arrière-plan
thread = threading.Thread(
target=process_youtube_background,
args=(task_id, youtube_url, summary_type)
)
thread.daemon = True
thread.start()
return jsonify({
'success': True,
'task_id': task_id,
'message': 'Traitement en cours...'
})
# Traitement fichier
if 'file' not in request.files:
return jsonify({'error': 'Aucun fichier ou URL YouTube fourni'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'Nom de fichier vide'}), 400
if not allowed_file(file.filename):
return jsonify({'error': 'Type de fichier non supporté'}), 400
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], f"{uuid.uuid4()}_{filename}")
file.save(filepath)
# Créer la tâche
task_id = create_task(
user_session=user_session,
filename=filename,
summary_type=summary_type,
source_type='file',
source_path=filepath
)
# Lancer le traitement en arrière-plan
thread = threading.Thread(
target=process_file_background,
args=(task_id, filepath, filename, summary_type)
)
thread.daemon = True
thread.start()
return jsonify({
'success': True,
'task_id': task_id,
'message': 'Traitement en cours...'
})
@app.route('/task/<task_id>')
def get_task_status(task_id):
"""Récupère le statut d'une tâche"""
task = get_task(task_id)
if not task:
return jsonify({'error': 'Tâche non trouvée'}), 404
return jsonify({
'task_id': task['task_id'],
'filename': task['filename'],
'summary_type': task['summary_type'],
'status': task['status'],
'progress': task['progress'],
'summary': task['summary'],
'error': task['error'],
'created_at': task['created_at'],
'completed_at': task['completed_at']
})
@app.route('/tasks')
def get_all_tasks():
"""Récupère toutes les tâches de l'utilisateur"""
user_session = get_session_id()
tasks = get_user_tasks(user_session)
task_list = []
for task in tasks:
task_list.append({
'task_id': task['task_id'],
'filename': task['filename'],
'summary_type': task['summary_type'],
'status': task['status'],
'progress': task['progress'],
'summary': task['summary'],
'error': task['error'],
'created_at': task['created_at'],
'completed_at': task['completed_at']
})
return jsonify(task_list)
@app.route('/download/<task_id>')
def download_pdf(task_id):
"""Télécharge le PDF d'une tâche"""
task = get_task(task_id)
if not task:
return jsonify({'error': 'Tâche non trouvée'}), 404
if task['status'] != 'completed':
return jsonify({'error': 'Résumé non encore disponible'}), 400
pdf_buffer = create_pdf(task['summary'], task['filename'], task['summary_type'])
return send_file(
pdf_buffer,
mimetype='application/pdf',
as_attachment=True,
download_name=f"resume_{task['filename']}.pdf"
)
@app.route('/delete-task/<task_id>', methods=['DELETE'])
def delete_task(task_id):
"""Supprime une tâche"""
user_session = get_session_id()
task = get_task(task_id)
if not task:
return jsonify({'error': 'Tâche non trouvée'}), 404
if task['user_session'] != user_session:
return jsonify({'error': 'Non autorisé'}), 403
conn = get_db_connection()
conn.execute('DELETE FROM tasks WHERE task_id = ?', (task_id,))
conn.commit()
conn.close()
return jsonify({'success': True})
@app.route('/clear-tasks', methods=['POST'])
def clear_all_tasks():
"""Supprime toutes les tâches de l'utilisateur"""
user_session = get_session_id()
conn = get_db_connection()
conn.execute('DELETE FROM tasks WHERE user_session = ?', (user_session,))
conn.commit()
conn.close()
return jsonify({'success': True})
# Ajouter cette route dans votre fichier app.py
# Ajouter cette route dans votre fichier app.py
@app.route('/stats')
def system_stats():
"""Affiche les statistiques du système"""
conn = get_db_connection()
# Nombre total de résumés
total = conn.execute('SELECT COUNT(*) as count FROM tasks').fetchone()['count']
# Nombre de résumés réussis
completed = conn.execute(
'SELECT COUNT(*) as count FROM tasks WHERE status = ?',
('completed',)
).fetchone()['count']
# Nombre de résumés en cours
processing = conn.execute(
'SELECT COUNT(*) as count FROM tasks WHERE status IN (?, ?)',
('pending', 'processing')
).fetchone()['count']
# Nombre de résumés échoués
failed = conn.execute(
'SELECT COUNT(*) as count FROM tasks WHERE status = ?',
('failed',)
).fetchone()['count']
# Statistiques par type de résumé
by_type = conn.execute('''
SELECT summary_type, COUNT(*) as count
FROM tasks
WHERE status = 'completed'
GROUP BY summary_type
''').fetchall()
# Statistiques par source
by_source = conn.execute('''
SELECT source_type, COUNT(*) as count
FROM tasks
WHERE status = 'completed'
GROUP BY source_type
''').fetchall()
# Récupérer tous les résumés complétés (limité aux 50 derniers)
all_summaries = conn.execute('''
SELECT task_id, filename, summary_type, summary, created_at, completed_at, source_type
FROM tasks
WHERE status = 'completed'
ORDER BY completed_at DESC
LIMIT 50
''').fetchall()
conn.close()
# Créer le HTML minimaliste
html = f'''<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Statistiques Système</title>
</head>
<body>
<h1>Statistiques du Système</h1>
<h2>Vue d'ensemble</h2>
<ul>
<li>Total de résumés : {total}</li>
<li>Résumés réussis : {completed}</li>
<li>En cours : {processing}</li>
<li>Échoués : {failed}</li>
</ul>
<h2>Par type de résumé</h2>
<ul>
'''
for row in by_type:
html += f' <li>{row["summary_type"].capitalize()} : {row["count"]}</li>\n'
if not by_type:
html += ' <li>Aucune donnée</li>\n'
html += ''' </ul>
<h2>Par source</h2>
<ul>
'''
for row in by_source:
source_name = 'Fichier' if row['source_type'] == 'file' else 'YouTube'
html += f' <li>{source_name} : {row["count"]}</li>\n'
if not by_source:
html += ' <li>Aucune donnée</li>\n'
html += ''' </ul>
<hr>
<h2>Résumés des utilisateurs (50 derniers)</h2>
'''
if not all_summaries:
html += ' <p>Aucun résumé disponible</p>\n'
else:
for i, summary in enumerate(all_summaries, 1):
source_name = 'Fichier' if summary['source_type'] == 'file' else 'YouTube'
created = summary['created_at'][:19] if summary['created_at'] else 'N/A'
completed = summary['completed_at'][:19] if summary['completed_at'] else 'N/A'
html += f'''
<h3>Résumé #{i}</h3>
<ul>
<li>Fichier : {summary["filename"]}</li>
<li>Type : {summary["summary_type"].capitalize()}</li>
<li>Source : {source_name}</li>
<li>Créé le : {created}</li>
<li>Complété le : {completed}</li>
</ul>
<p><strong>Résumé :</strong></p>
<pre>{summary["summary"]}</pre>
<hr>
'''
html += '''
<p><a href="/">Retour à l'accueil</a></p>
</body>
</html>'''
return html
# =======================
# Initialisation
# =======================
# Initialiser la base de données au démarrage
with app.app_context():
init_db()
# Nettoyage périodique des anciennes tâches
def cleanup_old_tasks():
while True:
time.sleep(86400) # Toutes les 24 heures
delete_old_tasks()
cleanup_thread = threading.Thread(target=cleanup_old_tasks)
cleanup_thread.daemon = True
cleanup_thread.start()
if __name__ == '__main__':
app.run(debug=True) |