Spaces:
Sleeping
Sleeping
updated
Browse files- app.py +16 -29
- header.webm +0 -0
- templates/index.html +2 -3
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
-
os.environ[
|
| 3 |
|
| 4 |
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Request
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
|
@@ -13,7 +13,6 @@ import logging
|
|
| 13 |
import io
|
| 14 |
from pydub import AudioSegment
|
| 15 |
from typing import List
|
| 16 |
-
import asyncio
|
| 17 |
|
| 18 |
logging.basicConfig(level=logging.INFO)
|
| 19 |
logger = logging.getLogger(__name__)
|
|
@@ -22,7 +21,7 @@ app = FastAPI()
|
|
| 22 |
|
| 23 |
@app.get("/", response_class=HTMLResponse)
|
| 24 |
async def get(request: Request):
|
| 25 |
-
logger.info("
|
| 26 |
with open("templates/index.html") as f:
|
| 27 |
html_content = f.read()
|
| 28 |
return HTMLResponse(content=html_content, status_code=200)
|
|
@@ -40,36 +39,23 @@ app.add_middleware(
|
|
| 40 |
)
|
| 41 |
|
| 42 |
is_detecting = False
|
| 43 |
-
detection_thread = None
|
| 44 |
|
| 45 |
model = joblib.load('models/xgb_test.pkl')
|
| 46 |
-
|
| 47 |
class ConnectionManager:
|
| 48 |
def __init__(self):
|
| 49 |
self.active_connections: List[WebSocket] = []
|
| 50 |
-
self.audio_buffers = {}
|
| 51 |
|
| 52 |
async def connect(self, websocket: WebSocket):
|
| 53 |
await websocket.accept()
|
| 54 |
self.active_connections.append(websocket)
|
| 55 |
-
self.audio_buffers[websocket] = b'' # Initialize buffer for each connection
|
| 56 |
|
| 57 |
def disconnect(self, websocket: WebSocket):
|
| 58 |
self.active_connections.remove(websocket)
|
| 59 |
-
del self.audio_buffers[websocket] # Clean up buffer
|
| 60 |
|
| 61 |
async def send_message(self, websocket: WebSocket, message: str):
|
| 62 |
await websocket.send_text(message)
|
| 63 |
|
| 64 |
-
def add_to_buffer(self, websocket: WebSocket, data: bytes):
|
| 65 |
-
self.audio_buffers[websocket] += data # Accumulate data in the buffer
|
| 66 |
-
|
| 67 |
-
def get_buffer(self, websocket: WebSocket) -> bytes:
|
| 68 |
-
return self.audio_buffers[websocket]
|
| 69 |
-
|
| 70 |
-
def clear_buffer(self, websocket: WebSocket):
|
| 71 |
-
self.audio_buffers[websocket] = b'' # Clear the buffer
|
| 72 |
-
|
| 73 |
manager = ConnectionManager()
|
| 74 |
|
| 75 |
def extract_features(audio):
|
|
@@ -89,19 +75,20 @@ def extract_features(audio):
|
|
| 89 |
combined_features = np.hstack([mfccs, chroma, contrast, centroid])
|
| 90 |
return combined_features
|
| 91 |
|
| 92 |
-
async def process_audio_data(
|
| 93 |
-
audio_data = manager.get_buffer(websocket)
|
| 94 |
try:
|
| 95 |
-
#
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
wav_io = io.BytesIO()
|
| 99 |
audio_segment.export(wav_io, format="wav")
|
| 100 |
-
logging.info("audio_segment wav worked")
|
| 101 |
wav_io.seek(0)
|
| 102 |
-
logging.info("wav io worked")
|
| 103 |
audio, sr = sf.read(wav_io, dtype='float32')
|
| 104 |
-
logging.info("sf worked")
|
| 105 |
except Exception as e:
|
| 106 |
logger.error(f"Failed to read audio data: {e}")
|
| 107 |
return
|
|
@@ -115,8 +102,7 @@ async def process_audio_data(websocket: WebSocket):
|
|
| 115 |
is_fake = prediction[0]
|
| 116 |
result = 'fake' if is_fake else 'real'
|
| 117 |
|
| 118 |
-
|
| 119 |
-
manager.clear_buffer(websocket)
|
| 120 |
|
| 121 |
@app.post("/start_detection")
|
| 122 |
async def start_detection():
|
|
@@ -138,8 +124,9 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
| 138 |
try:
|
| 139 |
while True:
|
| 140 |
data = await websocket.receive_bytes()
|
| 141 |
-
|
| 142 |
-
|
|
|
|
| 143 |
except WebSocketDisconnect:
|
| 144 |
manager.disconnect(websocket)
|
| 145 |
|
|
|
|
| 1 |
import os
|
| 2 |
+
os.environ['NUMBA_CACHE_DIR'] = '/tmp/'
|
| 3 |
|
| 4 |
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Request
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 13 |
import io
|
| 14 |
from pydub import AudioSegment
|
| 15 |
from typing import List
|
|
|
|
| 16 |
|
| 17 |
logging.basicConfig(level=logging.INFO)
|
| 18 |
logger = logging.getLogger(__name__)
|
|
|
|
| 21 |
|
| 22 |
@app.get("/", response_class=HTMLResponse)
|
| 23 |
async def get(request: Request):
|
| 24 |
+
logger.info("Serving the index page")
|
| 25 |
with open("templates/index.html") as f:
|
| 26 |
html_content = f.read()
|
| 27 |
return HTMLResponse(content=html_content, status_code=200)
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
is_detecting = False
|
|
|
|
| 42 |
|
| 43 |
model = joblib.load('models/xgb_test.pkl')
|
| 44 |
+
|
| 45 |
class ConnectionManager:
|
| 46 |
def __init__(self):
|
| 47 |
self.active_connections: List[WebSocket] = []
|
|
|
|
| 48 |
|
| 49 |
async def connect(self, websocket: WebSocket):
|
| 50 |
await websocket.accept()
|
| 51 |
self.active_connections.append(websocket)
|
|
|
|
| 52 |
|
| 53 |
def disconnect(self, websocket: WebSocket):
|
| 54 |
self.active_connections.remove(websocket)
|
|
|
|
| 55 |
|
| 56 |
async def send_message(self, websocket: WebSocket, message: str):
|
| 57 |
await websocket.send_text(message)
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
manager = ConnectionManager()
|
| 60 |
|
| 61 |
def extract_features(audio):
|
|
|
|
| 75 |
combined_features = np.hstack([mfccs, chroma, contrast, centroid])
|
| 76 |
return combined_features
|
| 77 |
|
| 78 |
+
async def process_audio_data(audio_data):
|
|
|
|
| 79 |
try:
|
| 80 |
+
# Prepend header to the audio data
|
| 81 |
+
with open("header.webm", 'rb') as source_file:
|
| 82 |
+
header_data = source_file.read(1024)
|
| 83 |
+
|
| 84 |
+
full_audio_data = header_data + audio_data
|
| 85 |
+
|
| 86 |
+
# Convert audio data from webm to wav format using pydub
|
| 87 |
+
audio_segment = AudioSegment.from_file(io.BytesIO(full_audio_data), format="webm")
|
| 88 |
wav_io = io.BytesIO()
|
| 89 |
audio_segment.export(wav_io, format="wav")
|
|
|
|
| 90 |
wav_io.seek(0)
|
|
|
|
| 91 |
audio, sr = sf.read(wav_io, dtype='float32')
|
|
|
|
| 92 |
except Exception as e:
|
| 93 |
logger.error(f"Failed to read audio data: {e}")
|
| 94 |
return
|
|
|
|
| 102 |
is_fake = prediction[0]
|
| 103 |
result = 'fake' if is_fake else 'real'
|
| 104 |
|
| 105 |
+
return result
|
|
|
|
| 106 |
|
| 107 |
@app.post("/start_detection")
|
| 108 |
async def start_detection():
|
|
|
|
| 124 |
try:
|
| 125 |
while True:
|
| 126 |
data = await websocket.receive_bytes()
|
| 127 |
+
result = await process_audio_data(data)
|
| 128 |
+
if result:
|
| 129 |
+
await manager.send_message(websocket, result)
|
| 130 |
except WebSocketDisconnect:
|
| 131 |
manager.disconnect(websocket)
|
| 132 |
|
header.webm
ADDED
|
Binary file (18.3 kB). View file
|
|
|
templates/index.html
CHANGED
|
@@ -90,14 +90,12 @@
|
|
| 90 |
logMessage('WebSocket connection closed', 'info');
|
| 91 |
};
|
| 92 |
|
| 93 |
-
mediaRecorder.start(
|
| 94 |
} catch (error) {
|
| 95 |
logMessage(`Error: ${error.message}`, 'error');
|
| 96 |
}
|
| 97 |
}
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
async function stopDetection() {
|
| 102 |
if (!isDetecting) {
|
| 103 |
logMessage('Detection is not running...', 'info');
|
|
@@ -138,3 +136,4 @@
|
|
| 138 |
</script>
|
| 139 |
</body>
|
| 140 |
</html>
|
|
|
|
|
|
| 90 |
logMessage('WebSocket connection closed', 'info');
|
| 91 |
};
|
| 92 |
|
| 93 |
+
mediaRecorder.start(5000); // Start recording with 3-second intervals
|
| 94 |
} catch (error) {
|
| 95 |
logMessage(`Error: ${error.message}`, 'error');
|
| 96 |
}
|
| 97 |
}
|
| 98 |
|
|
|
|
|
|
|
| 99 |
async function stopDetection() {
|
| 100 |
if (!isDetecting) {
|
| 101 |
logMessage('Detection is not running...', 'info');
|
|
|
|
| 136 |
</script>
|
| 137 |
</body>
|
| 138 |
</html>
|
| 139 |
+
|