Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, UploadFile, File, Response, Request
|
2 |
+
from fastapi.staticfiles import StaticFiles
|
3 |
+
import ggwave
|
4 |
+
import scipy.io.wavfile as wav
|
5 |
+
import numpy as np
|
6 |
+
import os
|
7 |
+
from pydantic import BaseModel
|
8 |
+
from groq import Groq
|
9 |
+
import io
|
10 |
+
|
11 |
+
app = FastAPI()
|
12 |
+
|
13 |
+
# Serve static files
|
14 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
15 |
+
|
16 |
+
# Initialize ggwave instance
|
17 |
+
instance = ggwave.init()
|
18 |
+
|
19 |
+
# Initialize Groq client
|
20 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
21 |
+
|
22 |
+
class TextInput(BaseModel):
|
23 |
+
text: str
|
24 |
+
|
25 |
+
@app.get("/")
|
26 |
+
async def serve_homepage():
|
27 |
+
"""Serve the chat interface HTML."""
|
28 |
+
with open("static/index.html", "r") as f:
|
29 |
+
return Response(content=f.read(), media_type="text/html")
|
30 |
+
|
31 |
+
@app.post("/stt/")
|
32 |
+
async def speech_to_text(file: UploadFile = File(...)):
|
33 |
+
"""Convert WAV audio file to text using ggwave."""
|
34 |
+
with open("temp.wav", "wb") as audio_file:
|
35 |
+
audio_file.write(await file.read())
|
36 |
+
|
37 |
+
# Load WAV file
|
38 |
+
fs, recorded_waveform = wav.read("temp.wav")
|
39 |
+
os.remove("temp.wav")
|
40 |
+
|
41 |
+
# Convert to bytes and decode
|
42 |
+
waveform_bytes = recorded_waveform.astype(np.uint8).tobytes()
|
43 |
+
decoded_message = ggwave.decode(instance, waveform_bytes)
|
44 |
+
|
45 |
+
return {"text": decoded_message}
|
46 |
+
|
47 |
+
@app.post("/tts/")
|
48 |
+
def text_to_speech(input_text: TextInput):
|
49 |
+
"""Convert text to a WAV audio file using ggwave and return as response."""
|
50 |
+
encoded_waveform = ggwave.encode(instance, input_text.text)
|
51 |
+
buffer = io.BytesIO()
|
52 |
+
wav.write(buffer, 44100, np.frombuffer(encoded_waveform, dtype=np.uint8))
|
53 |
+
buffer.seek(0)
|
54 |
+
return Response(content=buffer.getvalue(), media_type="audio/wav")
|
55 |
+
|
56 |
+
@app.post("/chat/")
|
57 |
+
async def chat_with_llm(file: UploadFile = File(...)):
|
58 |
+
"""Process input WAV, send text to LLM, and return generated response as WAV."""
|
59 |
+
with open("input_chat.wav", "wb") as audio_file:
|
60 |
+
audio_file.write(await file.read())
|
61 |
+
|
62 |
+
# Load WAV file
|
63 |
+
fs, recorded_waveform = wav.read("input_chat.wav")
|
64 |
+
os.remove("input_chat.wav")
|
65 |
+
|
66 |
+
# Convert to bytes and decode
|
67 |
+
waveform_bytes = recorded_waveform.astype(np.uint8).tobytes()
|
68 |
+
user_message = ggwave.decode(instance, waveform_bytes)
|
69 |
+
|
70 |
+
# Send to LLM
|
71 |
+
chat_completion = client.chat.completions.create(
|
72 |
+
messages=[{"role": "user", "content": user_message}],
|
73 |
+
model="llama-3.3-70b-versatile",
|
74 |
+
)
|
75 |
+
llm_response = chat_completion.choices[0].message.content
|
76 |
+
|
77 |
+
# Convert response to audio
|
78 |
+
response_waveform = ggwave.encode(instance, llm_response)
|
79 |
+
buffer = io.BytesIO()
|
80 |
+
wav.write(buffer, 44100, np.frombuffer(response_waveform, dtype=np.uint8))
|
81 |
+
buffer.seek(0)
|
82 |
+
|
83 |
+
return Response(content=buffer.getvalue(), media_type="audio/wav", headers={
|
84 |
+
"X-User-Message": user_message,
|
85 |
+
"X-LLM-Response": llm_response
|
86 |
+
})
|