Spaces:
Sleeping
Sleeping
- main.py +8 -42
- templates/index.html +1 -1
main.py
CHANGED
@@ -27,10 +27,6 @@ async def get(request: Request):
|
|
27 |
def health_check():
|
28 |
return {"status": "ok"}
|
29 |
|
30 |
-
# @app.get("/")
|
31 |
-
# def read_root():
|
32 |
-
# return {"status": "ok"}
|
33 |
-
|
34 |
app.add_middleware(
|
35 |
CORSMiddleware,
|
36 |
allow_origins=["*"],
|
@@ -39,9 +35,6 @@ app.add_middleware(
|
|
39 |
allow_headers=["*"],
|
40 |
)
|
41 |
|
42 |
-
duration = 2
|
43 |
-
sample_rate = 16000
|
44 |
-
|
45 |
is_detecting = False
|
46 |
detection_thread = None
|
47 |
|
@@ -82,57 +75,29 @@ def extract_features(audio):
|
|
82 |
combined_features = np.hstack([mfccs, chroma, contrast, centroid])
|
83 |
return combined_features
|
84 |
|
85 |
-
async def
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
audio_data = indata[:, 0]
|
90 |
-
print(f"Audio data: {audio_data[:10]}... (length: {len(audio_data)})")
|
91 |
-
logger.info(f"Audio data: {audio_data[:10]}... (length: {len(audio_data)})")
|
92 |
-
|
93 |
-
features = extract_features(audio_data)
|
94 |
features = features.reshape(1, -1)
|
95 |
prediction = model.predict(features)
|
96 |
is_fake = prediction[0]
|
97 |
|
98 |
-
print(f"Prediction: {is_fake}")
|
99 |
-
logger.info(f"Prediction: {is_fake}")
|
100 |
-
|
101 |
result = 'fake' if is_fake else 'real'
|
102 |
|
103 |
-
print(f"Detected {result} audio")
|
104 |
-
logger.info(f"Detected {result} audio")
|
105 |
-
|
106 |
await manager.send_message(result)
|
107 |
|
108 |
-
def detect_fake_audio():
|
109 |
-
global is_detecting
|
110 |
-
try:
|
111 |
-
with sd.InputStream(callback=lambda indata, frames, time, status: asyncio.run(audio_callback(indata, frames, time, status)), channels=1, samplerate=sample_rate, blocksize=int(sample_rate * duration)):
|
112 |
-
print("Listening...")
|
113 |
-
logger.info("Listening...")
|
114 |
-
while is_detecting:
|
115 |
-
sd.sleep(duration * 1000)
|
116 |
-
except Exception as e:
|
117 |
-
print(f"Exception: {str(e)}")
|
118 |
-
logger.info(f"Exception: {str(e)}")
|
119 |
-
|
120 |
@app.post("/start_detection")
|
121 |
async def start_detection():
|
122 |
-
global is_detecting
|
123 |
|
124 |
if not is_detecting:
|
125 |
is_detecting = True
|
126 |
-
detection_thread = threading.Thread(target=detect_fake_audio)
|
127 |
-
detection_thread.start()
|
128 |
return JSONResponse(content={'status': 'detection_started'})
|
129 |
|
130 |
@app.post("/stop_detection")
|
131 |
async def stop_detection():
|
132 |
-
global is_detecting
|
133 |
is_detecting = False
|
134 |
-
if detection_thread:
|
135 |
-
detection_thread.join()
|
136 |
return JSONResponse(content={'status': 'detection_stopped'})
|
137 |
|
138 |
@app.websocket("/ws")
|
@@ -140,9 +105,10 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
140 |
await manager.connect(websocket)
|
141 |
try:
|
142 |
while True:
|
143 |
-
await websocket.
|
|
|
144 |
except WebSocketDisconnect:
|
145 |
manager.disconnect(websocket)
|
146 |
|
147 |
if __name__ == '__main__':
|
148 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
27 |
def health_check():
|
28 |
return {"status": "ok"}
|
29 |
|
|
|
|
|
|
|
|
|
30 |
app.add_middleware(
|
31 |
CORSMiddleware,
|
32 |
allow_origins=["*"],
|
|
|
35 |
allow_headers=["*"],
|
36 |
)
|
37 |
|
|
|
|
|
|
|
38 |
is_detecting = False
|
39 |
detection_thread = None
|
40 |
|
|
|
75 |
combined_features = np.hstack([mfccs, chroma, contrast, centroid])
|
76 |
return combined_features
|
77 |
|
78 |
+
async def process_audio_data(audio_data):
|
79 |
+
audio_np = np.frombuffer(audio_data, dtype=np.float32)
|
80 |
+
features = extract_features(audio_np)
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
features = features.reshape(1, -1)
|
82 |
prediction = model.predict(features)
|
83 |
is_fake = prediction[0]
|
84 |
|
|
|
|
|
|
|
85 |
result = 'fake' if is_fake else 'real'
|
86 |
|
|
|
|
|
|
|
87 |
await manager.send_message(result)
|
88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
@app.post("/start_detection")
|
90 |
async def start_detection():
|
91 |
+
global is_detecting
|
92 |
|
93 |
if not is_detecting:
|
94 |
is_detecting = True
|
|
|
|
|
95 |
return JSONResponse(content={'status': 'detection_started'})
|
96 |
|
97 |
@app.post("/stop_detection")
|
98 |
async def stop_detection():
|
99 |
+
global is_detecting
|
100 |
is_detecting = False
|
|
|
|
|
101 |
return JSONResponse(content={'status': 'detection_stopped'})
|
102 |
|
103 |
@app.websocket("/ws")
|
|
|
105 |
await manager.connect(websocket)
|
106 |
try:
|
107 |
while True:
|
108 |
+
data = await websocket.receive_bytes()
|
109 |
+
await process_audio_data(data)
|
110 |
except WebSocketDisconnect:
|
111 |
manager.disconnect(websocket)
|
112 |
|
113 |
if __name__ == '__main__':
|
114 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
templates/index.html
CHANGED
@@ -81,7 +81,7 @@
|
|
81 |
const result = await response.json();
|
82 |
logMessage(`Detection started: ${result.status}`, 'info');
|
83 |
|
84 |
-
websocket = new WebSocket(`
|
85 |
websocket.onmessage = function(event) {
|
86 |
const data = event.data;
|
87 |
logMessage(`Detected ${data} audio`, data);
|
|
|
81 |
const result = await response.json();
|
82 |
logMessage(`Detection started: ${result.status}`, 'info');
|
83 |
|
84 |
+
websocket = new WebSocket(`ws://${window.location.host}/ws`);
|
85 |
websocket.onmessage = function(event) {
|
86 |
const data = event.data;
|
87 |
logMessage(`Detected ${data} audio`, data);
|