Compare commits

...

3 Commits

Author SHA1 Message Date
Flatlogic Bot
66a149c706 Revert to version 2f8a204 2025-11-28 12:23:38 +00:00
Flatlogic Bot
9043ce375e not so good 2025-11-28 12:23:32 +00:00
Flatlogic Bot
2f8a204b48 123 2025-11-28 12:21:10 +00:00
5 changed files with 367 additions and 145 deletions

75
api/chat.php Normal file
View File

@ -0,0 +1,75 @@
<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../ai/LocalAIApi.php';
require_once __DIR__ . '/../includes/pexels.php';
$action = $_POST['action'] ?? 'chat';
if ($action === 'chat') {
$day = $_POST['day'] ?? 'good';
$profession = $_POST['profession'] ?? 'programmer';
// 1. Generate content with AI using a JSON-structured prompt
$prompt = <<<PROMPT
A user had a "{$day}" day at their job as a "{$profession}".
Please generate a response in JSON format with two keys:
1. "wish": A short, funny, and cheerful weekend wish for them.
2. "meme_keyword": A single, simple, SFW (safe for work) keyword for a funny meme image related to their situation.
Example:
{
"wish": "Your code fought bravely, now let your weekend be bug-free! Enjoy the break!",
"meme_keyword": "relaxing cat"
}
PROMPT;
$aiResponse = LocalAIApi::createResponse([
'input' => [
['role' => 'system', 'content' => 'You are a cheerful and witty assistant who provides funny weekend wishes and suggests meme keywords in a structured JSON format.'],
['role' => 'user', 'content' => $prompt],
],
]);
$generatedText = "I'm a bit tired, but I wish you a fantastic weekend! Let's talk more on Monday.";
$memeKeyword = 'cat'; // Default keyword
if (!empty($aiResponse['success'])) {
$jsonOutput = LocalAIApi::decodeJsonFromResponse($aiResponse);
if ($jsonOutput && isset($jsonOutput['wish']) && isset($jsonOutput['meme_keyword'])) {
$generatedText = $jsonOutput['wish'];
$memeKeyword = $jsonOutput['meme_keyword'];
} else {
$generatedText = LocalAIApi::extractText($aiResponse); // Fallback to raw text
}
}
// 2. Fetch a meme image using the keyword
$memeData = null;
$pexelsUrl = 'https://api.pexels.com/v1/search?query=' . urlencode($memeKeyword) . '&orientation=portrait&per_page=1&page=1';
$pexelsData = pexels_get($pexelsUrl);
if ($pexelsData && !empty($pexelsData['photos'])) {
$photo = $pexelsData['photos'][0];
$src = $photo['src']['large'] ?? $photo['src']['original'];
$memeData = [
'src' => $src,
'photographer' => $photo['photographer']
];
} else {
// Fallback image
$memeData = [
'src' => 'https://images.pexels.com/photos/1741205/pexels-photo-1741205.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940',
'photographer' => 'Pexels'
];
}
echo json_encode([
'success' => true,
'message' => $generatedText,
'meme' => $memeData
]);
exit;
}
echo json_encode(['success' => false, 'error' => 'Invalid action.']);

77
assets/css/custom.css Normal file
View File

