193 lines
6.5 KiB
PHP
Executable File
193 lines
6.5 KiB
PHP
Executable File
<?php
|
|
$nomFicAppelant = basename(__FILE__);
|
|
$response = "";
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$question = trim($_POST["question"] ?? '');
|
|
$langue = trim($_POST["langue"] ?? 'fr');
|
|
$ton = trim($_POST["ton"] ?? 'neutre');
|
|
|
|
if (empty($question)) {
|
|
$response = "❌ Veuillez entrer une question. ";
|
|
} else {
|
|
$contenu_interdit = ['sex', 'porno', 'nude', 'viol', 'suicide', 'drugs', 'meurtre', 'assassiner', 'pédophilie'];
|
|
foreach ($contenu_interdit as $mot) {
|
|
if (stripos($question, $mot) !== false) {
|
|
$response = "❌ Cette question est interdite pour des raisons éthiques. ";
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (empty($response)) {
|
|
$apiFile = __DIR__ . "/api key.txt";
|
|
if (!file_exists($apiFile)) {
|
|
$response = "❌ Clé API introuvable. ";
|
|
} else {
|
|
$apiKey = trim(file_get_contents($apiFile));
|
|
$prompt = "Réponds en utilisant un ton $ton, en $langue, à la question suivante : $question";
|
|
|
|
$data = [
|
|
"model" => "gpt-3.5-turbo",
|
|
"messages" => [
|
|
["role" => "user", "content" => $prompt]
|
|
],
|
|
"temperature" => 0.7
|
|
];
|
|
|
|
$ch = curl_init("https://api.openai.com/v1/chat/completions");
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
"Content-Type: application/json",
|
|
"Authorization: Bearer $apiKey"
|
|
]);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
|
|
|
$result = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
if ($result) {
|
|
$json = json_decode($result, true);
|
|
if (isset($json["choices"][0]["message"]["content"])) {
|
|
$response = $json["choices"][0]["message"]["content"];
|
|
} else {
|
|
$response = "❌ Erreur de réponse de l'API : " . ($json["error"]["message"] ?? "Réponse vide. ");
|
|
}
|
|
} else {
|
|
$response = "❌ Erreur lors de la communication avec l'API. ";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="fr">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Comment puis-je vous aider ?</title>
|
|
<style>
|
|
body {
|
|
font-family: Arial, sans-serif;
|
|
background-color: #f4f4f4;
|
|
padding: 2em;
|
|
}
|
|
form {
|
|
background-color: #fff;
|
|
max-width: 600px;
|
|
margin: auto;
|
|
padding: 2em;
|
|
border-radius: 8px;
|
|
box-shadow: 0 0 10px rgba(0,0,0,0.1);
|
|
}
|
|
h1 {
|
|
text-align: center;
|
|
}
|
|
label {
|
|
display: block;
|
|
margin-top: 1em;
|
|
font-weight: bold;
|
|
}
|
|
select, textarea, input[type="text"] {
|
|
width: 100%;
|
|
padding: 0.5em;
|
|
margin-top: 0.5em;
|
|
border-radius: 4px;
|
|
border: 1px solid #ccc;
|
|
}
|
|
textarea::placeholder {
|
|
color: #999;
|
|
}
|
|
button {
|
|
margin-top: 1em;
|
|
padding: 0.75em 2em;
|
|
background-color: #28a745;
|
|
color: #fff;
|
|
border: none;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
font-size: 1em;
|
|
}
|
|
button:hover {
|
|
background-color: #218838;
|
|
}
|
|
.spinner {
|
|
display: none;
|
|
margin: 20px auto;
|
|
border: 6px solid #f3f3f3;
|
|
border-top: 6px solid #28a745;
|
|
border-radius: 50%;
|
|
width: 50px;
|
|
height: 50px;
|
|
animation: spin 1s linear infinite;
|
|
}
|
|
@keyframes spin {
|
|
0% {transform: rotate(0deg); }
|
|
100% {transform: rotate(360deg); }
|
|
}
|
|
.result {
|
|
margin-top: 2em;
|
|
background-color: #e6ffe6;
|
|
border-left: 5px solid #28a745;
|
|
padding: 1em;
|
|
border-radius: 4px;
|
|
white-space: pre-wrap;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<form method="POST" onsubmit="return validateForm();">
|
|
<h1>Comment puis-je vous aider ?</h1>
|
|
|
|
<label for="question">Votre question ?</label>
|
|
<textarea name="question" id="question" rows="4" placeholder="Posez votre question ici... " required></textarea>
|
|
|
|
<label for="langue">Langue</label>
|
|
<select name="langue" id="langue" required>
|
|
<option value="fr">Français</option>
|
|
<option value="en">Anglais</option>
|
|
<option value="es">Espagnol</option>
|
|
<option value="de">Allemand</option>
|
|
<option value="it">Italien</option>
|
|
<option value="pt">Portugais</option>
|
|
<option value="nl">Néerlandais</option>
|
|
</select>
|
|
|
|
<label for="ton">Ton de la réponse</label>
|
|
<select name="ton" id="ton" required>
|
|
<option value="neutre">Neutre</option>
|
|
<option value="sérieux">Sérieux</option>
|
|
<option value="enjoué">Enjoué</option>
|
|
<option value="dynamique">Dynamique</option>
|
|
<option value="coach">Coach</option>
|
|
<option value="prof">Prof</option>
|
|
<option value="humoristique">Humoristique</option>
|
|
<option value="bienveillant">Bienveillant</option>
|
|
<option value="motivant">Motivant</option>
|
|
<option value="formel">Formel</option>
|
|
<option value="familier">Familier</option>
|
|
<option value="poétique">Poétique</option>
|
|
<option value="analytique">Analytique</option>
|
|
</select>
|
|
|
|
<button type="submit">Envoyer</button>
|
|
<div class="spinner" id="spinner"></div>
|
|
|
|
<?php if (!empty($response)): ?>
|
|
<div class="result"><?php echo nl2br(htmlspecialchars($response)); ?></div>
|
|
<?php endif; ?>
|
|
</form>
|
|
|
|
<script>
|
|
function validateForm() {
|
|
var question = document.getElementById('question').value.trim();
|
|
if (question === '') {
|
|
alert("Veuillez entrer une question. ");
|
|
return false;
|
|
}
|
|
document.getElementById('spinner').style.display = 'block';
|
|
return true;
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|