-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstep5.html
137 lines (120 loc) · 4.61 KB
/
step5.html
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
<!DOCTYPE html>
<html>
<head>
<script src="https://www.WebRTC-Experiment.com/RecordRTC.js"></script>
<title>OpenAI Web Project</title>
</head>
<body>
<h1>OpenAI gpt-3 DALL.E Whisper Integration Project</h1>
<p>OpenAI API Key:</p>
<input type="text" id="apiKey">
<br><br>
<p>Recording:</p>
<button onclick="startRecording()">Start Recording</button>
<button onclick="stopRecording()">Stop Recording</button>
<br><br>
<audio controls></audio>
<p>Voice Content:</p>
<p id="voiceResult"></p>
<br><br>
<button onclick="generateTitle()">Create Title</button>
<br><br>
<p id="result"></p>
<img id="pic1" src="" width="256px" height="256px"/>
<script>
let API_KEY = "";
let mediaRecorder;
let recordedChunks = [];
async function startRecording() {
try {
API_KEY = document.getElementById("apiKey").value;
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const options = { mimeType: 'audio/webm' };
mediaRecorder = new RecordRTCPromisesHandler(stream, {
type: 'audio',
mimeType:"audio/wav",
recorderType: StereoAudioRecorder,
});
mediaRecorder.startRecording();
} catch (error) {
console.error('錄音失敗:', error);
}
}
async function stopRecording() {
if (mediaRecorder && mediaRecorder.state !== 'inactive') {
await mediaRecorder.stopRecording();
saveAudio(await mediaRecorder.getBlob())
}
}
function saveAudio(blob) {
const reader = new FileReader();
reader.onloadend = async () => {
const audioElement = document.querySelector('audio');
audioElement.src = reader.result;
const model = "whisper-1";
let formData = new FormData();
formData.append('file', new File([blob], "name.mp3",{type:"audio/mp3"}) );
formData.append('model', model);
const response = await fetch(`https://api.openai.com/v1/audio/transcriptions`,
{
body: formData,
method: "POST",
headers: {
Authorization: `Bearer ${API_KEY}`,
},
}
)
let data = await response.json();
console.log(`Title: ${JSON.stringify(data)}`);
const title = data.text.trim();
document.getElementById("voiceResult").innerHTML = title;
}
reader.readAsDataURL(blob);
}
async function generateTitle() {
const keyword = document.getElementById("voiceResult").textContent;
const prompt = `Generate an article title related to "${keyword}`;
const model = "gpt-3.5-turbo";
try {
const response = await fetch(`https://api.openai.com/v1/chat/completions`,
{
body: JSON.stringify({"model": model, "messages": [{"role": "user", "content": prompt}], "max_tokens": 20}),
method: "POST",
headers: {
"content-type": "application/json",
Authorization: `Bearer ${API_KEY}`,
},
}
)
let data = await response.json();
console.log(`Title: ${JSON.stringify(data)}`);
const title = data.choices[0].message.content.trim();
document.getElementById("result").innerHTML = title;
await generateImage()
} catch(error) {
console.log(`Error: ${error}`);
}
}
async function generateImage(){
try {
const prompt = document.getElementById('result').textContent;
const response = await fetch(`https://api.openai.com/v1/images/generations`,
{
body: JSON.stringify({"prompt":prompt, n:1, size:"256x256"}),
method: "POST",
headers: {
"content-type": "application/json",
Authorization: `Bearer ${API_KEY}`,
},
}
)
let data = await response.json();
console.log(`Image: ${JSON.stringify(data)}`);
document.getElementById("pic1").src= data.data[0].url;
} catch(error) {
console.log(`Error: ${error}`);
}
}
</script>
</body>
</html>