Spaces:
Paused
Paused
Alessandro Piana
commited on
Commit
·
86b211a
1
Parent(s):
29d8e61
dockerfile con logging 59
Browse files
app.py
CHANGED
|
@@ -10,7 +10,6 @@ from datetime import datetime
|
|
| 10 |
from flask import Flask, render_template, redirect, url_for, flash, request # AGGIUNTO 'request'
|
| 11 |
from flask_login import LoginManager, current_user
|
| 12 |
import logging
|
| 13 |
-
|
| 14 |
from werkzeug.middleware.proxy_fix import ProxyFix # NUOVA RIGA
|
| 15 |
from flask_cors import CORS
|
| 16 |
|
|
@@ -142,39 +141,27 @@ def get_life_coach_model():
|
|
| 142 |
|
| 143 |
return model_instance
|
| 144 |
|
| 145 |
-
|
| 146 |
def generate_response_threadsafe(prompt: str, conversation_history: list) -> str:
|
| 147 |
"""
|
| 148 |
Generate a response using the model with thread-safe access.
|
| 149 |
-
|
| 150 |
-
Args:
|
| 151 |
-
prompt: User's input message
|
| 152 |
-
conversation_history: List of previous messages
|
| 153 |
-
|
| 154 |
-
Returns:
|
| 155 |
-
Generated response
|
| 156 |
"""
|
| 157 |
-
logger.info(f"
|
| 158 |
-
logger.info(f"Prompt: {prompt}")
|
| 159 |
-
logger.info(f"History length: {len(conversation_history) if conversation_history else 0}")
|
| 160 |
-
|
| 161 |
model = get_life_coach_model()
|
| 162 |
|
| 163 |
-
logger.info(f"Model instance: {model}")
|
| 164 |
-
logger.info(f"Model type: {type(model)}")
|
| 165 |
-
|
| 166 |
# Use lock to ensure only one inference at a time (GPU limitation)
|
| 167 |
with model_lock:
|
| 168 |
-
logger.info("
|
| 169 |
response = model.generate_response(
|
| 170 |
prompt=prompt,
|
| 171 |
-
max_new_tokens=256,
|
| 172 |
conversation_history=conversation_history
|
| 173 |
)
|
| 174 |
-
logger.info(f"
|
| 175 |
|
| 176 |
return response
|
| 177 |
|
|
|
|
| 178 |
# Import models after db is initialized
|
| 179 |
from models import User, Conversation, Message
|
| 180 |
|
|
@@ -213,27 +200,7 @@ from chat import chat_bp
|
|
| 213 |
app.register_blueprint(auth_bp, url_prefix='/auth')
|
| 214 |
app.register_blueprint(chat_bp, url_prefix='/chat')
|
| 215 |
|
| 216 |
-
|
| 217 |
-
# --- NUOVA SEZIONE: PRE-CARICAMENTO MODELLO ASINCRONO (FIX 1) ---
|
| 218 |
-
# ------------------------------------------------------------------
|
| 219 |
-
# Nota: La funzione get_life_coach_model() è già definita in questo file.
|
| 220 |
-
def load_model_on_startup():
|
| 221 |
-
"""Carica il modello in un thread separato per evitare il blocco del server."""
|
| 222 |
-
try:
|
| 223 |
-
# L'app_context è necessario per garantire la sicurezza del DB/Logging
|
| 224 |
-
with app.app_context():
|
| 225 |
-
logger.info("--- PRE-LOAD: Avvio caricamento modello in background...")
|
| 226 |
-
get_life_coach_model() # La funzione che attiva il caricamento lento
|
| 227 |
-
logger.info("--- PRE-LOAD: Caricamento modello completato.")
|
| 228 |
-
except Exception as e:
|
| 229 |
-
logger.error(f"--- PRE-LOAD ERROR: Errore nel caricamento: {e}", exc_info=True)
|
| 230 |
-
|
| 231 |
-
# Avvia il thread immediatamente dopo la configurazione dell'app.
|
| 232 |
-
logger.info("--- PRE-LOAD: Avvio thread per caricamento modello...")
|
| 233 |
-
threading.Thread(target=load_model_on_startup).start()
|
| 234 |
-
# ------------------------------------------------------------------
|
| 235 |
-
# --- FINE PRE-CARICAMENTO ---
|
| 236 |
-
# ------------------------------------------------------------------
|
| 237 |
@app.after_request
|
| 238 |
def add_header(response):
|
| 239 |
"""Add headers to prevent caching of static files."""
|
|
|
|
| 10 |
from flask import Flask, render_template, redirect, url_for, flash, request # AGGIUNTO 'request'
|
| 11 |
from flask_login import LoginManager, current_user
|
| 12 |
import logging
|
|
|
|
| 13 |
from werkzeug.middleware.proxy_fix import ProxyFix # NUOVA RIGA
|
| 14 |
from flask_cors import CORS
|
| 15 |
|
|
|
|
| 141 |
|
| 142 |
return model_instance
|
| 143 |
|
| 144 |
+
|
| 145 |
def generate_response_threadsafe(prompt: str, conversation_history: list) -> str:
|
| 146 |
"""
|
| 147 |
Generate a response using the model with thread-safe access.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
"""
|
| 149 |
+
logger.info(f"--- GENERATE_RESPONSE: Chiamata per utente {current_user.username}")
|
|
|
|
|
|
|
|
|
|
| 150 |
model = get_life_coach_model()
|
| 151 |
|
|
|
|
|
|
|
|
|
|
| 152 |
# Use lock to ensure only one inference at a time (GPU limitation)
|
| 153 |
with model_lock:
|
| 154 |
+
logger.info("--- GENERATE_RESPONSE: Acquisito lock, chiamata a model.generate_response()...")
|
| 155 |
response = model.generate_response(
|
| 156 |
prompt=prompt,
|
| 157 |
+
max_new_tokens=256,
|
| 158 |
conversation_history=conversation_history
|
| 159 |
)
|
| 160 |
+
logger.info(f"--- GENERATE_RESPONSE: Risposta ricevuta.")
|
| 161 |
|
| 162 |
return response
|
| 163 |
|
| 164 |
+
|
| 165 |
# Import models after db is initialized
|
| 166 |
from models import User, Conversation, Message
|
| 167 |
|
|
|
|
| 200 |
app.register_blueprint(auth_bp, url_prefix='/auth')
|
| 201 |
app.register_blueprint(chat_bp, url_prefix='/chat')
|
| 202 |
|
| 203 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
@app.after_request
|
| 205 |
def add_header(response):
|
| 206 |
"""Add headers to prevent caching of static files."""
|