-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
82 lines (64 loc) · 2.6 KB
/
main.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
from flask import Flask, Response, request, jsonify, stream_with_context
from openai import OpenAI
from dotenv import load_dotenv
import os
import json
load_dotenv(os.path.join(os.path.dirname(__file__), ".env"))
app = Flask(__name__)
openaiClient = OpenAI(
api_key=os.getenv("OPENAI_API_KEY")
)
def api_error(message, status_code):
response = jsonify({"error": message})
response.status_code = status_code
return response
@app.route('/chat', methods=['POST'])
def chat():
data = request.get_json()
content = data.get('content')
if not content:
return api_error("\"content\" field is required", 400)
response = openaiClient.chat.completions.create(
messages=[
{
"role": "user",
"content": content,
}
],
model="gpt-3.5-turbo",
stream=True
)
def generate():
retrieved = ''
sentences = ''
for chunk in response.response.iter_bytes(1024):
if chunk:
retrieved += chunk.decode('utf-8')
if '\n\n' in retrieved:
*completed, retrieved = retrieved.split('\n\n')
for json_object in completed:
json_object = json_object.replace('data: ', '').strip()
if json_object == '[DONE]':
continue
try:
if json_object:
json_data = json.loads(json_object)
text = json_data.get('choices', [{}])[0].get(
'delta', {}).get('content', '')
sentences += text
if sentences and sentences.endswith('.' or '!' or '?'):
audio_response = openaiClient.audio.speech.create(
model="tts-1",
voice="nova",
input=sentences,
response_format="opus"
)
sentences = ''
for audio_chunk in audio_response.iter_bytes(1024):
yield audio_chunk
except json.JSONDecodeError as e:
print(e)
continue
return Response(stream_with_context(generate()), content_type='audio/opus')
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080)