timefgf commited on
Commit
c94d36e
Β·
verified Β·
1 Parent(s): 0d012d9

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +63 -111
index.html CHANGED
@@ -3,37 +3,34 @@
3
  <head>
4
  <meta charset="UTF-8" />
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
- <title>μŒμ„± λ²ˆμ—­κΈ° (고속 λͺ¨λ“œ)</title>
7
  <style>
8
  body { font-family: "맑은 κ³ λ”•", sans-serif; max-width: 720px; margin: 24px auto; padding: 0 16px; color: #202124; }
9
  h1 { font-size: 24px; margin-bottom: 4px; }
10
  .sub { color: #5f6368; margin-top: 0; }
11
- #status { display: block; margin: 12px 0; padding: 8px 12px; background: #f1f3f4; border-radius: 6px; word-break: keep-all; line-height: 1.5; font-weight: bold; color: #1a73e8; }
12
  textarea { width: 100%; height: 90px; font-size: 15px; padding: 8px; box-sizing: border-box; resize: none; }
13
  .box { margin-top: 16px; }
14
  h3 { font-size: 16px; margin-bottom: 8px; }
15
- .input-row { display: flex; align-items: center; gap: 12px; margin: 16px 0; flex-wrap: wrap; }
16
  #record-btn { padding: 8px 16px; font-size: 14px; cursor: pointer; border: 1px solid #dadce0; background: white; border-radius: 4px; transition: 0.2s; }
17
  #record-btn.recording { background-color: #fce8e6; color: #d93025; border-color: #d93025; font-weight: bold; animation: blink 1s infinite; }
18
  @keyframes blink { 50% { opacity: 0.7; } }
19
- .divider { color: #5f6368; font-size: 14px; }
20
  </style>
21
  </head>
22
  <body>
23
- <h1>μŒμ„± λ²ˆμ—­κΈ° βš‘κ³ μ† λͺ¨λ“œ</h1>
24
- <p class="sub">ν•œκ΅­μ–΄λ‘œ λ§ν•˜λ©΄ λ°›μ•„ 적고, μ˜μ–΄λ‘œ λ²ˆμ—­ν•΄ 쀌 (μ΅œλŒ€ 30초)</p>
25
 
26
  <div class="input-row">
27
  <button id="record-btn">🎀 λ…ΉμŒ μ‹œμž‘</button>
28
- <span class="divider">λ˜λŠ” μŒμ„± 파일:</span>
29
- <input id="upload" type="file" accept="audio/*" />
30
  </div>
31
 
32
- <span id="status">λͺ¨λΈ λΆˆλŸ¬μ˜€λŠ” 쀑... (μž μ‹œλ§Œ κΈ°λ‹€λ €μ£Όμ„Έμš”)</span>
33
 
34
  <div class="box">
35
  <h3>β‘  μ•Œμ•„λ“€μ€ ν•œκ΅­μ–΄</h3>
36
- <textarea id="korean-text" readonly placeholder="여기에 μΈμ‹λœ ν•œκ΅­μ–΄κ°€ ν‘œμ‹œλ©λ‹ˆλ‹€."></textarea>
37
  </div>
38
 
39
  <div class="box">
@@ -44,145 +41,100 @@
44
  <script type="module">
45
  import { pipeline } from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.7.6";
46
 
47
- // λ‘œλ”© 속도λ₯Ό 20초 μ΄λ‚΄λ‘œ 쀄이기 μœ„ν•΄ λͺ¨λΈμ„ tiny둜 λ³€κ²½
48
- const SPEECH_MODEL = "Xenova/whisper-tiny";
49
- const SPEECH_LANGUAGE = "korean";
50
- const TRANSLATE_MODEL = "Xenova/opus-mt-ko-en";
51
- const MAX_SECONDS = 30; // λ…ΉμŒ μ΅œλŒ€ 길이(초)[cite: 1]
52
 
53
  const statusBox = document.getElementById("status");
54
- const uploadInput = document.getElementById("upload");
55
  const recordBtn = document.getElementById("record-btn");
56
  const koreanBox = document.getElementById("korean-text");
57
  const englishBox = document.getElementById("english-text");
58
 
59
- let transcriber = null;
60
  let translator = null;
61
  let loadSeconds = "";
62
 
63
- // 1. λͺ¨λΈ 뢈러였기
64
  async function loadModels() {
65
  try {
66
  const loadStart = performance.now();
67
- const progressCallback = (data) => {
68
- if (data.status === 'progress') {
69
- statusBox.textContent = `πŸš€ μ΄ˆκ³ μ† λͺ¨λΈ λ‹€μš΄λ‘œλ“œ 쀑... ${Math.round(data.progress)}%`;
70
  }
71
- };
72
-
73
- // 병렬 λ‘œλ”©μœΌλ‘œ 속도 κ·ΉλŒ€ν™”
74
- [transcriber, translator] = await Promise.all([
75
- pipeline("automatic-speech-recognition", SPEECH_MODEL, { progress_callback: progressCallback }),
76
- pipeline("translation", TRANSLATE_MODEL, { progress_callback: progressCallback })
77
- ]);
78
-
79
  loadSeconds = ((performance.now() - loadStart) / 1000).toFixed(1);
80
- statusBox.textContent = `βœ… μ€€λΉ„ μ™„λ£Œ Β· (λ‘œλ”© μ‹œκ°„: ${loadSeconds}초)`;
81
  statusBox.style.color = "#0f9d58";
82
  } catch (error) {
83
- statusBox.textContent = `❌ λͺ¨λΈμ„ λΆˆλŸ¬μ˜€μ§€ λͺ»ν•¨ : ${error.message}`;
84
  statusBox.style.color = "#d93025";
85
  }
86
  }
87
  loadModels();
88
 
89
- // 2. μŒμ„± 처리 ν•¨μˆ˜
90
- async function processAudioData(audioData) {
91
- if (!transcriber || !translator) {
92
- alert("λͺ¨λΈμ΄ 아직 μ€€λΉ„λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. λ‹€μš΄λ‘œλ“œκ°€ μ™„λ£Œλ  λ•ŒκΉŒμ§€ κΈ°λ‹€λ € μ£Όμ„Έμš”.");
93
- return;
94
- }
95
 
96
- try {
97
- statusBox.textContent = "πŸ”„ μŒμ„±μ„ ν…μŠ€νŠΈλ‘œ λ³€ν™˜ν•˜λŠ” 쀑...";
98
- statusBox.style.color = "#1a73e8";
 
 
 
 
 
 
 
 
 
99
  koreanBox.value = "";
100
  englishBox.value = "";
 
101
 
102
- const audioUrl = URL.createObjectURL(audioData);
103
-
104
- const asrStart = performance.now();
105
- const asrResult = await transcriber(audioUrl, {
106
- language: SPEECH_LANGUAGE,
107
- task: "transcribe"
108
- });
109
- const koreanText = asrResult.text.trim();
110
- const asrSeconds = ((performance.now() - asrStart) / 1000).toFixed(1);
111
  koreanBox.value = koreanText;
 
 
112
 
113
- statusBox.textContent = `πŸ”„ λ°›μ•„ 적기 ${asrSeconds}초 Β· λ²ˆμ—­ν•˜λŠ” 쀑...`;
114
- const transStart = performance.now();
115
-
116
- const transResult = await translator(koreanText);
117
- const englishText = transResult[0].translation_text;
118
- const transSeconds = ((performance.now() - transStart) / 1000).toFixed(1);
119
- englishBox.value = englishText;
120
-
121
- statusBox.textContent = `✨ μ™„λ£Œ Β· λ°›μ•„ 적기 ${asrSeconds}초 Β· λ²ˆμ—­ ${transSeconds}초`;
122
- statusBox.style.color = "#0f9d58";
123
- } catch (error) {
124
- statusBox.textContent = `❌ μ‹€ν–‰ 쀑 였λ₯˜ λ°œμƒ : ${error.message}`;
125
- statusBox.style.color = "#d93025";
126
- }
127
- }
128
 
129
- uploadInput.addEventListener("change", (event) => {
130
- const file = event.target.files[0];
131
- if (file) processAudioData(file);
132
- });
133
 
134
- // 3. 마이크 λ…ΉμŒ μ œμ–΄ 둜직
135
- let mediaRecorder;
136
- let audioChunks = [];
137
- let isRecording = false;
138
- let recordingTimeout;
 
139
 
140
  function stopRecording() {
141
- if (mediaRecorder && mediaRecorder.state !== "inactive") {
142
- mediaRecorder.stop();
143
  }
144
  isRecording = false;
145
- clearTimeout(recordingTimeout);
146
  recordBtn.textContent = "🎀 λ…ΉμŒ μ‹œμž‘";
147
  recordBtn.classList.remove("recording");
148
  }
149
 
150
- recordBtn.addEventListener("click", async () => {
151
- if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
152
- alert("ν˜„μž¬ λΈŒλΌμš°μ €μ—μ„œ 마이크λ₯Ό μ§€μ›ν•˜μ§€ μ•Šκ±°λ‚˜ κΆŒν•œμ΄ μ°¨λ‹¨λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.\nμ„€μ •μ—μ„œ 마이크 κΆŒν•œμ„ ν—ˆμš©ν•΄μ£Όμ„Έμš”.");
153
- return;
154
- }
155
-
156
  if (!isRecording) {
157
- try {
158
- const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
159
- mediaRecorder = new MediaRecorder(stream);
160
- audioChunks = [];
161
-
162
- mediaRecorder.ondataavailable = (e) => {
163
- if (e.data.size > 0) audioChunks.push(e.data);
164
- };
165
-
166
- mediaRecorder.onstop = () => {
167
- const audioBlob = new Blob(audioChunks);
168
- processAudioData(audioBlob);
169
- stream.getTracks().forEach(track => track.stop());
170
- };
171
-
172
- mediaRecorder.start();
173
- isRecording = true;
174
- recordBtn.textContent = "⏹️ λ…ΉμŒ 쀑지";
175
- recordBtn.classList.add("recording");
176
- statusBox.textContent = `πŸŽ™οΈ λ…ΉμŒ 쀑... (μ΅œλŒ€ ${MAX_SECONDS}초)`;[cite: 1]
177
- statusBox.style.color = "#d93025";
178
-
179
- recordingTimeout = setTimeout(() => {
180
- if (isRecording) stopRecording();
181
- }, MAX_SECONDS * 1000);[cite: 1]
182
-
183
- } catch (err) {
184
- alert(`마이크 μ ‘κ·Ό 였λ₯˜: ${err.message}\nλΈŒλΌμš°μ € μ„€μ •μ—μ„œ 마이크 κΆŒν•œμ„ ν—ˆμš©ν•΄ μ£Όμ„Έμš”.`);
185
- }
186
  } else {
187
  stopRecording();
188
  }
 
3
  <head>
4
  <meta charset="UTF-8" />
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>μŒμ„± λ²ˆμ—­κΈ° (μ¦‰μ‹œ μ‹€ν–‰ λͺ¨λ“œ)</title>
7
  <style>
8
  body { font-family: "맑은 κ³ λ”•", sans-serif; max-width: 720px; margin: 24px auto; padding: 0 16px; color: #202124; }
9
  h1 { font-size: 24px; margin-bottom: 4px; }
10
  .sub { color: #5f6368; margin-top: 0; }
11
+ #status { display: block; margin: 12px 0; padding: 8px 12px; background: #e8f0fe; border-radius: 6px; font-weight: bold; color: #1a73e8; }
12
  textarea { width: 100%; height: 90px; font-size: 15px; padding: 8px; box-sizing: border-box; resize: none; }
13
  .box { margin-top: 16px; }
14
  h3 { font-size: 16px; margin-bottom: 8px; }
15
+ .input-row { display: flex; align-items: center; gap: 12px; margin: 16px 0; }
16
  #record-btn { padding: 8px 16px; font-size: 14px; cursor: pointer; border: 1px solid #dadce0; background: white; border-radius: 4px; transition: 0.2s; }
17
  #record-btn.recording { background-color: #fce8e6; color: #d93025; border-color: #d93025; font-weight: bold; animation: blink 1s infinite; }
18
  @keyframes blink { 50% { opacity: 0.7; } }
 
19
  </style>
20
  </head>
21
  <body>
22
+ <h1>μŒμ„± λ²ˆμ—­κΈ° βš‘μ¦‰μ‹œ μ‹€ν–‰ λͺ¨λ“œ</h1>
23
+ <p class="sub">ν•œκ΅­μ–΄λ‘œ λ§ν•˜λ©΄ λΈŒλΌμš°μ € λ‚΄μž₯ API둜 μ¦‰μ‹œ μΈμ‹ν•˜κ³  λ²ˆμ—­ν•΄ μ€λ‹ˆλ‹€.</p>
24
 
25
  <div class="input-row">
26
  <button id="record-btn">🎀 λ…ΉμŒ μ‹œμž‘</button>
 
 
27
  </div>
28
 
29
+ <span id="status">λ²ˆμ—­ λͺ¨λΈ λΆˆλŸ¬μ˜€λŠ” 쀑... (μ•½ 70MB)</span>
30
 
31
  <div class="box">
32
  <h3>β‘  μ•Œμ•„λ“€μ€ ν•œκ΅­μ–΄</h3>
33
+ <textarea id="korean-text" readonly placeholder="λ§μ”€ν•˜μ‹œλ©΄ 여기에 μ¦‰μ‹œ ν‘œμ‹œλ©λ‹ˆλ‹€."></textarea>
34
  </div>
35
 
36
  <div class="box">
 
41
  <script type="module">
42
  import { pipeline } from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.7.6";
43
 
44
+ // STT λͺ¨λΈμ€ μ œμ™Έν•˜κ³  λ²ˆμ—­ λͺ¨λΈλ§Œ λ‘œλ“œν•˜μ—¬ 속도 κ·ΉλŒ€ν™”
45
+ const TRANSLATE_MODEL = "Xenova/opus-mt-ko-en";[cite: 1]
 
 
 
46
 
47
  const statusBox = document.getElementById("status");
 
48
  const recordBtn = document.getElementById("record-btn");
49
  const koreanBox = document.getElementById("korean-text");
50
  const englishBox = document.getElementById("english-text");
51
 
 
52
  let translator = null;
53
  let loadSeconds = "";
54
 
55
+ // 1. λ²ˆμ—­ λͺ¨λΈλ§Œ λ‘œλ“œ
56
  async function loadModels() {
57
  try {
58
  const loadStart = performance.now();
59
+ translator = await pipeline("translation", TRANSLATE_MODEL, {
60
+ progress_callback: data => {
61
+ if (data.status === 'progress') statusBox.textContent = `λ²ˆμ—­ λͺ¨λΈ λ‹€μš΄λ‘œλ“œ 쀑... ${Math.round(data.progress)}%`;
62
  }
63
+ });
 
 
 
 
 
 
 
64
  loadSeconds = ((performance.now() - loadStart) / 1000).toFixed(1);
65
+ statusBox.textContent = `βœ… μ€€λΉ„ μ™„λ£Œ (λ²ˆμ—­ λ‘œλ”©: ${loadSeconds}초)`;
66
  statusBox.style.color = "#0f9d58";
67
  } catch (error) {
68
+ statusBox.textContent = `❌ λͺ¨λΈ λ‘œλ“œ μ‹€νŒ¨: ${error.message}`;
69
  statusBox.style.color = "#d93025";
70
  }
71
  }
72
  loadModels();
73
 
74
+ // 2. λΈŒλΌμš°μ € λ‚΄μž₯ Web Speech API μ„€μ • (λ‹€μš΄λ‘œλ“œ μ—†μŒ, μ¦‰μ‹œ 인식)
75
+ const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
76
+ let recognition;
77
+ let isRecording = false;
 
 
78
 
79
+ if (SpeechRecognition) {
80
+ recognition = new SpeechRecognition();
81
+ recognition.lang = 'ko-KR';
82
+ recognition.interimResults = false;
83
+ recognition.maxAlternatives = 1;
84
+
85
+ recognition.onstart = () => {
86
+ isRecording = true;
87
+ recordBtn.textContent = "⏹️ λ…ΉμŒ 쀑지";
88
+ recordBtn.classList.add("recording");
89
+ statusBox.textContent = "πŸŽ™οΈ λ“£κ³  μžˆμŠ΅λ‹ˆλ‹€...";
90
+ statusBox.style.color = "#d93025";
91
  koreanBox.value = "";
92
  englishBox.value = "";
93
+ };
94
 
95
+ // μŒμ„± 인식이 μ™„λ£Œλ˜μ—ˆμ„ λ•Œ
96
+ recognition.onresult = async (event) => {
97
+ const koreanText = event.results[0][0].transcript;
 
 
 
 
 
 
98
  koreanBox.value = koreanText;
99
+ statusBox.textContent = "πŸ”„ λ²ˆμ—­ν•˜λŠ” 쀑...";
100
+ statusBox.style.color = "#1a73e8";
101
 
102
+ if (translator) {
103
+ const transStart = performance.now();
104
+ const transResult = await translator(koreanText);
105
+ const transSeconds = ((performance.now() - transStart) / 1000).toFixed(1);
106
+ englishBox.value = transResult[0].translation_text;
107
+ statusBox.textContent = `✨ μ™„λ£Œ Β· (λ²ˆμ—­ μ†Œμš”μ‹œκ°„: ${transSeconds}초)`;
108
+ statusBox.style.color = "#0f9d58";
109
+ } else {
110
+ statusBox.textContent = "❌ λ²ˆμ—­ λͺ¨λΈμ΄ 아직 λ‘œλ“œλ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.";
111
+ }
112
+ };
 
 
 
 
113
 
114
+ recognition.onerror = (event) => {
115
+ statusBox.textContent = `❌ μŒμ„± 인식 였λ₯˜: ${event.error}`;
116
+ stopRecording();
117
+ };
118
 
119
+ recognition.onend = () => {
120
+ if (isRecording) stopRecording();
121
+ };
122
+ } else {
123
+ alert("이 λΈŒλΌμš°μ €λŠ” Web Speech APIλ₯Ό μ§€μ›ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. Chrome을 μ‚¬μš©ν•΄ μ£Όμ„Έμš”.");
124
+ }
125
 
126
  function stopRecording() {
127
+ if (recognition && isRecording) {
128
+ recognition.stop();
129
  }
130
  isRecording = false;
 
131
  recordBtn.textContent = "🎀 λ…ΉμŒ μ‹œμž‘";
132
  recordBtn.classList.remove("recording");
133
  }
134
 
135
+ recordBtn.addEventListener("click", () => {
 
 
 
 
 
136
  if (!isRecording) {
137
+ if (recognition) recognition.start();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  } else {
139
  stopRecording();
140
  }