Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,104 @@
|
|
| 1 |
-
#app.py
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
| 3 |
import sys
|
| 4 |
import subprocess
|
| 5 |
-
|
| 6 |
-
subprocess.check_call([
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
BASE_PATH = os.path.dirname(os.path.abspath(__file__))
|
| 13 |
-
# Forçar o output_dir para um local fixo e absoluto
|
| 14 |
FINAL_OUTPUT_DIR = os.path.join(BASE_PATH, "trained_model_output")
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
import json
|
| 18 |
-
import
|
| 19 |
import math
|
| 20 |
-
import psutil
|
| 21 |
-
import traceback
|
| 22 |
-
import time
|
| 23 |
-
import threading
|
| 24 |
-
import webbrowser
|
| 25 |
import platform
|
| 26 |
-
import cpuinfo
|
| 27 |
import statistics
|
| 28 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
import ctypes as ct
|
| 30 |
-
import
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
import torch.nn as nn
|
| 33 |
|
| 34 |
-
#
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
#
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
#
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
| 51 |
-
# Ajusta logger do TensorFlow
|
| 52 |
-
#tf.get_logger().setLevel('ERROR')
|
| 53 |
|
| 54 |
-
from datetime import timedelta
|
| 55 |
-
from datetime import datetime
|
| 56 |
-
from contextlib import suppress
|
| 57 |
-
from threading import Thread
|
| 58 |
-
from flask import Flask, render_template, request, jsonify
|
| 59 |
-
from contextlib import contextmanager
|
| 60 |
-
|
| 61 |
-
# Importações do Hugging Face
|
| 62 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, Trainer
|
| 63 |
-
from transformers import TrainerCallback, TrainerState, TrainerControl
|
| 64 |
-
from datasets import Dataset, load_from_disk # type: ignore
|
| 65 |
-
from peft import LoraConfig, get_peft_model, PeftModel, TaskType # type: ignore
|
| 66 |
-
from transformers import DataCollatorForLanguageModeling
|
| 67 |
-
from flask import send_from_directory
|
| 68 |
-
#------------------------------------------------------
|
| 69 |
-
from warnings import warn as log_warning
|
| 70 |
-
from config_manager import _load_constants_from_file, map_backend_to_frontend, update_python_constants
|
| 71 |
-
#pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cpu
|
| 72 |
-
|
| 73 |
-
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 74 |
#---------------------
|
| 75 |
app = Flask(__name__)
|
| 76 |
all_data = []
|
|
@@ -1634,6 +1662,18 @@ def dataset_dir():
|
|
| 1634 |
listing.append("(pasta ainda não criada)")
|
| 1635 |
return jsonify({"listing": listing})
|
| 1636 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1637 |
#--------------------------------------------
|
| 1638 |
@app.route("/api/train_status")
|
| 1639 |
def train_status():
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
# =========================================================================
|
| 3 |
+
# 0. Bootstrap — pip upgrades antes de qualquer import sensível
|
| 4 |
+
# =========================================================================
|
| 5 |
+
import os
|
| 6 |
import sys
|
| 7 |
import subprocess
|
| 8 |
+
|
| 9 |
+
subprocess.check_call([
|
| 10 |
+
sys.executable, "-m", "pip", "install", "--quiet", "--upgrade",
|
| 11 |
+
"urllib3<2.0", "charset_normalizer<3.4"
|
| 12 |
+
])
|
| 13 |
+
|
| 14 |
+
BASE_PATH = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
|
| 15 |
FINAL_OUTPUT_DIR = os.path.join(BASE_PATH, "trained_model_output")
|
| 16 |
+
|
| 17 |
+
# Suprimir warnings antes de qualquer outro import
|
| 18 |
+
import warnings
|
| 19 |
+
warnings.filterwarnings("ignore", category=UserWarning)
|
| 20 |
+
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
| 21 |
+
try:
|
| 22 |
+
from requests.packages.urllib3.exceptions import DependencyWarning
|
| 23 |
+
warnings.simplefilter("ignore", DependencyWarning)
|
| 24 |
+
except ImportError:
|
| 25 |
+
pass
|
| 26 |
+
|
| 27 |
+
# Silenciar oneDNN / TF (mesmo sem TensorFlow instalado)
|
| 28 |
+
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "0"
|
| 29 |
+
|
| 30 |
+
# =========================================================================
|
| 31 |
+
# 1. Stdlib
|
| 32 |
+
# =========================================================================
|
| 33 |
+
import glob
|
| 34 |
+
import inspect
|
| 35 |
import json
|
| 36 |
+
import logging
|
| 37 |
import math
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
import platform
|
|
|
|
| 39 |
import statistics
|
| 40 |
+
import threading
|
| 41 |
+
import time
|
| 42 |
+
import traceback
|
| 43 |
+
|
| 44 |
+
from contextlib import contextmanager, suppress
|
| 45 |
+
from datetime import datetime, timedelta
|
| 46 |
+
from threading import Thread
|
| 47 |
+
from typing import Any, Dict, Union
|
| 48 |
+
from warnings import warn as log_warning
|
| 49 |
+
|
| 50 |
+
# =========================================================================
|
| 51 |
+
# 2. Third-party — sistema / hardware
|
| 52 |
+
# =========================================================================
|
| 53 |
+
import chardet
|
| 54 |
+
import cpuinfo
|
| 55 |
import ctypes as ct
|
| 56 |
+
import psutil
|
| 57 |
+
|
| 58 |
+
# =========================================================================
|
| 59 |
+
# 3. PyTorch
|
| 60 |
+
# =========================================================================
|
| 61 |
+
import torch
|
| 62 |
import torch.nn as nn
|
| 63 |
|
| 64 |
+
# =========================================================================
|
| 65 |
+
# 4. Flask
|
| 66 |
+
# =========================================================================
|
| 67 |
+
from flask import Flask, jsonify, render_template, request, send_file, send_from_directory
|
| 68 |
|
| 69 |
+
# =========================================================================
|
| 70 |
+
# 5. HuggingFace / PEFT
|
| 71 |
+
# =========================================================================
|
| 72 |
+
from datasets import Dataset, load_from_disk
|
| 73 |
+
from peft import LoraConfig, PeftModel, TaskType, get_peft_model
|
| 74 |
+
from transformers import (
|
| 75 |
+
AutoModelForCausalLM,
|
| 76 |
+
AutoTokenizer,
|
| 77 |
+
DataCollatorForLanguageModeling,
|
| 78 |
+
Trainer,
|
| 79 |
+
TrainerCallback,
|
| 80 |
+
TrainerControl,
|
| 81 |
+
TrainerState,
|
| 82 |
+
TrainingArguments,
|
| 83 |
+
)
|
| 84 |
|
| 85 |
+
# =========================================================================
|
| 86 |
+
# 6. Local
|
| 87 |
+
# =========================================================================
|
| 88 |
+
from config_manager import (
|
| 89 |
+
_load_constants_from_file,
|
| 90 |
+
map_backend_to_frontend,
|
| 91 |
+
update_python_constants,
|
| 92 |
+
)
|
| 93 |
|
| 94 |
+
# =========================================================================
|
| 95 |
+
# 7. Logging
|
| 96 |
+
# =========================================================================
|
| 97 |
+
logging.basicConfig(
|
| 98 |
+
level=logging.INFO,
|
| 99 |
+
format="%(asctime)s - %(levelname)s - %(message)s"
|
| 100 |
+
)
|
|
|
|
|
|
|
|
|
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
#---------------------
|
| 103 |
app = Flask(__name__)
|
| 104 |
all_data = []
|
|
|
|
| 1662 |
listing.append("(pasta ainda não criada)")
|
| 1663 |
return jsonify({"listing": listing})
|
| 1664 |
|
| 1665 |
+
#-------------------------------------------
|
| 1666 |
+
@app.route("/api/dataset/download/<path:filename>")
|
| 1667 |
+
def dataset_download(filename):
|
| 1668 |
+
"""Serve ficheiro do directório de output do dataset para download."""
|
| 1669 |
+
import re
|
| 1670 |
+
# Segurança: só permite nomes de ficheiro simples sem path traversal
|
| 1671 |
+
safe_name = os.path.basename(filename)
|
| 1672 |
+
file_path = os.path.join(DATASET_OUT_DIR, safe_name)
|
| 1673 |
+
if not os.path.isfile(file_path):
|
| 1674 |
+
return jsonify({"error": "File not found."}), 404
|
| 1675 |
+
return send_file(file_path, as_attachment=True, download_name=safe_name)
|
| 1676 |
+
|
| 1677 |
#--------------------------------------------
|
| 1678 |
@app.route("/api/train_status")
|
| 1679 |
def train_status():
|