Update app.py
Browse files
app.py
CHANGED
|
@@ -17,9 +17,6 @@ app = FastAPI()
|
|
| 17 |
# Serve static files
|
| 18 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 19 |
|
| 20 |
-
# Initialize ggwave instance
|
| 21 |
-
instance = ggwave.init()
|
| 22 |
-
|
| 23 |
# Initialize Groq client
|
| 24 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 25 |
|
|
@@ -29,7 +26,7 @@ class TextInput(BaseModel):
|
|
| 29 |
@app.get("/")
|
| 30 |
async def serve_homepage():
|
| 31 |
"""Serve the chat interface HTML."""
|
| 32 |
-
return FileResponse("static/
|
| 33 |
|
| 34 |
@app.get("/conv/")
|
| 35 |
async def serve_convpage():
|
|
@@ -77,7 +74,9 @@ def text_to_speech(input_text: TextInput):
|
|
| 77 |
@app.post("/chat/")
|
| 78 |
async def chat_with_llm(file: UploadFile = File(...)):
|
| 79 |
"""Process input WAV, send text to LLM, and return generated response as WAV."""
|
| 80 |
-
|
|
|
|
|
|
|
| 81 |
# Read the file content into memory without saving to disk
|
| 82 |
file_content = await file.read()
|
| 83 |
|
|
@@ -128,7 +127,7 @@ async def chat_with_llm(file: UploadFile = File(...)):
|
|
| 128 |
wf.writeframes(waveform_int16.tobytes()) # Write waveform as bytes
|
| 129 |
|
| 130 |
buffer.seek(0)
|
| 131 |
-
|
| 132 |
return Response(
|
| 133 |
content=buffer.getvalue(),
|
| 134 |
media_type="audio/wav",
|
|
@@ -140,6 +139,7 @@ async def chat_with_llm(file: UploadFile = File(...)):
|
|
| 140 |
|
| 141 |
except Exception as e:
|
| 142 |
print(f"Error processing audio: {str(e)}")
|
|
|
|
| 143 |
return Response(
|
| 144 |
content=f"Error processing audio: {str(e)}",
|
| 145 |
media_type="text/plain",
|
|
@@ -151,7 +151,10 @@ async def continuous_chat(
|
|
| 151 |
chat_history: Optional[str] = Form(None)
|
| 152 |
):
|
| 153 |
"""Process input WAV with chat history, send text to LLM, and return response as WAV."""
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
# Parse chat history if provided
|
| 157 |
messages = [{"role": "system", "content": "you are a helpful assistant. answer always in one sentence"}]
|
|
@@ -212,6 +215,7 @@ async def continuous_chat(
|
|
| 212 |
wf.writeframes(waveform_int16.tobytes())
|
| 213 |
|
| 214 |
buffer.seek(0)
|
|
|
|
| 215 |
|
| 216 |
return Response(
|
| 217 |
content=buffer.getvalue(),
|
|
@@ -224,6 +228,7 @@ async def continuous_chat(
|
|
| 224 |
|
| 225 |
except Exception as e:
|
| 226 |
print(f"Error processing audio: {str(e)}")
|
|
|
|
| 227 |
return Response(
|
| 228 |
content=f"Error processing audio: {str(e)}",
|
| 229 |
media_type="text/plain",
|
|
|
|
| 17 |
# Serve static files
|
| 18 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
# Initialize Groq client
|
| 21 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 22 |
|
|
|
|
| 26 |
@app.get("/")
|
| 27 |
async def serve_homepage():
|
| 28 |
"""Serve the chat interface HTML."""
|
| 29 |
+
return FileResponse("static/conv.html")
|
| 30 |
|
| 31 |
@app.get("/conv/")
|
| 32 |
async def serve_convpage():
|
|
|
|
| 74 |
@app.post("/chat/")
|
| 75 |
async def chat_with_llm(file: UploadFile = File(...)):
|
| 76 |
"""Process input WAV, send text to LLM, and return generated response as WAV."""
|
| 77 |
+
# Initialize ggwave instance
|
| 78 |
+
instance = ggwave.init()
|
| 79 |
+
|
| 80 |
# Read the file content into memory without saving to disk
|
| 81 |
file_content = await file.read()
|
| 82 |
|
|
|
|
| 127 |
wf.writeframes(waveform_int16.tobytes()) # Write waveform as bytes
|
| 128 |
|
| 129 |
buffer.seek(0)
|
| 130 |
+
ggwave.free(instance)
|
| 131 |
return Response(
|
| 132 |
content=buffer.getvalue(),
|
| 133 |
media_type="audio/wav",
|
|
|
|
| 139 |
|
| 140 |
except Exception as e:
|
| 141 |
print(f"Error processing audio: {str(e)}")
|
| 142 |
+
ggwave.free(instance)
|
| 143 |
return Response(
|
| 144 |
content=f"Error processing audio: {str(e)}",
|
| 145 |
media_type="text/plain",
|
|
|
|
| 151 |
chat_history: Optional[str] = Form(None)
|
| 152 |
):
|
| 153 |
"""Process input WAV with chat history, send text to LLM, and return response as WAV."""
|
| 154 |
+
# Initialize ggwave instance
|
| 155 |
+
instance = ggwave.init()
|
| 156 |
+
|
| 157 |
+
|
| 158 |
|
| 159 |
# Parse chat history if provided
|
| 160 |
messages = [{"role": "system", "content": "you are a helpful assistant. answer always in one sentence"}]
|
|
|
|
| 215 |
wf.writeframes(waveform_int16.tobytes())
|
| 216 |
|
| 217 |
buffer.seek(0)
|
| 218 |
+
ggwave.free(instance)
|
| 219 |
|
| 220 |
return Response(
|
| 221 |
content=buffer.getvalue(),
|
|
|
|
| 228 |
|
| 229 |
except Exception as e:
|
| 230 |
print(f"Error processing audio: {str(e)}")
|
| 231 |
+
ggwave.free(instance)
|
| 232 |
return Response(
|
| 233 |
content=f"Error processing audio: {str(e)}",
|
| 234 |
media_type="text/plain",
|