-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraitement.php
More file actions
142 lines (126 loc) · 4.75 KB
/
traitement.php
File metadata and controls
142 lines (126 loc) · 4.75 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
<?php
// Do not show any errors
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
error_reporting(0);
$result = "";
// Check if POST content length is too large
if ($_SERVER['CONTENT_LENGTH'] > 50000000) {
http_response_code(400);
// Show error message in $result and exit
header("Location: index.php");
exit;
}
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
require __DIR__ . '/vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
// Récupération de la clé d'API depuis une variable d'environnement
$api_key = $_ENV['API_KEY'];
// Vérification de la présence d'un fichier audio
if (!isset($_FILES['audio-file'])) {
http_response_code(400);
$result = "Aucun fichier audio n'a été envoyé.";
}
// Récupération du fichier audio et vérification de son type
$file = $_FILES['audio-file'];
if ($file['error'] !== UPLOAD_ERR_OK) {
http_response_code(400);
$result = "Erreur lors de l'envoi du fichier : " . $file['error'];
}
if ($file['type'] !== 'audio/mpeg') {
http_response_code(400);
$result = "Le type de fichier " . $file['type'] . " n'est pas supporté. Le fichier doit être de type audio/mpeg.";
}
// Préparation de la requête API
$client = new Client(['headers' => ['Authorization' => 'Bearer ' . $api_key]]);
$url = 'https://api.openai.com/v1/audio/transcriptions';
$options = [
'multipart' => [
[
'name' => 'file',
'contents' => fopen($file['tmp_name'], 'r'),
'filename' => $file['name'],
],
[
'name' => 'model',
'contents' => 'whisper-1',
],
],
];
// Envoi de la requête API et récupération du résultat (mais seulement si le fichier audio est valide)
if ($result === "") {
try {
$result = json_decode($client->post($url, $options)->getBody(), true)['text'];
} catch (RequestException $e) {
http_response_code(400);
$result = "Erreur lors de la transcription de l'audio : " . $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html class="h-screen">
<head>
<meta charset="UTF-8" />
<title>HiberWhisper</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Chargement de Tailwind CSS -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- Chargement de SweetAlert2 -->
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>
<!-- Chargement de la police Ohno Blazeface -->
<link rel="stylesheet" href="https://use.typekit.net/ieo2idj.css" />
</head>
<body class="text-lg">
<!-- Header (only logo on the top left) (anchered) -->
<div class="min-h-screen bg-blue-200 flex items-center justify-center">
<div class="bg-white p-8 ml-4 mr-4 rounded-2xl shadow-sm w-full sm:max-w-screen-md md:max-w-md">
<h1 class="text-4xl mb-4" style="font-family: ohno-blazeface">
HiberWhisper
</h1>
<hr />
<p class="mb-4 mt-4">
Service de transcription d'audio <u>gratuit</u> qui convertit
n'importe quel fichier audio MP3 en texte grâce à l'API d'OpenAI
(Whisper).
</p>
<div id="result-container">
<h2 class="text-lg font-bold mb-2">
Résultat de la HiberWhisperisation :
</h2>
<div id="result-text" class="bg-blue-100 p-4 rounded max-h-96 overflow-y-auto">
<?php echo $result; ?>
</div>
<button id="copy-button" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded mt-4">
Copier
</button>
<button id="return-button" class="bg-gray-500 hover:bg-gray-700 text-white font-bold py-2 px-4 rounded mt-4">
Retour
</button>
</div>
</div>
</div>
<script>
// Copie le texte transcrit dans le presse-papier
const copyButton = document.getElementById("copy-button");
copyButton.addEventListener("click", () => {
const resultText = document.getElementById("result-text");
const text = resultText.innerText;
navigator.clipboard.writeText(text);
Swal.fire({
title: "Copié !",
text: "Le texte a été copié dans le presse-papiers",
icon: "success",
confirmButtonText: "OK",
});
});
// Retour à la page d'accueil
const returnButton = document.getElementById("return-button");
returnButton.addEventListener("click", () => {
window.location.href = "index.php";
});
</script>
</body>
</html>