Spaces:
Configuration error
Configuration error
Streaming agent
#2
by SpicyqSama007 - opened
1 unmodified line2345678944 unmodified lines545556575859606162636465666768697014 unmodified lines85868775888990787991929323 unmodified lines11711811912012111011112211311412312412512611711811912012112712812312412913013113251 unmodified lines1841851861821871881891901911921931941951 unmodified lineimport gradio as grimport numpy as npimport osimport ioimport waveimport threadingimport subprocessimport sys44 unmodified linesdef clear_fn(): return [], ChatState(), None, None, None
def wav_chunk_header(sample_rate=44100, bit_depth=16, channels=1): buffer = io.BytesIO()
with wave.open(buffer, "wb") as wav_file: wav_file.setnchannels(channels) wav_file.setsampwidth(bit_depth // 8) wav_file.setframerate(sample_rate)
wav_header_bytes = buffer.getvalue() buffer.close() return wav_header_bytes
async def process_audio_input( sys_audio_input, sys_text_input, audio_input, state: ChatState, text_input: str14 unmodified lines
if isinstance(sys_audio_input, tuple): sr, sys_audio_data = sys_audio_input elif text_input: else: sr = 44100 sys_audio_data = None else: raise gr.Error("Invalid audio format")
def append_to_chat_ctx( part: ServeTextPart | ServeVQPart, role: str = "assistant"23 unmodified lines ): if event.type == FishE2EEventType.USER_CODES: append_to_chat_ctx(ServeVQPart(codes=event.vq_codes), role="user") elif event.type == FishE2EEventType.SPEECH_SEGMENT: result_audio += event.frame.data np_audio = np.frombuffer(result_audio, dtype=np.int16) append_to_chat_ctx(ServeVQPart(codes=event.vq_codes))
yield state.get_history(), (44100, np_audio), None, None yield state.get_history(), wav_chunk_header() + event.frame.data, None, None elif event.type == FishE2EEventType.TEXT_SEGMENT: append_to_chat_ctx(ServeTextPart(text=event.text)) if result_audio: np_audio = np.frombuffer(result_audio, dtype=np.int16) yield state.get_history(), (44100, np_audio), None, None else: yield state.get_history(), None, None, None yield state.get_history(), None, None, None
np_audio = np.frombuffer(result_audio, dtype=np.int16) yield state.get_history(), (44100, np_audio), None, None yield state.get_history(), None, None, None
async def process_text_input(51 unmodified lines
text_input = gr.Textbox(label="Or type your message", type="text",value="Can you give a brief introduction of yourself?")
output_audio = gr.Audio(label="Assistant's Voice", type="numpy") output_audio = gr.Audio( label="Assistant's Voice", streaming=True, autoplay=True, interactive=False, )
send_button = gr.Button("Send", variant="primary") clear_button = gr.Button("Clear")app.py+25-15