@ -0,0 +1,77 @@
body {
background-color: #f8f9fa;
font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
.hero {
background: linear-gradient(45deg, #6f42c1, #fd7e14);
color: white;
padding: 100px 0;
text-align: center;
}
.chat-container {
max-width: 800px;
margin: 50px auto;
background-color: #ffffff;
border-radius: 0.75rem;
box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
}
.chat-header {
background-color: #6f42c1;
color: white;
padding: 1rem;
border-top-left-radius: 0.75rem;
border-top-right-radius: 0.75rem;
text-align: center;
}
.chat-box {
height: 400px;
overflow-y: auto;
padding: 1rem;
}
.chat-bubble {
margin-bottom: 1rem;
padding: 0.75rem 1.25rem;
border-radius: 0.75rem;
max-width: 75%;
line-height: 1.5;
}
.chat-bubble.user {
background-color: #e9ecef;
color: #212529;
margin-left: auto;
text-align: right;
}
.chat-bubble.ai {
background-color: #6f42c1;
color: white;
margin-right: auto;
}
.chat-bubble.ai .meme {
max-width: 100%;
border-radius: 0.5rem;
margin-top: 0.5rem;
}
.chat-input {
border-top: 1px solid #dee2e6;
padding: 1rem;
}
.btn-primary {
background-color: #6f42c1;
border-color: #6f42c1;
}
.btn-primary:hover {
background-color: #5a37a0;
border-color: #5a37a0;
}

133
assets/js/main.js Normal file
View File

@ -0,0 +1,133 @@
document.addEventListener('DOMContentLoaded', () => {
const startChatBtn = document.getElementById('startChatBtn');
const chatContainer = document.getElementById('chatContainer');
const chatBox = document.getElementById('chatBox');
const chatForm = document.getElementById('chatForm');
const chatInput = document.getElementById('chatInput');
// Conversation context
let conversationState = 'initial'; // 'initial', 'asked_day', 'asked_profession', 'done'
let userDay = '';
let userProfession = '';
if (startChatBtn) {
startChatBtn.addEventListener('click', () => {
chatContainer.scrollIntoView({ behavior: 'smooth' });
if (conversationState === 'initial') {
setTimeout(() => {
addAiMessage('Hello there! How was your day?');
conversationState = 'asked_day';
}, 500);
}
});
}
chatForm.addEventListener('submit', (e) => {
e.preventDefault();
const userMessage = chatInput.value.trim();
if (userMessage) {
addUserMessage(userMessage);
chatInput.value = '';
handleConversation(userMessage);
}
});
function handleConversation(userMessage) {
if (conversationState === 'asked_day') {
userDay = userMessage;
addAiMessage('Interesting! And what is your profession?');
conversationState = 'asked_profession';
} else if (conversationState === 'asked_profession') {
userProfession = userMessage;
conversationState = 'done';
getAiResponse();
}
}
function getAiResponse() {
addAiMessage('Thinking of something funny...', 'typing');
const formData = new FormData();
formData.append('day', userDay);
formData.append('profession', userProfession);
fetch('api/chat.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
removeTypingIndicator();
if (data.success) {
addAiMeme(data.message, data.meme.src, data.meme.photographer);
addMoreButton();
} else {
addAiMessage('Oops, my wires are crossed. Please try again!');
}
})
.catch(error => {
removeTypingIndicator();
console.error('Error:', error);
addAiMessage('I seem to have lost my train of thought. Could you try again?');
});
}
function addMoreButton() {
const moreButton = document.createElement('button');
moreButton.textContent = 'Another one!';
moreButton.className = 'btn btn-secondary btn-sm mt-2';
moreButton.onclick = () => {
moreButton.remove();
getAiResponse();
};
chatBox.appendChild(moreButton);
chatBox.scrollTop = chatBox.scrollHeight;
}
function addUserMessage(message) {
const bubble = document.createElement('div');
bubble.className = 'chat-bubble user';
bubble.textContent = message;
chatBox.appendChild(bubble);
chatBox.scrollTop = chatBox.scrollHeight;
}
function addAiMessage(message, id = null) {
const bubble = document.createElement('div');
bubble.className = 'chat-bubble ai';
if (id) bubble.id = id;
bubble.textContent = message;
chatBox.appendChild(bubble);
chatBox.scrollTop = chatBox.scrollHeight;
}
function removeTypingIndicator() {
const typingBubble = document.getElementById('typing');
if (typingBubble) {
typingBubble.remove();
}
}
function addAiMeme(text, imageUrl, photographer) {
const bubble = document.createElement('div');
bubble.className = 'chat-bubble ai';
const textElement = document.createElement('p');
textElement.textContent = text;
bubble.appendChild(textElement);
const memeElement = document.createElement('img');
memeElement.src = imageUrl;
memeElement.className = 'meme';
memeElement.alt = `Meme for ${userProfession}`;
bubble.appendChild(memeElement);
const credit = document.createElement('p');
credit.className = 'text-muted small mt-2';
credit.innerHTML = `Photo by ${photographer} on Pexels`;
bubble.appendChild(credit);
chatBox.appendChild(bubble);
chatBox.scrollTop = chatBox.scrollHeight;
}
});

25
includes/pexels.php Normal file
View File

@ -0,0 +1,25 @@
<?php
function pexels_key() {
$k = getenv('PEXELS_KEY');
return $k && strlen($k) > 0 ? $k : 'Vc99rnmOhHhJAbgGQoKLZtsaIVfkeownoQNbTj78VemUjKh08ZYRbf18';
}
function pexels_get($url) {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [ 'Authorization: '. pexels_key() ],
CURLOPT_TIMEOUT => 15,
]);
$resp = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code >= 200 && $code < 300 && $resp) return json_decode($resp, true);
return null;
}
function download_to($srcUrl, $destPath) {
$data = file_get_contents($srcUrl);
if ($data === false) return false;
if (!is_dir(dirname($destPath))) mkdir(dirname($destPath), 0775, true);
return file_put_contents($destPath, $data) !== false;
}

192
index.php
View File

