Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,753 +1,748 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import re
|
| 3 |
-
import csv
|
| 4 |
-
import secrets
|
| 5 |
-
import unicodedata
|
| 6 |
-
from datetime import datetime, timedelta
|
| 7 |
-
from io import BytesIO
|
| 8 |
-
|
| 9 |
-
from flask import (
|
| 10 |
-
Flask,
|
| 11 |
-
render_template,
|
| 12 |
-
request,
|
| 13 |
-
redirect,
|
| 14 |
-
url_for,
|
| 15 |
-
send_file,
|
| 16 |
-
flash,
|
| 17 |
-
session,
|
| 18 |
-
abort,
|
| 19 |
-
)
|
| 20 |
-
from flask_sqlalchemy import SQLAlchemy
|
| 21 |
-
from flask_login import (
|
| 22 |
-
LoginManager,
|
| 23 |
-
UserMixin,
|
| 24 |
-
login_user,
|
| 25 |
-
login_required,
|
| 26 |
-
current_user,
|
| 27 |
-
logout_user,
|
| 28 |
-
)
|
| 29 |
-
from werkzeug.security import generate_password_hash, check_password_hash
|
| 30 |
-
from reportlab.pdfgen import canvas
|
| 31 |
-
from reportlab.lib.pagesizes import letter
|
| 32 |
-
|
| 33 |
-
from sendgrid import SendGridAPIClient
|
| 34 |
-
from sendgrid.helpers.mail import Mail
|
| 35 |
-
|
| 36 |
-
# your HF-based classifier
|
| 37 |
-
from model import classify_tone_rich
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
# =========================================================
|
| 41 |
-
# APP CONFIG
|
| 42 |
-
# =========================================================
|
| 43 |
-
|
| 44 |
-
app = Flask(__name__)
|
| 45 |
-
|
| 46 |
-
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY", "change-this-in-prod")
|
| 47 |
-
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///data.db"
|
| 48 |
-
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
| 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 |
-
value
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
return
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
def
|
| 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 |
-
if
|
| 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 |
-
def
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
return
|
| 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 |
-
<p>
|
| 227 |
-
<p>
|
| 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 |
-
user.
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
session
|
| 333 |
-
flash("
|
| 334 |
-
return redirect(url_for("
|
| 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 |
-
if
|
| 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 |
-
e.
|
| 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 |
-
c.setFont("Helvetica",
|
| 646 |
-
c.
|
| 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 |
-
flash("
|
| 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 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
import csv
|
| 4 |
+
import secrets
|
| 5 |
+
import unicodedata
|
| 6 |
+
from datetime import datetime, timedelta
|
| 7 |
+
from io import BytesIO
|
| 8 |
+
|
| 9 |
+
from flask import (
|
| 10 |
+
Flask,
|
| 11 |
+
render_template,
|
| 12 |
+
request,
|
| 13 |
+
redirect,
|
| 14 |
+
url_for,
|
| 15 |
+
send_file,
|
| 16 |
+
flash,
|
| 17 |
+
session,
|
| 18 |
+
abort,
|
| 19 |
+
)
|
| 20 |
+
from flask_sqlalchemy import SQLAlchemy
|
| 21 |
+
from flask_login import (
|
| 22 |
+
LoginManager,
|
| 23 |
+
UserMixin,
|
| 24 |
+
login_user,
|
| 25 |
+
login_required,
|
| 26 |
+
current_user,
|
| 27 |
+
logout_user,
|
| 28 |
+
)
|
| 29 |
+
from werkzeug.security import generate_password_hash, check_password_hash
|
| 30 |
+
from reportlab.pdfgen import canvas
|
| 31 |
+
from reportlab.lib.pagesizes import letter
|
| 32 |
+
|
| 33 |
+
from sendgrid import SendGridAPIClient
|
| 34 |
+
from sendgrid.helpers.mail import Mail
|
| 35 |
+
|
| 36 |
+
# your HF-based classifier
|
| 37 |
+
from model import classify_tone_rich
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# =========================================================
|
| 41 |
+
# APP CONFIG
|
| 42 |
+
# =========================================================
|
| 43 |
+
|
| 44 |
+
app = Flask(__name__)
|
| 45 |
+
|
| 46 |
+
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY", "change-this-in-prod")
|
| 47 |
+
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///data.db"
|
| 48 |
+
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
| 49 |
+
|
| 50 |
+
# Detect if running on Hugging Face Spaces (they set SPACE_ID)
|
| 51 |
+
IS_HF = os.getenv("SPACE_ID") is not None
|
| 52 |
+
|
| 53 |
+
# cookie security
|
| 54 |
+
app.config["SESSION_COOKIE_HTTPONLY"] = True
|
| 55 |
+
|
| 56 |
+
if IS_HF:
|
| 57 |
+
# Needed because app runs inside an iframe on huggingface.co
|
| 58 |
+
app.config["SESSION_COOKIE_SAMESITE"] = "None"
|
| 59 |
+
app.config["SESSION_COOKIE_SECURE"] = True
|
| 60 |
+
else:
|
| 61 |
+
# Local dev / normal hosting
|
| 62 |
+
app.config["SESSION_COOKIE_SAMESITE"] = "Lax"
|
| 63 |
+
app.config["SESSION_COOKIE_SECURE"] = False
|
| 64 |
+
|
| 65 |
+
db = SQLAlchemy(app)
|
| 66 |
+
login_manager = LoginManager(app)
|
| 67 |
+
login_manager.login_view = "login"
|
| 68 |
+
|
| 69 |
+
# Email (SendGrid)
|
| 70 |
+
SENDGRID_API_KEY = os.getenv("SENDGRID_API_KEY")
|
| 71 |
+
SENDER_EMAIL = os.getenv("SENDER_EMAIL", "no-reply@example.com")
|
| 72 |
+
|
| 73 |
+
# simple email regex
|
| 74 |
+
EMAIL_RE = re.compile(r"^[^@\s]+@[^@\s]+\.[^@\s]+$")
|
| 75 |
+
|
| 76 |
+
os.makedirs("exports", exist_ok=True)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
# =========================================================
|
| 80 |
+
# HELPER FUNCTIONS – SANITIZATION, CSRF, PASSWORDS
|
| 81 |
+
# =========================================================
|
| 82 |
+
|
| 83 |
+
def normalize_text(value: str) -> str:
|
| 84 |
+
if not value:
|
| 85 |
+
return ""
|
| 86 |
+
value = unicodedata.normalize("NFKC", value)
|
| 87 |
+
value = value.replace("\u200b", "").replace("\u200c", "").replace("\u200d", "")
|
| 88 |
+
return value.strip()
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def sanitize_string(value: str, max_len: int = 255) -> str:
|
| 92 |
+
value = normalize_text(value)
|
| 93 |
+
if len(value) > max_len:
|
| 94 |
+
value = value[:max_len]
|
| 95 |
+
return value
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def sanitize_long_text(value: str, max_len: int = 4000) -> str:
|
| 99 |
+
value = normalize_text(value)
|
| 100 |
+
if len(value) > max_len:
|
| 101 |
+
value = value[:max_len]
|
| 102 |
+
return value
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def is_valid_email(email: str) -> bool:
|
| 106 |
+
return bool(email and EMAIL_RE.match(email))
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def is_strong_password(pw: str) -> bool:
|
| 110 |
+
if not pw or len(pw) < 8:
|
| 111 |
+
return False
|
| 112 |
+
has_letter = any(c.isalpha() for c in pw)
|
| 113 |
+
has_digit = any(c.isdigit() for c in pw)
|
| 114 |
+
return has_letter and has_digit
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def generate_code() -> str:
|
| 118 |
+
"""6-digit numeric code used for verify + reset."""
|
| 119 |
+
return f"{secrets.randbelow(1000000):06d}"
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
def generate_csrf_token() -> str:
|
| 123 |
+
token = session.get("csrf_token")
|
| 124 |
+
if not token:
|
| 125 |
+
token = secrets.token_hex(16)
|
| 126 |
+
session["csrf_token"] = token
|
| 127 |
+
return token
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
@app.before_request
|
| 131 |
+
def csrf_protect():
|
| 132 |
+
# ensure CSRF token exists
|
| 133 |
+
generate_csrf_token()
|
| 134 |
+
|
| 135 |
+
if request.method == "POST":
|
| 136 |
+
form_token = request.form.get("csrf_token", "")
|
| 137 |
+
sess_token = session.get("csrf_token", "")
|
| 138 |
+
if not form_token or form_token != sess_token:
|
| 139 |
+
abort(400, description="Invalid CSRF token")
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
@app.context_processor
|
| 143 |
+
def inject_csrf():
|
| 144 |
+
return {"csrf_token": session.get("csrf_token", "")}
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
# =========================================================
|
| 148 |
+
# MODELS
|
| 149 |
+
# =========================================================
|
| 150 |
+
|
| 151 |
+
class User(UserMixin, db.Model):
|
| 152 |
+
id = db.Column(db.Integer, primary_key=True)
|
| 153 |
+
email = db.Column(db.String(255), unique=True, nullable=False)
|
| 154 |
+
password_hash = db.Column(db.String(255), nullable=False)
|
| 155 |
+
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
| 156 |
+
|
| 157 |
+
# login security
|
| 158 |
+
failed_logins = db.Column(db.Integer, default=0)
|
| 159 |
+
lock_until = db.Column(db.DateTime, nullable=True)
|
| 160 |
+
|
| 161 |
+
# email verification
|
| 162 |
+
is_verified = db.Column(db.Boolean, default=False)
|
| 163 |
+
verification_code = db.Column(db.String(6), nullable=True)
|
| 164 |
+
verification_expires = db.Column(db.DateTime, nullable=True)
|
| 165 |
+
|
| 166 |
+
# password reset
|
| 167 |
+
reset_code = db.Column(db.String(6), nullable=True)
|
| 168 |
+
reset_expires = db.Column(db.DateTime, nullable=True)
|
| 169 |
+
|
| 170 |
+
# activity (for possible retention rules)
|
| 171 |
+
last_active_at = db.Column(db.DateTime, nullable=True)
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
class Entry(db.Model):
|
| 175 |
+
id = db.Column(db.Integer, primary_key=True)
|
| 176 |
+
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
| 177 |
+
text = db.Column(db.Text, nullable=False)
|
| 178 |
+
label = db.Column(db.String(32))
|
| 179 |
+
confidence = db.Column(db.Float)
|
| 180 |
+
severity = db.Column(db.Integer)
|
| 181 |
+
threat_score = db.Column(db.Integer)
|
| 182 |
+
politeness_score = db.Column(db.Integer)
|
| 183 |
+
friendly_score = db.Column(db.Integer)
|
| 184 |
+
has_threat = db.Column(db.Boolean, default=False)
|
| 185 |
+
has_profanity = db.Column(db.Boolean, default=False)
|
| 186 |
+
has_sarcasm = db.Column(db.Boolean, default=False)
|
| 187 |
+
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
|
| 188 |
+
|
| 189 |
+
user = db.relationship("User", backref="entries")
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
@login_manager.user_loader
|
| 193 |
+
def load_user(user_id):
|
| 194 |
+
try:
|
| 195 |
+
return User.query.get(int(user_id))
|
| 196 |
+
except Exception:
|
| 197 |
+
return None
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
# =========================================================
|
| 201 |
+
# EMAIL HELPERS
|
| 202 |
+
# =========================================================
|
| 203 |
+
|
| 204 |
+
def send_email(to_email: str, subject: str, html: str):
|
| 205 |
+
if not SENDGRID_API_KEY:
|
| 206 |
+
print("[WARN] SENDGRID_API_KEY not set. Skipping email send.")
|
| 207 |
+
print(f"Subject: {subject}\nTo: {to_email}\n{html}")
|
| 208 |
+
return
|
| 209 |
+
|
| 210 |
+
message = Mail(
|
| 211 |
+
from_email=SENDER_EMAIL,
|
| 212 |
+
to_emails=to_email,
|
| 213 |
+
subject=subject,
|
| 214 |
+
html_content=html,
|
| 215 |
+
)
|
| 216 |
+
try:
|
| 217 |
+
sg = SendGridAPIClient(SENDGRID_API_KEY)
|
| 218 |
+
sg.send(message)
|
| 219 |
+
print(f"[INFO] Sent email to {to_email}: {subject}")
|
| 220 |
+
except Exception as e:
|
| 221 |
+
print(f"[ERROR] Failed to send email to {to_email}: {e}")
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
def send_verification_email(to_email: str, code: str):
|
| 225 |
+
html = f"""
|
| 226 |
+
<p>Thanks for signing up for the AI Email Tone Classifier.</p>
|
| 227 |
+
<p>Your verification code is: <strong>{code}</strong></p>
|
| 228 |
+
<p>This code will expire in 15 minutes.</p>
|
| 229 |
+
"""
|
| 230 |
+
send_email(to_email, "Verify your email", html)
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
def send_password_reset_email(to_email: str, code: str):
|
| 234 |
+
html = f"""
|
| 235 |
+
<p>You requested to reset your password for the AI Email Tone Classifier.</p>
|
| 236 |
+
<p>Your password reset code is: <strong>{code}</strong></p>
|
| 237 |
+
<p>This code will expire in 15 minutes.</p>
|
| 238 |
+
<p>If you did not request this, you can ignore this email.</p>
|
| 239 |
+
"""
|
| 240 |
+
send_email(to_email, "Password reset code", html)
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
# =========================================================
|
| 244 |
+
# AUTH ROUTES: REGISTER / LOGIN / LOGOUT / VERIFY
|
| 245 |
+
# =========================================================
|
| 246 |
+
|
| 247 |
+
@app.route("/register", methods=["GET", "POST"])
|
| 248 |
+
def register():
|
| 249 |
+
if current_user.is_authenticated:
|
| 250 |
+
return redirect(url_for("index"))
|
| 251 |
+
|
| 252 |
+
if request.method == "POST":
|
| 253 |
+
email = sanitize_string(request.form.get("email", ""), 255).lower()
|
| 254 |
+
password = normalize_text(request.form.get("password", ""))
|
| 255 |
+
consent = request.form.get("consent_privacy") == "on"
|
| 256 |
+
|
| 257 |
+
if not email or not password:
|
| 258 |
+
flash("Email and password are required.", "error")
|
| 259 |
+
return redirect(url_for("register"))
|
| 260 |
+
|
| 261 |
+
if not is_valid_email(email):
|
| 262 |
+
flash("Please enter a valid email address.", "error")
|
| 263 |
+
return redirect(url_for("register"))
|
| 264 |
+
|
| 265 |
+
if not is_strong_password(password):
|
| 266 |
+
flash("Password must be at least 8 characters and contain letters and numbers.", "error")
|
| 267 |
+
return redirect(url_for("register"))
|
| 268 |
+
|
| 269 |
+
if not consent:
|
| 270 |
+
flash("You must agree to the Privacy Policy to create an account.", "error")
|
| 271 |
+
return redirect(url_for("register"))
|
| 272 |
+
|
| 273 |
+
existing = User.query.filter_by(email=email).first()
|
| 274 |
+
if existing:
|
| 275 |
+
flash("An account with that email already exists.", "error")
|
| 276 |
+
return redirect(url_for("register"))
|
| 277 |
+
|
| 278 |
+
user = User(
|
| 279 |
+
email=email,
|
| 280 |
+
password_hash=generate_password_hash(password),
|
| 281 |
+
last_active_at=datetime.utcnow(),
|
| 282 |
+
)
|
| 283 |
+
|
| 284 |
+
code = generate_code()
|
| 285 |
+
user.verification_code = code
|
| 286 |
+
user.verification_expires = datetime.utcnow() + timedelta(minutes=15)
|
| 287 |
+
user.is_verified = False
|
| 288 |
+
|
| 289 |
+
db.session.add(user)
|
| 290 |
+
db.session.commit()
|
| 291 |
+
|
| 292 |
+
send_verification_email(email, code)
|
| 293 |
+
session["pending_email"] = email
|
| 294 |
+
|
| 295 |
+
flash("Account created. Check your email for the verification code.", "success")
|
| 296 |
+
return redirect(url_for("verify"))
|
| 297 |
+
|
| 298 |
+
return render_template("login.html", mode="register", title="Register")
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
@app.route("/login", methods=["GET", "POST"])
|
| 302 |
+
def login():
|
| 303 |
+
if current_user.is_authenticated:
|
| 304 |
+
return redirect(url_for("index"))
|
| 305 |
+
|
| 306 |
+
if request.method == "POST":
|
| 307 |
+
email = sanitize_string(request.form.get("email", ""), 255).lower()
|
| 308 |
+
password = normalize_text(request.form.get("password", ""))
|
| 309 |
+
|
| 310 |
+
if not email or not password:
|
| 311 |
+
flash("Email and password are required.", "error")
|
| 312 |
+
return redirect(url_for("login"))
|
| 313 |
+
|
| 314 |
+
user = User.query.filter_by(email=email).first()
|
| 315 |
+
if not user:
|
| 316 |
+
flash("Invalid email or password.", "error")
|
| 317 |
+
return redirect(url_for("login"))
|
| 318 |
+
|
| 319 |
+
now = datetime.utcnow()
|
| 320 |
+
|
| 321 |
+
# lockout check
|
| 322 |
+
if user.lock_until and user.lock_until > now:
|
| 323 |
+
remaining = int((user.lock_until - now).total_seconds() // 60) + 1
|
| 324 |
+
flash(f"Account locked due to too many failed attempts. Try again in ~{remaining} minutes.", "error")
|
| 325 |
+
return redirect(url_for("login"))
|
| 326 |
+
|
| 327 |
+
if not check_password_hash(user.password_hash, password):
|
| 328 |
+
user.failed_logins = (user.failed_logins or 0) + 1
|
| 329 |
+
if user.failed_logins >= 5:
|
| 330 |
+
user.lock_until = now + timedelta(minutes=10)
|
| 331 |
+
user.failed_logins = 0
|
| 332 |
+
db.session.commit()
|
| 333 |
+
flash("Invalid email or password.", "error")
|
| 334 |
+
return redirect(url_for("login"))
|
| 335 |
+
|
| 336 |
+
# reset counters
|
| 337 |
+
user.failed_logins = 0
|
| 338 |
+
user.lock_until = None
|
| 339 |
+
user.last_active_at = now
|
| 340 |
+
db.session.commit()
|
| 341 |
+
|
| 342 |
+
if not user.is_verified:
|
| 343 |
+
session["pending_email"] = user.email
|
| 344 |
+
flash("Please verify your email before logging in.", "error")
|
| 345 |
+
return redirect(url_for("verify"))
|
| 346 |
+
|
| 347 |
+
login_user(user)
|
| 348 |
+
flash("Logged in successfully.", "success")
|
| 349 |
+
return redirect(url_for("index"))
|
| 350 |
+
|
| 351 |
+
return render_template("login.html", mode="login", title="Login")
|
| 352 |
+
|
| 353 |
+
|
| 354 |
+
@app.route("/logout")
|
| 355 |
+
@login_required
|
| 356 |
+
def logout():
|
| 357 |
+
logout_user()
|
| 358 |
+
flash("You have been logged out.", "success")
|
| 359 |
+
return redirect(url_for("login"))
|
| 360 |
+
|
| 361 |
+
|
| 362 |
+
@app.route("/verify", methods=["GET", "POST"])
|
| 363 |
+
def verify():
|
| 364 |
+
email = sanitize_string(
|
| 365 |
+
request.args.get("email", "") or session.get("pending_email", ""), 255
|
| 366 |
+
).lower()
|
| 367 |
+
|
| 368 |
+
if not email:
|
| 369 |
+
flash("No email specified for verification. Please register or log in again.", "error")
|
| 370 |
+
return redirect(url_for("register"))
|
| 371 |
+
|
| 372 |
+
user = User.query.filter_by(email=email).first()
|
| 373 |
+
if not user:
|
| 374 |
+
flash("Account not found. Please register again.", "error")
|
| 375 |
+
return redirect(url_for("register"))
|
| 376 |
+
|
| 377 |
+
if user.is_verified:
|
| 378 |
+
flash("Your email is already verified. You can log in.", "success")
|
| 379 |
+
return redirect(url_for("login"))
|
| 380 |
+
|
| 381 |
+
if request.method == "POST":
|
| 382 |
+
action = request.form.get("action", "verify")
|
| 383 |
+
|
| 384 |
+
if action == "resend":
|
| 385 |
+
code = generate_code()
|
| 386 |
+
user.verification_code = code
|
| 387 |
+
user.verification_expires = datetime.utcnow() + timedelta(minutes=15)
|
| 388 |
+
db.session.commit()
|
| 389 |
+
send_verification_email(user.email, code)
|
| 390 |
+
flash("A new verification code has been sent.", "success")
|
| 391 |
+
return redirect(url_for("verify", email=user.email))
|
| 392 |
+
|
| 393 |
+
code_input = sanitize_string(request.form.get("code", ""), 6)
|
| 394 |
+
|
| 395 |
+
if not code_input:
|
| 396 |
+
flash("Please enter the verification code.", "error")
|
| 397 |
+
return redirect(url_for("verify", email=user.email))
|
| 398 |
+
|
| 399 |
+
if not user.verification_code or not user.verification_expires:
|
| 400 |
+
flash("No active verification code. Please resend.", "error")
|
| 401 |
+
return redirect(url_for("verify", email=user.email))
|
| 402 |
+
|
| 403 |
+
if datetime.utcnow() > user.verification_expires:
|
| 404 |
+
flash("Verification code expired. Please request a new one.", "error")
|
| 405 |
+
return redirect(url_for("verify", email=user.email))
|
| 406 |
+
|
| 407 |
+
if code_input != user.verification_code:
|
| 408 |
+
flash("Invalid verification code.", "error")
|
| 409 |
+
return redirect(url_for("verify", email=user.email))
|
| 410 |
+
|
| 411 |
+
user.is_verified = True
|
| 412 |
+
user.verification_code = None
|
| 413 |
+
user.verification_expires = None
|
| 414 |
+
user.last_active_at = datetime.utcnow()
|
| 415 |
+
db.session.commit()
|
| 416 |
+
|
| 417 |
+
flash("Email verified successfully. You can now log in.", "success")
|
| 418 |
+
return redirect(url_for("login"))
|
| 419 |
+
|
| 420 |
+
session["pending_email"] = email
|
| 421 |
+
return render_template("verify.html", email=email, title="Verify Email")
|
| 422 |
+
|
| 423 |
+
|
| 424 |
+
# =========================================================
|
| 425 |
+
# FORGOT PASSWORD + RESET
|
| 426 |
+
# =========================================================
|
| 427 |
+
|
| 428 |
+
@app.route("/forgot", methods=["GET", "POST"])
|
| 429 |
+
def forgot_password():
|
| 430 |
+
if request.method == "POST":
|
| 431 |
+
email = sanitize_string(request.form.get("email", ""), 255).lower()
|
| 432 |
+
|
| 433 |
+
if not is_valid_email(email):
|
| 434 |
+
flash("If that email exists, a reset code has been sent. Check your email, then enter the code below.", "success")
|
| 435 |
+
return redirect(url_for("reset_password"))
|
| 436 |
+
|
| 437 |
+
user = User.query.filter_by(email=email).first()
|
| 438 |
+
if user:
|
| 439 |
+
code = generate_code()
|
| 440 |
+
user.reset_code = code
|
| 441 |
+
user.reset_expires = datetime.utcnow() + timedelta(minutes=15)
|
| 442 |
+
db.session.commit()
|
| 443 |
+
send_password_reset_email(user.email, code)
|
| 444 |
+
|
| 445 |
+
flash("If that email exists, a reset code has been sent. Check your email, then enter the code below.", "success")
|
| 446 |
+
return redirect(url_for("reset_password"))
|
| 447 |
+
|
| 448 |
+
return render_template("forgot.html", title="Forgot Password")
|
| 449 |
+
|
| 450 |
+
|
| 451 |
+
@app.route("/reset", methods=["GET", "POST"])
|
| 452 |
+
def reset_password():
|
| 453 |
+
if request.method == "POST":
|
| 454 |
+
email = sanitize_string(request.form.get("email", ""), 255).lower()
|
| 455 |
+
code_input = sanitize_string(request.form.get("code", ""), 6)
|
| 456 |
+
new_pw = normalize_text(request.form.get("password", ""))
|
| 457 |
+
confirm_pw = normalize_text(request.form.get("confirm_password", ""))
|
| 458 |
+
|
| 459 |
+
if not email or not code_input or not new_pw or not confirm_pw:
|
| 460 |
+
flash("All fields are required.", "error")
|
| 461 |
+
return redirect(url_for("reset_password"))
|
| 462 |
+
|
| 463 |
+
if new_pw != confirm_pw:
|
| 464 |
+
flash("Passwords do not match.", "error")
|
| 465 |
+
return redirect(url_for("reset_password"))
|
| 466 |
+
|
| 467 |
+
if not is_strong_password(new_pw):
|
| 468 |
+
flash("Password must be at least 8 characters and contain letters and numbers.", "error")
|
| 469 |
+
return redirect(url_for("reset_password"))
|
| 470 |
+
|
| 471 |
+
user = User.query.filter_by(email=email).first()
|
| 472 |
+
if (
|
| 473 |
+
not user
|
| 474 |
+
or not user.reset_code
|
| 475 |
+
or not user.reset_expires
|
| 476 |
+
or datetime.utcnow() > user.reset_expires
|
| 477 |
+
or code_input != user.reset_code
|
| 478 |
+
):
|
| 479 |
+
flash("Invalid or expired reset code.", "error")
|
| 480 |
+
return redirect(url_for("reset_password"))
|
| 481 |
+
|
| 482 |
+
user.password_hash = generate_password_hash(new_pw)
|
| 483 |
+
user.reset_code = None
|
| 484 |
+
user.reset_expires = None
|
| 485 |
+
db.session.commit()
|
| 486 |
+
|
| 487 |
+
flash("Password reset successfully. You can now log in.", "success")
|
| 488 |
+
return redirect(url_for("login"))
|
| 489 |
+
|
| 490 |
+
return render_template("reset_password.html", title="Reset Password")
|
| 491 |
+
|
| 492 |
+
|
| 493 |
+
# =========================================================
|
| 494 |
+
# MAIN CLASSIFIER
|
| 495 |
+
# =========================================================
|
| 496 |
+
|
| 497 |
+
@app.route("/", methods=["GET", "POST"])
|
| 498 |
+
@login_required
|
| 499 |
+
def index():
|
| 500 |
+
if not current_user.is_verified:
|
| 501 |
+
flash("Please verify your email to use the classifier.", "error")
|
| 502 |
+
return redirect(url_for("verify", email=current_user.email))
|
| 503 |
+
|
| 504 |
+
result = None # dict from classify_tone_rich
|
| 505 |
+
text = ""
|
| 506 |
+
|
| 507 |
+
if request.method == "POST":
|
| 508 |
+
text = sanitize_long_text(request.form.get("email_text", ""))
|
| 509 |
+
if text:
|
| 510 |
+
result = classify_tone_rich(text)
|
| 511 |
+
|
| 512 |
+
entry = Entry(
|
| 513 |
+
text=text,
|
| 514 |
+
label=result["label"],
|
| 515 |
+
confidence=float(result["confidence"]),
|
| 516 |
+
severity=int(result["severity"]),
|
| 517 |
+
threat_score=int(result["threat_score"]),
|
| 518 |
+
politeness_score=int(result["politeness_score"]),
|
| 519 |
+
friendly_score=int(result["friendly_score"]),
|
| 520 |
+
has_threat=bool(result["has_threat"]),
|
| 521 |
+
has_profanity=bool(result["has_profanity"]),
|
| 522 |
+
has_sarcasm=bool(result["has_sarcasm"]),
|
| 523 |
+
user_id=current_user.id,
|
| 524 |
+
)
|
| 525 |
+
db.session.add(entry)
|
| 526 |
+
db.session.commit()
|
| 527 |
+
|
| 528 |
+
return render_template(
|
| 529 |
+
"index.html",
|
| 530 |
+
title="Analyze Email",
|
| 531 |
+
email_text=text,
|
| 532 |
+
result=result,
|
| 533 |
+
)
|
| 534 |
+
|
| 535 |
+
|
| 536 |
+
# =========================================================
|
| 537 |
+
# HISTORY + EXPORTS
|
| 538 |
+
# =========================================================
|
| 539 |
+
|
| 540 |
+
@app.route("/history")
|
| 541 |
+
@login_required
|
| 542 |
+
def history_view():
|
| 543 |
+
if not current_user.is_verified:
|
| 544 |
+
flash("Please verify your email to view history.", "error")
|
| 545 |
+
return redirect(url_for("verify", email=current_user.email))
|
| 546 |
+
|
| 547 |
+
q = sanitize_string(request.args.get("q", ""), 255).lower()
|
| 548 |
+
filter_label = sanitize_string(request.args.get("label", ""), 32).lower()
|
| 549 |
+
|
| 550 |
+
query = Entry.query.filter_by(user_id=current_user.id)
|
| 551 |
+
|
| 552 |
+
if q:
|
| 553 |
+
query = query.filter(Entry.text.ilike(f"%{q}%"))
|
| 554 |
+
|
| 555 |
+
if filter_label:
|
| 556 |
+
query = query.filter(Entry.label.ilike(filter_label))
|
| 557 |
+
|
| 558 |
+
entries = query.order_by(Entry.created_at.desc()).all()
|
| 559 |
+
|
| 560 |
+
return render_template(
|
| 561 |
+
"history.html",
|
| 562 |
+
title="History",
|
| 563 |
+
history=entries,
|
| 564 |
+
search=q,
|
| 565 |
+
active_filter=filter_label,
|
| 566 |
+
)
|
| 567 |
+
|
| 568 |
+
|
| 569 |
+
@app.route("/export/csv")
|
| 570 |
+
@login_required
|
| 571 |
+
def export_csv():
|
| 572 |
+
if not current_user.is_verified:
|
| 573 |
+
flash("Please verify your email to export data.", "error")
|
| 574 |
+
return redirect(url_for("verify", email=current_user.email))
|
| 575 |
+
|
| 576 |
+
filepath = os.path.join("exports", f"history_{current_user.id}.csv")
|
| 577 |
+
entries = Entry.query.filter_by(user_id=current_user.id).order_by(Entry.created_at.asc())
|
| 578 |
+
|
| 579 |
+
with open(filepath, "w", newline="", encoding="utf-8") as f:
|
| 580 |
+
writer = csv.writer(f)
|
| 581 |
+
writer.writerow(
|
| 582 |
+
[
|
| 583 |
+
"Time UTC",
|
| 584 |
+
"Label",
|
| 585 |
+
"Confidence",
|
| 586 |
+
"Severity",
|
| 587 |
+
"ThreatScore",
|
| 588 |
+
"PolitenessScore",
|
| 589 |
+
"FriendlyScore",
|
| 590 |
+
"HasThreat",
|
| 591 |
+
"HasProfanity",
|
| 592 |
+
"HasSarcasm",
|
| 593 |
+
"Text",
|
| 594 |
+
]
|
| 595 |
+
)
|
| 596 |
+
for e in entries:
|
| 597 |
+
writer.writerow(
|
| 598 |
+
[
|
| 599 |
+
e.created_at.isoformat(),
|
| 600 |
+
e.label,
|
| 601 |
+
f"{e.confidence:.1f}",
|
| 602 |
+
e.severity,
|
| 603 |
+
e.threat_score,
|
| 604 |
+
e.politeness_score,
|
| 605 |
+
e.friendly_score,
|
| 606 |
+
int(e.has_threat),
|
| 607 |
+
int(e.has_profanity),
|
| 608 |
+
int(e.has_sarcasm),
|
| 609 |
+
e.text,
|
| 610 |
+
]
|
| 611 |
+
)
|
| 612 |
+
|
| 613 |
+
return send_file(filepath, as_attachment=True)
|
| 614 |
+
|
| 615 |
+
|
| 616 |
+
@app.route("/export/pdf")
|
| 617 |
+
@login_required
|
| 618 |
+
def export_pdf():
|
| 619 |
+
if not current_user.is_verified:
|
| 620 |
+
flash("Please verify your email to export data.", "error")
|
| 621 |
+
return redirect(url_for("verify", email=current_user.email))
|
| 622 |
+
|
| 623 |
+
buffer = BytesIO()
|
| 624 |
+
c = canvas.Canvas(buffer, pagesize=letter)
|
| 625 |
+
width, height = letter
|
| 626 |
+
|
| 627 |
+
c.setFillColorRGB(0.12, 0.15, 0.20)
|
| 628 |
+
c.rect(0, height - 60, width, 60, fill=1)
|
| 629 |
+
c.setFillColorRGB(1, 1, 1)
|
| 630 |
+
c.setFont("Helvetica-Bold", 18)
|
| 631 |
+
c.drawString(40, height - 35, "Tone Classifier – History Report")
|
| 632 |
+
|
| 633 |
+
entries = (
|
| 634 |
+
Entry.query.filter_by(user_id=current_user.id)
|
| 635 |
+
.order_by(Entry.created_at.desc())
|
| 636 |
+
.all()
|
| 637 |
+
)
|
| 638 |
+
|
| 639 |
+
y = height - 80
|
| 640 |
+
for e in entries:
|
| 641 |
+
if y < 90:
|
| 642 |
+
c.showPage()
|
| 643 |
+
y = height - 60
|
| 644 |
+
|
| 645 |
+
c.setFont("Helvetica-Bold", 10)
|
| 646 |
+
c.setFillColorRGB(0, 0, 0)
|
| 647 |
+
c.drawString(
|
| 648 |
+
40,
|
| 649 |
+
y,
|
| 650 |
+
f"{e.created_at.isoformat()} | {e.label} | Severity {e.severity}",
|
| 651 |
+
)
|
| 652 |
+
y -= 12
|
| 653 |
+
|
| 654 |
+
meta = f"Threat:{e.threat_score} Polite:{e.politeness_score} Friendly:{e.friendly_score}"
|
| 655 |
+
c.setFont("Helvetica", 9)
|
| 656 |
+
c.drawString(40, y, meta)
|
| 657 |
+
y -= 12
|
| 658 |
+
|
| 659 |
+
text = e.text
|
| 660 |
+
while len(text) > 90:
|
| 661 |
+
idx = text.rfind(" ", 0, 90)
|
| 662 |
+
if idx == -1:
|
| 663 |
+
idx = 90
|
| 664 |
+
c.drawString(50, y, text[:idx])
|
| 665 |
+
text = text[idx:].strip()
|
| 666 |
+
y -= 11
|
| 667 |
+
|
| 668 |
+
c.drawString(50, y, text)
|
| 669 |
+
y -= 20
|
| 670 |
+
|
| 671 |
+
c.showPage()
|
| 672 |
+
c.save()
|
| 673 |
+
|
| 674 |
+
buffer.seek(0)
|
| 675 |
+
filepath = os.path.join("exports", f"history_{current_user.id}.pdf")
|
| 676 |
+
with open(filepath, "wb") as f:
|
| 677 |
+
f.write(buffer.getvalue())
|
| 678 |
+
|
| 679 |
+
return send_file(filepath, as_attachment=True)
|
| 680 |
+
|
| 681 |
+
|
| 682 |
+
@app.route("/history/clear", methods=["POST"])
|
| 683 |
+
@login_required
|
| 684 |
+
def clear_history():
|
| 685 |
+
if not current_user.is_verified:
|
| 686 |
+
flash("Please verify your email to clear history.", "error")
|
| 687 |
+
return redirect(url_for("verify", email=current_user.email))
|
| 688 |
+
|
| 689 |
+
Entry.query.filter_by(user_id=current_user.id).delete()
|
| 690 |
+
db.session.commit()
|
| 691 |
+
flash("History cleared.", "success")
|
| 692 |
+
return redirect(url_for("history_view"))
|
| 693 |
+
|
| 694 |
+
|
| 695 |
+
# =========================================================
|
| 696 |
+
# DELETE ACCOUNT + GDPR PAGES
|
| 697 |
+
# =========================================================
|
| 698 |
+
|
| 699 |
+
@app.route("/account/delete", methods=["GET", "POST"])
|
| 700 |
+
@login_required
|
| 701 |
+
def delete_account():
|
| 702 |
+
if request.method == "POST":
|
| 703 |
+
password = normalize_text(request.form.get("password", ""))
|
| 704 |
+
|
| 705 |
+
if not check_password_hash(current_user.password_hash, password):
|
| 706 |
+
flash("Incorrect password. Account not deleted.", "error")
|
| 707 |
+
return redirect(url_for("delete_account"))
|
| 708 |
+
|
| 709 |
+
try:
|
| 710 |
+
uid = current_user.id
|
| 711 |
+
Entry.query.filter_by(user_id=uid).delete()
|
| 712 |
+
user = User.query.get(uid)
|
| 713 |
+
logout_user()
|
| 714 |
+
db.session.delete(user)
|
| 715 |
+
db.session.commit()
|
| 716 |
+
flash("Your account and all data have been deleted.", "success")
|
| 717 |
+
except Exception as e:
|
| 718 |
+
db.session.rollback()
|
| 719 |
+
flash("Error deleting account. Please try again.", "error")
|
| 720 |
+
print(f"[ERROR] delete_account failed: {e}")
|
| 721 |
+
return redirect(url_for("delete_account"))
|
| 722 |
+
|
| 723 |
+
return redirect(url_for("register"))
|
| 724 |
+
|
| 725 |
+
return render_template("delete_account.html", title="Delete Account")
|
| 726 |
+
|
| 727 |
+
|
| 728 |
+
@app.route("/privacy")
|
| 729 |
+
def privacy():
|
| 730 |
+
from datetime import datetime as dt
|
| 731 |
+
return render_template("privacy.html", title="Privacy Policy", datetime=dt)
|
| 732 |
+
|
| 733 |
+
|
| 734 |
+
@app.route("/do-not-sell")
|
| 735 |
+
def do_not_sell():
|
| 736 |
+
return render_template("do_not_sell.html", title="Do Not Sell My Info")
|
| 737 |
+
|
| 738 |
+
|
| 739 |
+
# =========================================================
|
| 740 |
+
# INIT DB & RUN (LOCAL)
|
| 741 |
+
# =========================================================
|
| 742 |
+
|
| 743 |
+
with app.app_context():
|
| 744 |
+
db.create_all()
|
| 745 |
+
|
| 746 |
+
if __name__ == "__main__":
|
| 747 |
+
# local dev
|
| 748 |
+
app.run(debug=True, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|