Spaces:
Running
Running
fix "not working on HF Spaces but working locally"
Browse files- app/main.py +21 -0
- app/static/index.html +1 -1
app/main.py
CHANGED
@@ -5,6 +5,27 @@ from app.asr_worker import create_recognizer, stream_audio, finalize_stream
|
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
app.mount("/static", StaticFiles(directory="app/static"), name="static")
|
9 |
|
10 |
recognizer = create_recognizer()
|
|
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
8 |
+
@app.websocket("/ws")
|
9 |
+
async def websocket_endpoint(websocket: WebSocket):
|
10 |
+
await websocket.accept()
|
11 |
+
print("[INFO] WebSocket connection accepted.")
|
12 |
+
stream = recognizer.create_stream()
|
13 |
+
|
14 |
+
try:
|
15 |
+
while True:
|
16 |
+
data = await websocket.receive_bytes()
|
17 |
+
print(f"[DEBUG] Received {len(data)} bytes")
|
18 |
+
result, rms = stream_audio(data, stream, recognizer)
|
19 |
+
await websocket.send_json({
|
20 |
+
"partial": result,
|
21 |
+
"volume": min(rms * 5.0, 1.0)
|
22 |
+
})
|
23 |
+
except Exception as e:
|
24 |
+
print(f"[ERROR] {e}")
|
25 |
+
final = finalize_stream(stream, recognizer)
|
26 |
+
await websocket.send_json({"final": final})
|
27 |
+
await websocket.close()
|
28 |
+
|
29 |
app.mount("/static", StaticFiles(directory="app/static"), name="static")
|
30 |
|
31 |
recognizer = create_recognizer()
|
app/static/index.html
CHANGED
@@ -9,7 +9,7 @@
|
|
9 |
<p>Partial: <span id="partial"></span></p>
|
10 |
<p>Final: <b id="final"></b></p>
|
11 |
<script>
|
12 |
-
const ws = new WebSocket("
|
13 |
const vol = document.getElementById("vol");
|
14 |
const partial = document.getElementById("partial");
|
15 |
const finalText = document.getElementById("final");
|
|
|
9 |
<p>Partial: <span id="partial"></span></p>
|
10 |
<p>Final: <b id="final"></b></p>
|
11 |
<script>
|
12 |
+
const ws = new WebSocket("wss://" + location.host + "/ws");
|
13 |
const vol = document.getElementById("vol");
|
14 |
const partial = document.getElementById("partial");
|
15 |
const finalText = document.getElementById("final");
|