Update main.py
Browse files
main.py
CHANGED
|
@@ -4,34 +4,19 @@ 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 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 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 |
-
|
|
|
|
| 4 |
import asyncio
|
| 5 |
|
| 6 |
app = FastAPI()
|
|
|
|
|
|
|
| 7 |
llm = pipeline("text-generation", model="HuggingFaceTB/SmolLM2-360M-Instruct")
|
| 8 |
tts = KPipeline(lang_code='a', model='shahid202/Kokoro-82M-TTS')
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
@app.websocket("/ws/chat")
|
| 11 |
async def websocket_endpoint(websocket: WebSocket):
|
| 12 |
await websocket.accept()
|
| 13 |
while True:
|
| 14 |
user_msg = await websocket.receive_text()
|
| 15 |
+
# Get LLM text
|
| 16 |
+
response_text = llm(f"User: {user_msg}\nBella:", max_new_tokens=30)[0]['generated_text']
|
| 17 |
+
text = response_text.split("Bella:")[-1].strip()
|
| 18 |
|
| 19 |
+
# Stream audio chunk by chunk
|
| 20 |
+
for _, _, audio in tts(text, voice="af_heart", speed=1.0):
|
| 21 |
+
await websocket.send_bytes(audio.tobytes())
|
| 22 |
+
await asyncio.sleep(0.05) # Keeps flow smooth
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|