daihui.zhang
commited on
Commit
·
c96e5e2
1
Parent(s):
138d976
update api entrypoint
Browse files- frontend/index.html +2 -48
- main.py +1 -1
frontend/index.html
CHANGED
|
@@ -10,52 +10,6 @@
|
|
| 10 |
</head>
|
| 11 |
<body>
|
| 12 |
<div id="app"></div>
|
|
|
|
| 13 |
</body>
|
| 14 |
-
|
| 15 |
-
async function startRecording() {
|
| 16 |
-
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
| 17 |
-
const audioContext = new AudioContext({ sampleRate: 16000 });
|
| 18 |
-
const source = audioContext.createMediaStreamSource(stream);
|
| 19 |
-
const processor = audioContext.createScriptProcessor(4096, 1, 1);
|
| 20 |
-
|
| 21 |
-
const wsUrl = "ws://localhost:9090/ws?from=zh&to=en";
|
| 22 |
-
ws = new WebSocket(wsUrl);
|
| 23 |
-
|
| 24 |
-
ws.binaryType = "arraybuffer";
|
| 25 |
-
|
| 26 |
-
ws.onopen = () => {
|
| 27 |
-
console.log("WebSocket opened");
|
| 28 |
-
source.connect(processor);
|
| 29 |
-
processor.connect(audioContext.destination);
|
| 30 |
-
|
| 31 |
-
processor.onaudioprocess = (e) => {
|
| 32 |
-
const input = e.inputBuffer.getChannelData(0);
|
| 33 |
-
const buffer = new Int16Array(input.length);
|
| 34 |
-
for (let i = 0; i < input.length; i++) {
|
| 35 |
-
buffer[i] = Math.max(-1, Math.min(1, input[i])) * 0x7FFF;
|
| 36 |
-
}
|
| 37 |
-
ws.send(buffer);
|
| 38 |
-
};
|
| 39 |
-
};
|
| 40 |
-
|
| 41 |
-
ws.onmessage = (event) => {
|
| 42 |
-
try {
|
| 43 |
-
const msg = JSON.parse(event.data);
|
| 44 |
-
if (msg.result) {
|
| 45 |
-
addTranslation(msg.result);
|
| 46 |
-
}
|
| 47 |
-
} catch (e) {
|
| 48 |
-
console.error("Parse error:", e);
|
| 49 |
-
}
|
| 50 |
-
};
|
| 51 |
-
|
| 52 |
-
ws.onerror = (e) => console.error("WebSocket error:", e);
|
| 53 |
-
ws.onclose = () => {
|
| 54 |
-
console.log("WebSocket closed");
|
| 55 |
-
processor.disconnect();
|
| 56 |
-
source.disconnect();
|
| 57 |
-
};
|
| 58 |
-
}
|
| 59 |
-
</script>
|
| 60 |
-
</body>
|
| 61 |
-
</html>
|
|
|
|
| 10 |
</head>
|
| 11 |
<body>
|
| 12 |
<div id="app"></div>
|
| 13 |
+
|
| 14 |
</body>
|
| 15 |
+
</html>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
main.py
CHANGED
|
@@ -39,7 +39,7 @@ async def lifespan(app:FastAPI):
|
|
| 39 |
|
| 40 |
|
| 41 |
app = FastAPI(lifespan=lifespan)
|
| 42 |
-
app.mount("/
|
| 43 |
pipe = None
|
| 44 |
|
| 45 |
@app.websocket("/ws")
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
app = FastAPI(lifespan=lifespan)
|
| 42 |
+
app.mount("/trans", StaticFiles(directory="frontend"),)
|
| 43 |
pipe = None
|
| 44 |
|
| 45 |
@app.websocket("/ws")
|