@ -1,150 +1,62 @@
<?php
declare(strict_types=1);
@ini_set('display_errors', '1');
@error_reporting(E_ALL);
@date_default_timezone_set('UTC');
$phpVersion = PHP_VERSION;
$now = date('Y-m-d H:i:s');
?>
<!doctype html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>New Style</title>
<?php
// Read project preview data from environment
$projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? '';
$projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
?>
<?php if ($projectDescription): ?>
<!-- Meta description -->
<meta name="description" content='<?= htmlspecialchars($projectDescription) ?>' />
<!-- Open Graph meta tags -->
<meta property="og:description" content="<?= htmlspecialchars($projectDescription) ?>" />
<!-- Twitter meta tags -->
<meta property="twitter:description" content="<?= htmlspecialchars($projectDescription) ?>" />
<?php endif; ?>
<?php if ($projectImageUrl): ?>
<!-- Open Graph image -->
<meta property="og:image" content="<?= htmlspecialchars($projectImageUrl) ?>" />
<!-- Twitter image -->
<meta property="twitter:image" content="<?= htmlspecialchars($projectImageUrl) ?>" />
<?php endif; ?>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" rel="stylesheet">
<style>
:root {
--bg-color-start: #6a11cb;
--bg-color-end: #2575fc;
--text-color: #ffffff;
--card-bg-color: rgba(255, 255, 255, 0.01);
--card-border-color: rgba(255, 255, 255, 0.1);
}
body {
margin: 0;
font-family: 'Inter', sans-serif;
background: linear-gradient(45deg, var(--bg-color-start), var(--bg-color-end));
color: var(--text-color);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
text-align: center;
overflow: hidden;
position: relative;
}
body::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><path d="M-10 10L110 10M10 -10L10 110" stroke-width="1" stroke="rgba(255,255,255,0.05)"/></svg>');
animation: bg-pan 20s linear infinite;
z-index: -1;
}
@keyframes bg-pan {
0% { background-position: 0% 0%; }
100% { background-position: 100% 100%; }
}
main {
padding: 2rem;
}
.card {
background: var(--card-bg-color);
border: 1px solid var(--card-border-color);
border-radius: 16px;
padding: 2rem;
backdrop-filter: blur(20px);
-webkit-backdrop-filter: blur(20px);
box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.1);
}
.loader {
margin: 1.25rem auto 1.25rem;
width: 48px;
height: 48px;
border: 3px solid rgba(255, 255, 255, 0.25);
border-top-color: #fff;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.hint {
opacity: 0.9;
}
.sr-only {
position: absolute;
width: 1px; height: 1px;
padding: 0; margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap; border: 0;
}
h1 {
font-size: 3rem;
font-weight: 700;
margin: 0 0 1rem;
letter-spacing: -1px;
}
p {
margin: 0.5rem 0;
font-size: 1.1rem;
}
code {
background: rgba(0,0,0,0.2);
padding: 2px 6px;
border-radius: 4px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
footer {
position: absolute;
bottom: 1rem;
font-size: 0.8rem;
opacity: 0.7;
}
</style>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MemeChat - Your Daily Dose of Humor</title>
<meta name="description" content="An AI-powered chat to make you laugh.">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">
<!-- Custom CSS -->
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
<!-- Platform-managed meta tags -->
<meta property="og:image" content="<?php echo $_SERVER['PROJECT_IMAGE_URL']; ?>">
<meta name="twitter:image" content="<?php echo $_SERVER['PROJECT_IMAGE_URL']; ?>">
</head>
<body>
<main>
<div class="card">
<h1>Analyzing your requirements and generating your website…</h1>
<div class="loader" role="status" aria-live="polite" aria-label="Applying initial changes">
<span class="sr-only">Loading…</span>
<!-- Hero Section -->
<section class="hero d-flex flex-column justify-content-center align-items-center">
<h1 class="display-4 fw-bold">Welcome to MemeChat</h1>
<p class="lead my-3">Your AI companion for a brighter, funnier day.</p>
<button id="startChatBtn" class="btn btn-light btn-lg">
Start Chat <i class="bi bi-arrow-down-circle-fill ms-2"></i>
</button>
</section>
<!-- Chat Interface Section -->
<main class="container">
<div id="chatContainer" class="chat-container">
<div class="chat-header">
<h5 class="mb-0">MemeChat AI</h5>
</div>
<div id="chatBox" class="chat-box">
<!-- Chat messages will be appended here -->
</div>
<div class="chat-input">
<form id="chatForm" class="d-flex">
<input type="text" id="chatInput" class="form-control" placeholder="Type your message..." autocomplete="off">
<button type="submit" class="btn btn-primary ms-2">
<i class="bi bi-send-fill"></i> Send
</button>
</form>
</div>
<p class="hint"><?= ($_SERVER['HTTP_HOST'] ?? '') === 'appwizzy.com' ? 'AppWizzy' : 'Flatlogic' ?> AI is collecting your requirements and applying the first changes.</p>
<p class="hint">This page will update automatically as the plan is implemented.</p>
<p>Runtime: PHP <code><?= htmlspecialchars($phpVersion) ?></code> — UTC <code><?= htmlspecialchars($now) ?></code></p>
</div>
</main>
<footer>
Page updated: <?= htmlspecialchars($now) ?> (UTC)
<footer class="text-center py-4">
<p>&copy; <?php echo date("Y"); ?> MemeChat. All Rights Reserved.</p>
</footer>
<!-- Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<!-- Custom JS -->
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
</body>
</html>