fastrtc icon indicating copy to clipboard operation
fastrtc copied to clipboard

Error processing frame: %s generator already executing

Open Katehuuh opened this issue 2 days ago • 2 comments

import os
from fastrtc import (
    Stream, ReplyOnPause, get_stt_model, get_tts_model, KokoroTTSOptions
)
import numpy as np
import requests
import json

# Initialize local STT and TTS models
stt_model = get_stt_model(model="moonshine/base")
tts_model = get_tts_model(model="kokoro")

# Configure voice options for Kokoro TTS
voice_options = KokoroTTSOptions(
    voice="af_heart",  # Other options: bf_emma, am_adam, etc.
    speed=1.0,
    lang="en-us"
)

# Function to send text to a local LLM server (e.g., Ollama, LM Studio, etc.)
def query_local_llm(prompt, history=None):
    if history is None:
        history = []
    
    # using a local API oobabooga/text-generation-webui with OpenAI format
    url = "http://localhost:5000/v1/chat/completions"
    
    messages = history + [{"role": "user", "content": prompt}]
    data = {
        "model": "local_model_name",
        "messages": messages,
        "temperature": 0.7
    }
    
    response = requests.post(url, json=data)
    return response.json()["choices"][0]["message"]["content"]

# Handler function for the audio stream
def voice_assistant(audio: tuple[int, np.ndarray], chat_history=None):
    if chat_history is None:
        chat_history = []
    
    # 1. Convert speech to text using local Moonshine model
    sample_rate, audio_array = audio
    transcription = stt_model.stt(audio)
    print(f"User said: {transcription}")
    
    # 2. Send to local LLM
    llm_response = query_local_llm(transcription, chat_history)
    print(f"Assistant response: {llm_response}")
    
    # 3. Convert response to speech using local Kokoro TTS
    for audio_chunk in tts_model.stream_tts_sync(llm_response, options=voice_options):
        yield audio_chunk

# Create and launch the stream
stream = Stream(
    handler=ReplyOnPause(voice_assistant),
    modality="audio",
    mode="send-receive",
)

# Launch the UI
stream.ui.launch()

All local only, quick connect. After first recording message & answer, then it shows this log error and stops recording indefinitely:

INFO:     Warming up STT model.
INFO:     STT model warmed up.
INFO:     Warming up VAD model.
INFO:     VAD model warmed up.
* Running on local URL:  http://127.0.0.1:7860

To create a public link, set `share=True` in `launch()`.
User said: Hello.
Assistant response: Hello! How can I assist you today?
User said: Tell me a story.
Timeout in frame processing cycle after 60 seconds - resetting
Traceback (most recent call last):
  File C:\venv\lib\site-packages\fastrtc\reply_on_pause.py", line 217, in emit
    output = next(self.generator)  # type: ignore
ValueError: generator already executing
Traceback (most recent call last):
  File C:\venv\lib\site-packages\fastrtc\utils.py", line 377, in sync_wrapper
    return func(*args, **kwargs)
  File C:\venv\lib\site-packages\fastrtc\reply_on_pause.py", line 236, in emit
    raise e
  File C:\venv\lib\site-packages\fastrtc\reply_on_pause.py", line 217, in emit
    output = next(self.generator)  # type: ignore
ValueError: generator already executing
traceback %s Traceback (most recent call last):
  File C:\venv\lib\site-packages\fastrtc\utils.py", line 377, in sync_wrapper
    return func(*args, **kwargs)
  File C:\venv\lib\site-packages\fastrtc\reply_on_pause.py", line 236, in emit
    raise e
  File C:\venv\lib\site-packages\fastrtc\reply_on_pause.py", line 217, in emit
    output = next(self.generator)  # type: ignore
ValueError: generator already executing

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File C:\venv\lib\site-packages\fastrtc\utils.py", line 141, in player_worker_decode
    await asyncio.wait_for(next_frame(), timeout=60)
  File "C:\Python\Python310\lib\asyncio\tasks.py", line 445, in wait_for
    return fut.result()
  File "C:\Python\Python310\lib\concurrent\futures\thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File C:\venv\lib\site-packages\fastrtc\tracks.py", line 436, in event_handler_emit
    return cast(Callable, self.event_handler.emit)()
  File C:\venv\lib\site-packages\fastrtc\utils.py", line 383, in sync_wrapper
    raise WebRTCError(str(e)) from e
fastrtc.utils.WebRTCError: generator already executing

Error processing frame: %s generator already executing
Assistant response: Certainly! Here's a short story for you:
...

pip install fastrtc[vad,stt,tts] Version: 0.0.5.post1; win, python 3.10.8

Katehuuh avatar Feb 26 '25 10:02 Katehuuh