Update main.py
Browse files
main.py
CHANGED
|
@@ -7,11 +7,8 @@ import asyncio
|
|
| 7 |
import numpy as np
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
-
|
| 11 |
-
# Mount the static folder
|
| 12 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 13 |
|
| 14 |
-
# Initialize models
|
| 15 |
llm = pipeline("text-generation", model="HuggingFaceTB/SmolLM2-360M-Instruct")
|
| 16 |
tts = KPipeline(lang_code='a', model='shahid202/Kokoro-82M-TTS')
|
| 17 |
|
|
@@ -22,8 +19,8 @@ async def get_index():
|
|
| 22 |
@app.websocket("/ws/chat")
|
| 23 |
async def websocket_endpoint(websocket: WebSocket):
|
| 24 |
await websocket.accept()
|
| 25 |
-
|
| 26 |
-
|
| 27 |
user_msg = await websocket.receive_text()
|
| 28 |
# Generate response
|
| 29 |
response = llm(f"User: {user_msg}\nBella:", max_new_tokens=30)[0]['generated_text']
|
|
@@ -31,9 +28,7 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
| 31 |
|
| 32 |
# Stream audio
|
| 33 |
for _, _, audio in tts(text, voice="af_heart", speed=1.0):
|
| 34 |
-
# Ensure float32 format
|
| 35 |
await websocket.send_bytes(audio.astype(np.float32).tobytes())
|
| 36 |
await asyncio.sleep(0.01)
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
break
|
|
|
|
| 7 |
import numpy as np
|
| 8 |
|
| 9 |
app = FastAPI()
|
|
|
|
|
|
|
| 10 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 11 |
|
|
|
|
| 12 |
llm = pipeline("text-generation", model="HuggingFaceTB/SmolLM2-360M-Instruct")
|
| 13 |
tts = KPipeline(lang_code='a', model='shahid202/Kokoro-82M-TTS')
|
| 14 |
|
|
|
|
| 19 |
@app.websocket("/ws/chat")
|
| 20 |
async def websocket_endpoint(websocket: WebSocket):
|
| 21 |
await websocket.accept()
|
| 22 |
+
try:
|
| 23 |
+
while True:
|
| 24 |
user_msg = await websocket.receive_text()
|
| 25 |
# Generate response
|
| 26 |
response = llm(f"User: {user_msg}\nBella:", max_new_tokens=30)[0]['generated_text']
|
|
|
|
| 28 |
|
| 29 |
# Stream audio
|
| 30 |
for _, _, audio in tts(text, voice="af_heart", speed=1.0):
|
|
|
|
| 31 |
await websocket.send_bytes(audio.astype(np.float32).tobytes())
|
| 32 |
await asyncio.sleep(0.01)
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"Connection closed: {e}")
|
|
|