Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, WebSocket
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from kokoro import KPipeline
|
| 4 |
+
import asyncio
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
# Initialize models
|
| 9 |
+
llm = pipeline("text-generation", model="HuggingFaceTB/SmolLM2-360M-Instruct")
|
| 10 |
+
tts = KPipeline(lang_code='a', model='shahid202/Kokoro-82M-TTS')
|
| 11 |
+
|
| 12 |
+
def get_llm_response(text):
|
| 13 |
+
# Very short, witty generation
|
| 14 |
+
prompt = f"User: {text}\nBella:"
|
| 15 |
+
return llm(prompt, max_new_tokens=30)[0]['generated_text'].split("Bella:")[-1].strip()
|
| 16 |
+
|
| 17 |
+
@app.websocket("/ws/chat")
|
| 18 |
+
async def websocket_endpoint(websocket: WebSocket):
|
| 19 |
+
await websocket.accept()
|
| 20 |
+
while True:
|
| 21 |
+
user_msg = await websocket.receive_text()
|
| 22 |
+
|
| 23 |
+
# 1. Get response from LLM
|
| 24 |
+
full_text = get_llm_response(user_msg)
|
| 25 |
+
|
| 26 |
+
# 2. Simple split by punctuation to trigger TTS immediately
|
| 27 |
+
sentences = [s.strip() + "." for s in full_text.split('.') if s]
|
| 28 |
+
|
| 29 |
+
for sentence in sentences:
|
| 30 |
+
# 3. Generate audio for the sentence
|
| 31 |
+
generator = tts(sentence, voice="af_heart", speed=1.0)
|
| 32 |
+
for _, _, audio in generator:
|
| 33 |
+
# 4. Send audio bytes over WebSocket
|
| 34 |
+
await websocket.send_bytes(audio.tobytes())
|
| 35 |
+
# Small pause to mimic natural speech flow
|
| 36 |
+
await asyncio.sleep(0.1)
|
| 37 |
+
|