-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.py
More file actions
126 lines (92 loc) · 3.74 KB
/
server.py
File metadata and controls
126 lines (92 loc) · 3.74 KB
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
126
from flask import Flask, send_file
from dotenv import load_dotenv
from openai import OpenAI
import os
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
import cohere
import io
load_dotenv()
COHERE_API_KEY = os.environ['COHERE_API_KEY']
OPENAI_API_KEY = os.environ['OPENAI_API_KEY']
COHERE_MODEL = "command-r-plus-08-2024"
OPENAI_TTS_VOICE = "nova"
oa_langchain = ChatOpenAI(model="gpt-4o-mini")
oa = OpenAI(api_key=OPENAI_API_KEY)
app = Flask(__name__)
@app.route("/")
def hello():
return "<h1>The endpoint is working</h1>"
@app.route("/score/<current_score>")
def score(current_score):
messages = [
SystemMessage("Respond to the following instructions exactly as they are stated."),
HumanMessage(f"Provide me with a short sentence telling someone they currently have {current_score} points, then say something motivational. If the current score is below 60, that's considered a low score, and say that the user can do better. Otherwise, say that they're doing a great job."),
]
response = oa_langchain.invoke(messages)
audio_response = oa.audio.speech.create(
model="tts-1",
voice=OPENAI_TTS_VOICE,
input=response.content
)
audio_buffer = io.BytesIO()
for chunk in audio_response.iter_bytes():
audio_buffer.write(chunk)
audio_buffer.seek(0)
return send_file(audio_buffer, mimetype="audio/mpeg", as_attachment=True, download_name="output.mp3")
@app.route("/goodjob")
def goodjob():
messages = [
SystemMessage("Respond to the following instructions exactly as they are stated."),
HumanMessage("Provide me with a short, informal, and positive sentence telling someone they are doing a good job."),
]
response = oa_langchain.invoke(messages)
audio_response = oa.audio.speech.create(
model="tts-1",
voice=OPENAI_TTS_VOICE,
input=response.content
)
audio_buffer = io.BytesIO()
for chunk in audio_response.iter_bytes():
audio_buffer.write(chunk)
audio_buffer.seek(0)
return send_file(audio_buffer, mimetype="audio/mpeg", as_attachment=True, download_name="output.mp3")
@app.route("/badjob")
def better_next_time():
messages = [
SystemMessage("Respond to the following instructions exactly as they are stated."),
HumanMessage("Write a short, informal sentence telling the user that what they're doing isn't quite right, and to try again."),
]
response = oa_langchain.invoke(messages)
audio_response = oa.audio.speech.create(
model="tts-1",
voice=OPENAI_TTS_VOICE,
input=response.content
)
# Create a BytesIO buffer to hold the audio data
audio_buffer = io.BytesIO()
# Stream the response content into the buffer
for chunk in audio_response.iter_bytes():
audio_buffer.write(chunk)
# Seek to the beginning of the buffer
audio_buffer.seek(0)
# Return the buffer as an audio file response
return send_file(audio_buffer, mimetype="audio/mpeg", as_attachment=True, download_name="output.mp3")
@app.route("/audio")
def audio_gen():
response = oa.audio.speech.create(
model="tts-1",
voice=OPENAI_TTS_VOICE,
input="Hello world!"
)
# Create a BytesIO buffer to hold the audio data
audio_buffer = io.BytesIO()
# Stream the response content into the buffer
for chunk in response.iter_bytes():
audio_buffer.write(chunk)
# Seek to the beginning of the buffer
audio_buffer.seek(0)
# Return the buffer as an audio file response
return send_file(audio_buffer, mimetype="audio/mpeg", as_attachment=True, download_name="output.mp3")
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)