forked from NeptuneHub/AudioMuse-AI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_analysis.py
More file actions
157 lines (143 loc) · 5.64 KB
/
app_analysis.py
File metadata and controls
157 lines (143 loc) · 5.64 KB
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
# app_analysis.py
from flask import Blueprint, jsonify, request
import uuid
import logging
# Import configuration from the main config.py
from config import NUM_RECENT_ALBUMS, TOP_N_MOODS
# RQ import
from rq import Retry
logger = logging.getLogger(__name__)
# Create a Blueprint for analysis-related routes
analysis_bp = Blueprint('analysis_bp', __name__)
@analysis_bp.route('/cleaning', methods=['GET'])
def cleaning_page():
"""
Serves the HTML page for the Database Cleaning feature.
---
tags:
- UI
responses:
200:
description: HTML content of the cleaning page.
content:
text/html:
schema:
type: string
"""
from flask import render_template
return render_template('cleaning.html', title = 'AudioMuse-AI - Database Cleaning', active='cleaning')
@analysis_bp.route('/api/analysis/start', methods=['POST'])
def start_analysis_endpoint():
"""
Start the music analysis process for recent albums.
This endpoint enqueues a main analysis task.
Note: Starting a new analysis task will archive previously successful tasks by setting their status to REVOKED.
---
tags:
- Analysis
requestBody:
description: Configuration for the analysis task.
required: false
content:
application/json:
schema:
type: object
properties:
num_recent_albums:
type: integer
description: Number of recent albums to process.
default: "Configured NUM_RECENT_ALBUMS"
top_n_moods:
type: integer
description: Number of top moods to extract per track.
default: "Configured TOP_N_MOODS"
responses:
202:
description: Analysis task successfully enqueued.
content:
application/json:
schema:
type: object
properties:
task_id:
type: string
description: The ID of the enqueued main analysis task.
task_type:
type: string
description: Type of the task (e.g., main_analysis).
example: main_analysis
status:
type: string
description: The initial status of the job in the queue (e.g., queued).
400:
description: Invalid input.
500:
description: Server error during task enqueue.
"""
# Local imports to prevent circular dependency at startup
from app_helper import rq_queue_high, clean_up_previous_main_tasks, save_task_status, TASK_STATUS_PENDING
data = request.json or {}
# MODIFIED: Removed jellyfin_url, jellyfin_user_id, and jellyfin_token as they are no longer passed to the task.
# The task now gets these details from the central config.
num_recent_albums = int(data.get('num_recent_albums', NUM_RECENT_ALBUMS))
top_n_moods = int(data.get('top_n_moods', TOP_N_MOODS))
logger.info(f"Starting analysis request: num_recent_albums={num_recent_albums}, top_n_moods={top_n_moods}")
job_id = str(uuid.uuid4())
# Clean up details of previously successful or stale tasks before starting a new one
clean_up_previous_main_tasks()
save_task_status(job_id, "main_analysis", TASK_STATUS_PENDING, details={"message": "Task enqueued."})
# Enqueue task using a string path to its function.
# MODIFIED: The arguments passed to the task are updated to match the new function signature.
job = rq_queue_high.enqueue(
'tasks.analysis.run_analysis_task',
args=(num_recent_albums, top_n_moods),
job_id=job_id,
description="Main Music Analysis",
retry=Retry(max=3),
job_timeout=-1 # No timeout
)
return jsonify({"task_id": job.id, "task_type": "main_analysis", "status": job.get_status()}), 202
@analysis_bp.route('/api/cleaning/start', methods=['POST'])
def start_cleaning_endpoint():
"""
Identify and automatically clean orphaned albums from the database.
This endpoint enqueues a cleaning task that both identifies and deletes orphaned albums.
---
tags:
- Cleaning
responses:
202:
description: Database cleaning task successfully enqueued.
content:
application/json:
schema:
type: object
properties:
task_id:
type: string
description: The ID of the enqueued database cleaning task.
task_type:
type: string
description: Type of the task (cleaning).
example: cleaning
status:
type: string
description: The initial status of the job in the queue (e.g., queued).
500:
description: Server error during task enqueue.
"""
# Local imports to prevent circular dependency at startup
from app_helper import rq_queue_high, clean_up_previous_main_tasks, save_task_status, TASK_STATUS_PENDING
# Clean up any previous cleaning tasks
clean_up_previous_main_tasks()
job_id = str(uuid.uuid4())
save_task_status(job_id, "cleaning", TASK_STATUS_PENDING, details={"message": "Database cleaning task enqueued."})
# Enqueue combined cleaning task
job = rq_queue_high.enqueue(
'tasks.cleaning.identify_and_clean_orphaned_albums_task',
job_id=job_id,
description="Database Cleaning (Identify and Delete Orphaned Albums)",
retry=Retry(max=2),
job_timeout=-1 # No timeout
)
return jsonify({"task_id": job.id, "task_type": "cleaning", "status": job.get_status()}), 202