Update app.py
Browse files
app.py
CHANGED
|
@@ -76,9 +76,8 @@ async def chat_with_llm(file: UploadFile = File(...)):
|
|
| 76 |
# Load WAV file
|
| 77 |
fs, recorded_waveform = wav.read("input_chat.wav")
|
| 78 |
os.remove("input_chat.wav")
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
waveform_bytes = recorded_waveform.astype(np.uint8).tobytes()
|
| 82 |
user_message = ggwave.decode(instance, waveform_bytes)
|
| 83 |
|
| 84 |
# Send to LLM
|
|
@@ -89,9 +88,23 @@ async def chat_with_llm(file: UploadFile = File(...)):
|
|
| 89 |
llm_response = chat_completion.choices[0].message.content
|
| 90 |
|
| 91 |
# Convert response to audio
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
buffer = io.BytesIO()
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
buffer.seek(0)
|
| 96 |
|
| 97 |
return Response(content=buffer.getvalue(), media_type="audio/wav", headers={
|
|
|
|
| 76 |
# Load WAV file
|
| 77 |
fs, recorded_waveform = wav.read("input_chat.wav")
|
| 78 |
os.remove("input_chat.wav")
|
| 79 |
+
recorded_waveform = recorded_waveform.astype(np.float32) / 32767.0
|
| 80 |
+
waveform_bytes = recorded_waveform.tobytes()
|
|
|
|
| 81 |
user_message = ggwave.decode(instance, waveform_bytes)
|
| 82 |
|
| 83 |
# Send to LLM
|
|
|
|
| 88 |
llm_response = chat_completion.choices[0].message.content
|
| 89 |
|
| 90 |
# Convert response to audio
|
| 91 |
+
"""Convert text to a WAV audio file using ggwave and return as response."""
|
| 92 |
+
encoded_waveform = ggwave.encode(input_text.text, protocolId=1, volume=100)
|
| 93 |
+
|
| 94 |
+
# Convert byte data into float32 array
|
| 95 |
+
waveform_float32 = np.frombuffer(encoded_waveform, dtype=np.float32)
|
| 96 |
+
|
| 97 |
+
# Normalize float32 data to the range of int16
|
| 98 |
+
waveform_int16 = np.int16(waveform_float32 * 32767)
|
| 99 |
+
|
| 100 |
+
# Save to buffer instead of a file
|
| 101 |
buffer = io.BytesIO()
|
| 102 |
+
with wave.open(buffer, "wb") as wf:
|
| 103 |
+
wf.setnchannels(1) # Mono audio
|
| 104 |
+
wf.setsampwidth(2) # 2 bytes per sample (16-bit PCM)
|
| 105 |
+
wf.setframerate(48000) # Sample rate
|
| 106 |
+
wf.writeframes(waveform_int16.tobytes()) # Write waveform as bytes
|
| 107 |
+
|
| 108 |
buffer.seek(0)
|
| 109 |
|
| 110 |
return Response(content=buffer.getvalue(), media_type="audio/wav", headers={
|