Spaces:
Runtime error
Runtime error
added not real time detection option
Browse files- main.py +33 -1
- templates/index.html +63 -4
main.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 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
|
| 6 |
from fastapi.responses import JSONResponse, HTMLResponse
|
| 7 |
from fastapi.staticfiles import StaticFiles
|
|
@@ -138,6 +138,38 @@ async def stop_detection():
|
|
| 138 |
is_detecting = False
|
| 139 |
return JSONResponse(content={'status': 'detection_stopped'})
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
@app.websocket("/ws")
|
| 142 |
async def websocket_endpoint(websocket: WebSocket):
|
| 143 |
await manager.connect(websocket)
|
|
|
|
| 1 |
import os
|
| 2 |
os.environ['NUMBA_CACHE_DIR'] = '/tmp/'
|
| 3 |
|
| 4 |
+
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Request, UploadFile, File
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
from fastapi.responses import JSONResponse, HTMLResponse
|
| 7 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
| 138 |
is_detecting = False
|
| 139 |
return JSONResponse(content={'status': 'detection_stopped'})
|
| 140 |
|
| 141 |
+
@app.post("/upload_audio/")
|
| 142 |
+
async def upload_audio(file: UploadFile = File(...)):
|
| 143 |
+
try:
|
| 144 |
+
# Read the uploaded file
|
| 145 |
+
audio_data = await file.read()
|
| 146 |
+
|
| 147 |
+
# Convert the audio file to the appropriate format for processing
|
| 148 |
+
audio_segment = AudioSegment.from_file(io.BytesIO(audio_data), format=file.filename.split('.')[-1])
|
| 149 |
+
wav_io = io.BytesIO()
|
| 150 |
+
audio_segment.export(wav_io, format="wav")
|
| 151 |
+
wav_io.seek(0)
|
| 152 |
+
audio, sr = sf.read(wav_io, dtype='float32')
|
| 153 |
+
|
| 154 |
+
# If audio has more than one channel, average them
|
| 155 |
+
if audio.ndim > 1:
|
| 156 |
+
audio = np.mean(audio, axis=1)
|
| 157 |
+
|
| 158 |
+
# Extract features
|
| 159 |
+
features = extract_features(audio)
|
| 160 |
+
features = scaler.transform(features.reshape(1, -1))
|
| 161 |
+
|
| 162 |
+
# Predict using the loaded model
|
| 163 |
+
prediction = model.predict(features)
|
| 164 |
+
is_fake = prediction[0]
|
| 165 |
+
result = 'fake' if is_fake else 'real'
|
| 166 |
+
|
| 167 |
+
return JSONResponse(content={'status': 'success', 'result': result})
|
| 168 |
+
|
| 169 |
+
except Exception as e:
|
| 170 |
+
logger.error(f"Failed to process audio file: {e}")
|
| 171 |
+
return JSONResponse(content={'status': 'error', 'message': str(e)}, status_code=500)
|
| 172 |
+
|
| 173 |
@app.websocket("/ws")
|
| 174 |
async def websocket_endpoint(websocket: WebSocket):
|
| 175 |
await manager.connect(websocket)
|
templates/index.html
CHANGED
|
@@ -74,6 +74,26 @@
|
|
| 74 |
border-radius: 5px;
|
| 75 |
display: none;
|
| 76 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
</style>
|
| 78 |
</head>
|
| 79 |
<body>
|
|
@@ -86,6 +106,13 @@
|
|
| 86 |
<div id="console"></div>
|
| 87 |
<div id="notification">Fake Call Detected!</div>
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
<script>
|
| 90 |
let isDetecting = false;
|
| 91 |
const startButton = document.getElementById('startButton');
|
|
@@ -131,8 +158,8 @@
|
|
| 131 |
}
|
| 132 |
};
|
| 133 |
|
| 134 |
-
const response = await fetch(`${window.location.origin}/start_detection`, {
|
| 135 |
-
|
| 136 |
method: 'POST',
|
| 137 |
});
|
| 138 |
|
|
@@ -143,8 +170,8 @@
|
|
| 143 |
const result = await response.json();
|
| 144 |
logMessage(`Status: ${result.status}`, 'info');
|
| 145 |
|
| 146 |
-
websocket = new WebSocket(`wss://${window.location.host}/ws`);
|
| 147 |
-
|
| 148 |
websocket.onmessage = async function(event) {
|
| 149 |
const data = event.data;
|
| 150 |
if (data === 'global-fake') {
|
|
@@ -202,6 +229,38 @@
|
|
| 202 |
|
| 203 |
startButton.addEventListener('click', startDetection);
|
| 204 |
stopButton.addEventListener('click', stopDetection);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
</script>
|
| 206 |
</body>
|
| 207 |
</html>
|
|
|
|
| 74 |
border-radius: 5px;
|
| 75 |
display: none;
|
| 76 |
}
|
| 77 |
+
#fileUploadSection {
|
| 78 |
+
margin-top: 30px;
|
| 79 |
+
text-align: center;
|
| 80 |
+
}
|
| 81 |
+
#uploadButton {
|
| 82 |
+
padding: 10px 20px;
|
| 83 |
+
font-size: 16px;
|
| 84 |
+
cursor: pointer;
|
| 85 |
+
border: none;
|
| 86 |
+
border-radius: 5px;
|
| 87 |
+
background-color: #28a745;
|
| 88 |
+
color: #fff;
|
| 89 |
+
transition: background-color 0.3s;
|
| 90 |
+
}
|
| 91 |
+
#uploadButton:hover {
|
| 92 |
+
background-color: #218838;
|
| 93 |
+
}
|
| 94 |
+
#fileInput {
|
| 95 |
+
margin-bottom: 10px;
|
| 96 |
+
}
|
| 97 |
</style>
|
| 98 |
</head>
|
| 99 |
<body>
|
|
|
|
| 106 |
<div id="console"></div>
|
| 107 |
<div id="notification">Fake Call Detected!</div>
|
| 108 |
|
| 109 |
+
<div id="fileUploadSection">
|
| 110 |
+
<h2>Upload Audio for Detection</h2>
|
| 111 |
+
<input type="file" id="fileInput" accept="audio/*">
|
| 112 |
+
<button id="uploadButton">Upload and Detect</button>
|
| 113 |
+
<p id="uploadResult"></p>
|
| 114 |
+
</div>
|
| 115 |
+
|
| 116 |
<script>
|
| 117 |
let isDetecting = false;
|
| 118 |
const startButton = document.getElementById('startButton');
|
|
|
|
| 158 |
}
|
| 159 |
};
|
| 160 |
|
| 161 |
+
// const response = await fetch(`${window.location.origin}/start_detection`, {
|
| 162 |
+
const response = await fetch(`http://localhost:7860/start_detection`, {
|
| 163 |
method: 'POST',
|
| 164 |
});
|
| 165 |
|
|
|
|
| 170 |
const result = await response.json();
|
| 171 |
logMessage(`Status: ${result.status}`, 'info');
|
| 172 |
|
| 173 |
+
// websocket = new WebSocket(`wss://${window.location.host}/ws`);
|
| 174 |
+
websocket = new WebSocket(`ws://localhost:7860/ws`);
|
| 175 |
websocket.onmessage = async function(event) {
|
| 176 |
const data = event.data;
|
| 177 |
if (data === 'global-fake') {
|
|
|
|
| 229 |
|
| 230 |
startButton.addEventListener('click', startDetection);
|
| 231 |
stopButton.addEventListener('click', stopDetection);
|
| 232 |
+
|
| 233 |
+
const fileInput = document.getElementById('fileInput');
|
| 234 |
+
const uploadButton = document.getElementById('uploadButton');
|
| 235 |
+
const uploadResult = document.getElementById('uploadResult');
|
| 236 |
+
|
| 237 |
+
uploadButton.addEventListener('click', async () => {
|
| 238 |
+
const file = fileInput.files[0];
|
| 239 |
+
if (!file) {
|
| 240 |
+
uploadResult.textContent = 'Please select a file first.';
|
| 241 |
+
return;
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
const formData = new FormData();
|
| 245 |
+
formData.append('file', file);
|
| 246 |
+
|
| 247 |
+
try {
|
| 248 |
+
// const response = await fetch(`${window.location.origin}/upload_audio/`, {
|
| 249 |
+
const response = await fetch(`http://localhost:7860/upload_audio/`, {
|
| 250 |
+
method: 'POST',
|
| 251 |
+
body: formData,
|
| 252 |
+
});
|
| 253 |
+
|
| 254 |
+
if (!response.ok) {
|
| 255 |
+
throw new Error('Network response was not ok');
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
const result = await response.json();
|
| 259 |
+
uploadResult.textContent = `Detected audio is: ${result.result}`;
|
| 260 |
+
} catch (error) {
|
| 261 |
+
uploadResult.textContent = `Error: ${error.message}`;
|
| 262 |
+
}
|
| 263 |
+
});
|
| 264 |
</script>
|
| 265 |
</body>
|
| 266 |
</html>
|