57 lines
2.7 KiB
PHP
57 lines
2.7 KiB
PHP
<?php
|
|
session_start();
|
|
|
|
require_once __DIR__ . '/../ai/LocalAIApi.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
echo json_encode(['error' => 'User not logged in']);
|
|
exit;
|
|
}
|
|
|
|
$moodText = isset($_POST['mood_text']) ? trim($_POST['mood_text']) : '';
|
|
|
|
if (empty($moodText)) {
|
|
echo json_encode(['error' => 'Mood text cannot be empty']);
|
|
exit;
|
|
}
|
|
|
|
$musicDatabase = [
|
|
'happy' => [['videoId' => 'y6Sxv-sUYtM', 'title' => 'Pharrell Williams - Happy'], ['videoId' => 'HgzGwKwLmgM', 'title' => 'Queen - Don\'t Stop Me Now'], ['videoId' => 'iPUmE-tne5s', 'title' => 'Katrina & The Waves - Walking On Sunshine']],
|
|
'sad' => [['videoId' => 'hLQl3WQQoQ0', 'title' => 'Adele - Someone Like You'], ['videoId' => '8AHCfZTRGiI', 'title' => 'Johnny Cash - Hurt'], ['videoId' => 'XFkzRNyygfk', 'title' => 'Radiohead - Creep']],
|
|
'energetic' => [['videoId' => 'v2AC41dglnM', 'title' => 'AC/DC - Thunderstruck'], ['videoId' => 'o1tj2zJ2Wvg', 'title' => 'Guns N\' Roses - Welcome to the Jungle'], ['videoId' => 'CD-E-LDc384', 'title' => 'Metallica - Enter Sandman']],
|
|
'calm' => [['videoId' => 'vNwYtllyt3Q', 'title' => 'Brian Eno - Music for Airports 1/1'], ['videoId' => 'S-Xm7s9eGxU', 'title' => 'Erik Satie - Gymnopédie No. 1'], ['videoId' => 'U3u4pQ44e14', 'title' => 'Claude Debussy - Clair de Lune']],
|
|
'upbeat' => [['videoId' => 'FGBhQbmPwH8', 'title' => 'Daft Punk - One More Time'], ['videoId' => 'sy1dYFGkPUE', 'title' => 'Justice - D.A.N.C.E.'], ['videoId' => 'Cj8JrQ9w5jY', 'title' => 'LCD Soundsystem - Daft Punk Is Playing at My House']]
|
|
];
|
|
|
|
$prompt = 'Based on the following text, what is the primary mood? Please respond with only one of the following words: happy, sad, energetic, calm, upbeat. Text: ' . $moodText;
|
|
|
|
$resp = LocalAIApi::createResponse([
|
|
'input' => [
|
|
['role' => 'system', 'content' => 'You are a mood detection assistant.'],
|
|
['role' => 'user', 'content' => $prompt],
|
|
],
|
|
]);
|
|
|
|
if (!empty($resp['success'])) {
|
|
$decoded = LocalAIApi::decodeJsonFromResponse($resp);
|
|
$aiReply = $decoded['choices'][0]['message']['content'] ?? '';
|
|
$mood = trim(strtolower($aiReply));
|
|
|
|
if (array_key_exists($mood, $musicDatabase)) {
|
|
$songs = $musicDatabase[$mood];
|
|
echo json_encode(['mood' => $mood, 'songs' => $songs]);
|
|
} else {
|
|
// Fallback to a random mood if the AI response is not a valid key
|
|
$randomMood = array_rand($musicDatabase);
|
|
$songs = $musicDatabase[$randomMood];
|
|
echo json_encode(['mood' => $randomMood, 'songs' => $songs]);
|
|
}
|
|
} else {
|
|
// Fallback to a random mood if the AI call fails
|
|
$randomMood = array_rand($musicDatabase);
|
|
$songs = $musicDatabase[$randomMood];
|
|
echo json_encode(['mood' => $randomMood, 'songs' => $songs]);
|
|
}
|