ultima versao
This commit is contained in:
parent
b358e01b21
commit
5e8f1995f8
53
generate.php
53
generate.php
@ -1,15 +1,6 @@
|
||||
<?php
|
||||
ini_set('display_errors', 1);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
require_once 'ai/LocalAIApi.php';
|
||||
|
||||
// Helper for debugging
|
||||
function debug_log($message) {
|
||||
echo "<pre style=\"background: #eee; padding: 5px; border: 1px solid #ccc;\">DEBUG: " . htmlspecialchars($message) . "</pre>\n";
|
||||
flush();
|
||||
}
|
||||
|
||||
// Function to fetch URL content using cURL
|
||||
function fetch_url_content($url) {
|
||||
if (!filter_var($url, FILTER_VALIDATE_URL)) {
|
||||
@ -89,37 +80,29 @@ $language = $_POST['language'] ?? 'en';
|
||||
$error_message = null;
|
||||
$final_html = '';
|
||||
|
||||
debug_log("Starting process for URL: {$page_url}");
|
||||
|
||||
$fetch_result = fetch_url_content($page_url);
|
||||
|
||||
if (isset($fetch_result['error'])) {
|
||||
$error_message = $fetch_result['error'];
|
||||
debug_log("Error fetching URL: {$error_message}");
|
||||
} else {
|
||||
$html_content = $fetch_result['html'];
|
||||
debug_log("URL content fetched successfully (" . strlen($html_content) . " bytes).");
|
||||
|
||||
if (empty($html_content)) {
|
||||
$error_message = "The fetched HTML content is empty.";
|
||||
debug_log($error_message);
|
||||
} else {
|
||||
$doc = new DOMDocument();
|
||||
@$doc->loadHTML('<?xml encoding="utf-8" ?>' . $html_content);
|
||||
$xpath = new DOMXPath($doc);
|
||||
|
||||
$texts_map = parse_html_and_extract_texts($doc, $xpath);
|
||||
debug_log("Extracted " . count($texts_map) . " text fragments.");
|
||||
|
||||
if (empty($texts_map)) {
|
||||
$error_message = "No visible text could be extracted from the HTML provided.";
|
||||
debug_log($error_message);
|
||||
$final_html = $doc->saveHTML(); // Save the cleaned HTML even if no text was found
|
||||
} else {
|
||||
$prompt_texts = json_encode($texts_map, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
||||
$system_prompt = "Act as an expert direct response copywriter... your JSON output must be flawless."; // Truncated for brevity
|
||||
|
||||
debug_log("Sending to AI API...");
|
||||
$ai_response = LocalAIApi::createResponse([
|
||||
'input' => [
|
||||
['role' => 'system', 'content' => $system_prompt],
|
||||
@ -128,21 +111,17 @@ if (isset($fetch_result['error'])) {
|
||||
]);
|
||||
|
||||
$improved_texts_json = LocalAIApi::extractText($ai_response);
|
||||
debug_log("Received AI response.");
|
||||
|
||||
if (empty($improved_texts_json)) {
|
||||
$error_message = "AI response was empty.";
|
||||
debug_log($error_message . " Raw Response: " . json_encode($ai_response));
|
||||
$final_html = '<!-- AI Error -->';
|
||||
} else {
|
||||
$improved_texts_map = json_decode($improved_texts_json, true);
|
||||
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
$error_message = "Failed to decode AI response. Error: " . json_last_error_msg();
|
||||
debug_log($error_message . " Raw response: " . $improved_texts_json);
|
||||
$final_html = '<!-- AI JSON Decode Error -->';
|
||||
} else {
|
||||
debug_log("AI response decoded. Replacing text nodes...");
|
||||
// Reconstruct the HTML by replacing placeholders in the DOM
|
||||
foreach ($improved_texts_map as $placeholder => $new_text) {
|
||||
$xpath_query = "//text()[. = '{$placeholder}']";
|
||||
@ -152,7 +131,6 @@ if (isset($fetch_result['error'])) {
|
||||
}
|
||||
}
|
||||
$final_html = $doc->saveHTML();
|
||||
debug_log("HTML reconstruction complete.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -174,26 +152,43 @@ if (isset($fetch_result['error'])) {
|
||||
<body class="bg-gray-50 text-gray-800">
|
||||
<div class="container mx-auto px-4 py-8">
|
||||
<div class="max-w-4xl mx-auto">
|
||||
<a href="/"><img src="https://storage.googleapis.com/flatlogic-builder-production-images/brand-logo-with-text-side-v2.svg" alt="Flatlogic" class="h-8 w-auto mb-6"></a>
|
||||
|
||||
|
||||
<?php if (!empty($error_message)): ?>
|
||||
<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative mb-6" role="alert">
|
||||
<strong class="font-bold">An Error Occurred!</strong>
|
||||
<strong class="font-bold">Ocorreu um erro!</strong>
|
||||
<span class="block sm:inline"><?php echo htmlspecialchars($error_message); ?></span>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<h1 class="text-3xl font-bold text-gray-900 mb-4">Your Improved Page is Ready</h1>
|
||||
<p class="text-gray-600 mb-6">The copy has been rewritten for higher conversion. Copy the code below and paste it into your website builder.</p>
|
||||
<h1 class="text-3xl font-bold text-gray-900 mb-4">Sua Página Melhorada está Pronta</h1>
|
||||
<p class="text-gray-600 mb-6">O texto foi reescrito para maior conversão. Copie o código abaixo e cole no seu construtor de sites.</p>
|
||||
|
||||
<div class="bg-white p-2 rounded-lg shadow-md">
|
||||
<textarea readonly class="w-full h-96 border-gray-300 rounded-md font-mono text-sm p-4 result-textarea" onclick="this.select();"><?php echo htmlspecialchars($final_html); ?></textarea>
|
||||
<div class="bg-white p-2 rounded-lg shadow-md relative">
|
||||
<textarea readonly id="result-code" class="w-full h-96 border-gray-300 rounded-md font-mono text-sm p-4 result-textarea" onclick="this.select();"><?php echo htmlspecialchars($final_html); ?></textarea>
|
||||
<button id="copy-button" class="absolute top-2 right-2 bg-gray-700 text-white px-3 py-1 rounded-md text-sm hover:bg-gray-800">Copiar</button>
|
||||
</div>
|
||||
|
||||
<div class="mt-8 text-center">
|
||||
<a href="/" class="bg-indigo-600 text-white font-bold py-2 px-4 rounded-md hover:bg-indigo-700">Create Another Page</a>
|
||||
<a href="/" class="bg-indigo-600 text-white font-bold py-2 px-4 rounded-md hover:bg-indigo-700">Criar Outra Página</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
const copyButton = document.getElementById('copy-button');
|
||||
const resultCode = document.getElementById('result-code');
|
||||
|
||||
copyButton.addEventListener('click', () => {
|
||||
resultCode.select();
|
||||
navigator.clipboard.writeText(resultCode.value).then(() => {
|
||||
copyButton.textContent = 'Copiado!';
|
||||
setTimeout(() => {
|
||||
copyButton.textContent = 'Copiar';
|
||||
}, 2000);
|
||||
}).catch(err => {
|
||||
console.error('Failed to copy: ', err);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@ -21,8 +21,8 @@
|
||||
|
||||
<div class="container my-5">
|
||||
<header class="text-center mb-5">
|
||||
<h1 class="logo display-4">Page Hacker</h1>
|
||||
<p class="lead text-muted">Paste your website URL, and we'll rewrite the copy to be more persuasive and convert better.</p>
|
||||
<h1 class="logo display-4">Page Hack</h1>
|
||||
<p class="lead text-muted">Seja Bem vindo ao Page Hack. Cole a URL do site abaixo que você deseja modelar.</p>
|
||||
</header>
|
||||
|
||||
<div class="row justify-content-center">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user