-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
245 lines (190 loc) · 7.76 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
from fastapi import FastAPI, UploadFile, File, Response
from maximai.context import get_full_patient_context
from maximai.langchain import create_context_aware_chatbot
from maximai.schemas import Prompt
from maximai.text_processing import transform_numbers_to_text
from google.cloud.speech_v2 import SpeechClient
from google.cloud.speech_v2.types import cloud_speech
# from fastapi import FastAPI, Response
from google.cloud import texttospeech
app = FastAPI(title="MaximAI Chat App")
# et_debug(True)
chatbot = create_context_aware_chatbot()
# Define a route to handle API calls
@app.post("/chat")
async def root(prompt: Prompt):
context = get_full_patient_context(prompt.user_id)
print(context)
output = chatbot.invoke(
{"input": prompt.text, "context": context},
config={"configurable": {"user_id": prompt.user_id}},
)
# output = output["content"]
return {
"input_message": prompt.text,
"output_message": transform_numbers_to_text(output.content), # TODO transform "8" into eight
"user_id": prompt.user_id,
}
@app.post("/audio_text")
async def audio_text(file: UploadFile = File(...), user_id: str = "Emmy"):
client = SpeechClient()
model="latest_long"
project_id="qwiklabs-gcp-02-c86bd22dbd03"
content = await file.read()
config = cloud_speech.RecognitionConfig(
auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),
language_codes=["en-US"],
model=model,
)
request = cloud_speech.RecognizeRequest(
recognizer=f"projects/{project_id}/locations/global/recognizers/_",
config=config,
content=content,
)
# Transcribes the audio into text
response = client.recognize(request=request)
for result in response.results:
print(f"Transcript: {result.alternatives[0].transcript}")
text = result.alternatives[0].transcript
print(f"{text = }")
break
context = get_full_patient_context(user_id)
# print(context)
output = chatbot.invoke(
{"input": text, "context": context},
config={"configurable": {"user_id": user_id}},
)
# TODO: pass transcript to langchain
# chain_output = root(result.alternatives[0].transcript)
# print(f"chain_output: {chain_output}")
# TODO: pass output of langchain
# chain_output["output_message"]
return {
# "input_message": prompt.text,
"output_message": transform_numbers_to_text(output.content), # TODO transform "8" into eight
"user_id": user_id,
}
@app.post("/audio")
async def audio(file: UploadFile = File(...), user_id: str = "Emmy"):
client = SpeechClient()
model="latest_long"
project_id="qwiklabs-gcp-02-c86bd22dbd03"
content = await file.read()
config = cloud_speech.RecognitionConfig(
auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),
language_codes=["en-US"],
model=model,
)
request = cloud_speech.RecognizeRequest(
recognizer=f"projects/{project_id}/locations/global/recognizers/_",
config=config,
content=content,
)
# Transcribes the audio into text
response = client.recognize(request=request)
for result in response.results:
print(f"Transcript: {result.alternatives[0].transcript}")
text = result.alternatives[0].transcript
print(f"{text = }")
break
context = get_full_patient_context(user_id)
# print(context)
output = chatbot.invoke(
{"input": text, "context": context},
config={"configurable": {"user_id": user_id}},
)
# TODO: pass transcript to langchain
# chain_output = root(result.alternatives[0].transcript)
# print(f"chain_output: {chain_output}")
# TODO: pass output of langchain
# chain_output["output_message"]
##################
# text to speech #
##################
text = output.content
# Instantiates a client
client = texttospeech.TextToSpeechClient()
# Set the text input to be synthesized
synthesis_input = texttospeech.SynthesisInput(text=text)
# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.VoiceSelectionParams(
language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL
)
# Select the type of audio file you want returned
audio_config = texttospeech.AudioConfig(audio_encoding=texttospeech.AudioEncoding.MP3)
# Perform the text-to-speech request on the text input with the selected
# voice parameters and audio file type
response = client.synthesize_speech(
input=synthesis_input, voice=voice, audio_config=audio_config
)
# Set the content type of the response to audio/mpeg
headers = {
"Content-Disposition": "attachment; filename=output.mp3",
"Content-Type": "audio/mpeg",
}
return Response(content=response.audio_content, media_type="audio/mpeg", headers=headers)
@app.post("/text_audio")
async def text_audio(prompt: Prompt):
context = get_full_patient_context(prompt.user_id)
print(context)
output = chatbot.invoke(
{"input": prompt.text, "context": context},
config={"configurable": {"user_id": prompt.user_id}},
)
text = transform_numbers_to_text(output.content)
# return {
# "input_message": prompt.text,
# "output_message": transform_numbers_to_text(output.content), # TODO transform "8" into eight
# "user_id": prompt.user_id,
# }
##################
# text to speech #
##################
# Instantiates a client
client = texttospeech.TextToSpeechClient()
# Set the text input to be synthesized
synthesis_input = texttospeech.SynthesisInput(text=text)
# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.VoiceSelectionParams(
language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL
)
# Select the type of audio file you want returned
audio_config = texttospeech.AudioConfig(audio_encoding=texttospeech.AudioEncoding.MP3)
# Perform the text-to-speech request on the text input with the selected
# voice parameters and audio file type
response = client.synthesize_speech(
input=synthesis_input, voice=voice, audio_config=audio_config
)
# Set the content type of the response to audio/mpeg
headers = {
"Content-Disposition": "attachment; filename=output.mp3",
"Content-Type": "audio/mpeg",
}
return Response(content=response.audio_content, media_type="audio/mpeg", headers=headers)
@app.get("/synthesize_audio")
async def synthesize_audio():
# Instantiates a client
client = texttospeech.TextToSpeechClient()
# Set the text input to be synthesized
synthesis_input = texttospeech.SynthesisInput(text="Hello, World!")
# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.VoiceSelectionParams(
language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL
)
# Select the type of audio file you want returned
audio_config = texttospeech.AudioConfig(audio_encoding=texttospeech.AudioEncoding.MP3)
# Perform the text-to-speech request on the text input with the selected
# voice parameters and audio file type
response = client.synthesize_speech(
input=synthesis_input, voice=voice, audio_config=audio_config
)
# Set the content type of the response to audio/mpeg
headers = {
"Content-Disposition": "attachment; filename=output.mp3",
"Content-Type": "audio/mpeg",
}
# Return the audio content as a response
return Response(content=response.audio_content, media_type="audio/mpeg", headers=headers)