-
Notifications
You must be signed in to change notification settings - Fork 13
/
server.py
53 lines (45 loc) · 1.93 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Copyright (c) 2023 Braedon Hendy
# This software is released under the GNU General Public License v3.0
from http.server import BaseHTTPRequestHandler, HTTPServer
import whisper
import cgi
import json
import os
import tempfile
# Initialize Whisper model
model = whisper.load_model("medium")
class RequestHandler(BaseHTTPRequestHandler):
def do_POST(self):
if self.path == '/whisperaudio':
ctype, pdict = cgi.parse_header(self.headers.get('content-type'))
if ctype == 'multipart/form-data':
pdict['boundary'] = bytes(pdict['boundary'], "utf-8")
fields = cgi.parse_multipart(self.rfile, pdict)
audio_data = fields.get('audio')[0]
# Save the audio file temporarily
with tempfile.NamedTemporaryFile(delete=False) as temp_audio_file:
temp_audio_file.write(audio_data)
temp_file_path = temp_audio_file.name
try:
# Process the file with Whisper
result = model.transcribe(temp_file_path)
# Send response
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
response_data = json.dumps({"text": result["text"]})
self.wfile.write(response_data.encode())
finally:
# Clean up the temporary file
os.remove(temp_file_path)
else:
self.send_error(400, "Invalid content type")
else:
self.send_error(404, "File not found")
def run(server_class=HTTPServer, handler_class=RequestHandler, port=8000):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print(f'Server running at http://localhost:{port}/')
httpd.serve_forever()
if __name__ == '__main__':
run()