-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.py
125 lines (91 loc) · 4.17 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from flask import Flask, request, Response, stream_with_context, send_from_directory
import requests
import argparse
import os
import whisper
whisper_model = whisper.load_model("small")
app = Flask(__name__, static_url_path='', static_folder='static')
proxy_paths = {}
def generate_stream(proxy_response):
for chunk in proxy_response.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
yield chunk
@app.route('/')
def index():
return send_from_directory(app.static_folder, 'index.html')
@app.route('/api/transcribe', methods=['POST'])
def transcribe_audio():
if 'audio' not in request.files:
return "No audio file found", 400
audio_file = request.files['audio']
audio_file.save("audio.wav")
# load audio and pad/trim it to fit 30 seconds
audio = whisper.load_audio("audio.wav")
audio = whisper.pad_or_trim(audio)
# make log-Mel spectrogram and move to the same device as the model
mel = whisper.log_mel_spectrogram(audio).to(whisper_model.device)
# detect the spoken language
_, probs = whisper_model.detect_language(mel)
print(f"Detected language: {max(probs, key=probs.get)}")
# decode the audio
options = whisper.DecodingOptions()
result = whisper.decode(whisper_model, mel, options)
# print the recognized text
print(result.text)
return result.text, 200
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'])
def proxy_or_static(path):
head_path = path.split('/')[0]
# Print the request path
print(f"Request path: {path}")
print(f"Head path: {head_path}")
# Print proxy paths
print(f"Proxy paths: {proxy_paths}")
if head_path in proxy_paths:
tail_path = '/'.join(path.split('/')[1:])
target_base_url = proxy_paths[head_path]
print(f"Target base URL: {target_base_url}")
print(f"Tail path: {tail_path}")
proxy_url = target_base_url + '/' + tail_path
print(f"Proxy URL: {proxy_url}")
headers = {key: value for (key, value) in request.headers if key != 'Host'}
# If the OPENAI_API_KEY environment variable is set, add it to the request headers
if 'OPENAI_API_KEY' in os.environ and head_path == 'oai':
headers['Authorization'] = f"Bearer {os.environ['OPENAI_API_KEY']}"
resp = requests.request(
method=request.method,
url=proxy_url,
headers=headers,
data=request.get_data(),
cookies=request.cookies,
allow_redirects=False,
stream=True
)
if 'text/event-stream' in resp.headers.get('Content-Type', ''):
response = Response(stream_with_context(generate_stream(resp)), content_type='text/event-stream')
else:
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
response = Response(resp.content, resp.status_code, headers)
return response
else:
# Static file serving logic
if os.path.isfile(os.path.join(app.static_folder, path)):
return send_from_directory(app.static_folder, path)
else:
return "File not found", 404
def run():
parser = argparse.ArgumentParser(description="Flask-based HTTPS server with proxy functionality.")
parser.add_argument('--host', default='localhost', help='Host name (default: localhost)')
parser.add_argument('--port', type=int, default=443, help='Port number (default: 443)')
parser.add_argument('--proxy', action='append', help='Proxy paths in the format path:host:port')
# debug mode
parser.add_argument('--debug', action='store_true', help='Run in debug mode')
args = parser.parse_args()
global proxy_paths
proxy_paths = {p.split(':')[0]: ':'.join(p.split(':')[1:]) for p in args.proxy or []}
print(f"Proxy paths: {proxy_paths}")
context = ('cert.pem', 'key.pem')
app.run(host=args.host, port=args.port, ssl_context=context, debug=args.debug)
if __name__ == '__main__':
run()