kowalsky commited on
Commit
830d345
·
1 Parent(s): 8a7a578
Files changed (4) hide show
  1. Dockerfile +0 -1
  2. main.py +29 -1
  3. static/i.jpg +0 -0
  4. templates/index.html +63 -4
Dockerfile CHANGED
@@ -26,5 +26,4 @@ COPY . .
26
  # Expose the port that the FastAPI app runs on
27
  EXPOSE 7860
28
 
29
- # Run the FastAPI application with Uvicorn
30
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
 
26
  # Expose the port that the FastAPI app runs on
27
  EXPOSE 7860
28
 
 
29
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
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
@@ -129,6 +129,34 @@ async def stop_detection():
129
  is_detecting = False
130
  return JSONResponse(content={'status': 'detection_stopped'})
131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  @app.websocket("/ws")
133
  async def websocket_endpoint(websocket: WebSocket):
134
  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
 
129
  is_detecting = False
130
  return JSONResponse(content={'status': 'detection_stopped'})
131
 
132
+ @app.post("/upload_audio/")
133
+ async def upload_audio(file: UploadFile = File(...)):
134
+ try:
135
+
136
+ audio_data = await file.read()
137
+
138
+ audio_segment = AudioSegment.from_file(io.BytesIO(audio_data), format=file.filename.split('.')[-1])
139
+ wav_io = io.BytesIO()
140
+ audio_segment.export(wav_io, format="wav")
141
+ wav_io.seek(0)
142
+ audio, sr = sf.read(wav_io, dtype='float32')
143
+
144
+ if audio.ndim > 1:
145
+ audio = np.mean(audio, axis=1)
146
+
147
+ features = extract_features(audio)
148
+ features = features.reshape(1, -1)
149
+
150
+ prediction = model.predict(features)
151
+ is_fake = prediction[0]
152
+ result = 'fake' if is_fake else 'real'
153
+
154
+ return JSONResponse(content={'status': 'success', 'result': result})
155
+
156
+ except Exception as e:
157
+ logger.error(f"Failed to process audio file: {e}")
158
+ return JSONResponse(content={'status': 'error', 'message': str(e)}, status_code=500)
159
+
160
  @app.websocket("/ws")
161
  async def websocket_endpoint(websocket: WebSocket):
162
  await manager.connect(websocket)
static/i.jpg CHANGED
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
- // const response = await fetch(`http://localhost:7860/start_detection`, {
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
- // websocket = new WebSocket(`ws://localhost:7860/ws`);
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>