File size: 1,958 Bytes
64ae41c
 
 
 
 
 
 
 
 
beabfb7
 
64ae41c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# app.py

import os
from datetime import datetime
from typing import Optional

from flask import Flask, render_template

# Your existing config and core logic
from src.config import AppConfig
from src.hn_mood_reader import HnMoodReader, FeedEntry

# --- Flask App Initialization ---
app = Flask(__name__)

# --- Global Cache for the Model ---
global_reader: Optional[HnMoodReader] = None

def initialize_reader() -> HnMoodReader:
    """
    Initializes the HnMoodReader instance. This function is called once
    when the application starts.
    """
    print("Attempting to initialize the mood reader model...")
    model_name = os.environ.get("MOOD_MODEL", AppConfig.DEFAULT_MOOD_READER_MODEL)
    try:
        reader = HnMoodReader(model_name=model_name)
        print("Model loaded successfully.")
        return reader
    except Exception as e:
        # If the model fails to load, print a fatal error and exit the app.
        print(f"FATAL: Could not initialize model '{model_name}'. Error: {e}", file=sys.stderr)
        sys.exit(1) # Exit with a non-zero code to indicate failure

# --- Initialize the reader as soon as the app starts ---
global_reader = initialize_reader()

# --- Flask Route ---
@app.route('/')
def index():
    """Main page route."""
    try:
        scored_entries = global_reader.fetch_and_score_feed()
        
        return render_template(
            'index.html',
            entries=scored_entries,
            model_name=global_reader.model_name,
            last_updated=datetime.now().strftime('%H:%M:%S')
        )
    except Exception as e:
        # Render a simple error page if something goes wrong
        return render_template('error.html', error=str(e)), 500

if __name__ == '__main__':
    # Using debug=False is recommended for a stable display
# use_reloader=False prevents the app from initializing the model twice in debug mode
    app.run(host='0.0.0.0', port=5000, debug=False, use_reloader=False)