Spaces:
Running
on
L40S
Running
on
L40S
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,26 @@
|
|
|
|
|
| 1 |
import re
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
-
import os
|
| 5 |
-
import io
|
| 6 |
-
import wave
|
| 7 |
-
import threading
|
| 8 |
-
import subprocess
|
| 9 |
-
import sys
|
| 10 |
-
import time
|
| 11 |
|
| 12 |
-
from
|
| 13 |
-
from
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
# Download Weights
|
| 17 |
-
os.makedirs("checkpoints", exist_ok=True)
|
| 18 |
-
snapshot_download(repo_id="fishaudio/fish-speech-1.4", local_dir="./checkpoints/fish-speech-1.4")
|
| 19 |
-
snapshot_download(repo_id="fishaudio/fish-agent-v0.1-3b", local_dir="./checkpoints/fish-agent-v0.1-3b")
|
| 20 |
-
SYSTEM_PROMPT = 'You are a voice assistant created by Fish Audio, offering end-to-end voice interaction for a seamless user experience. You are required to first transcribe the user\'s speech, then answer it in the following format: "Question: [USER_SPEECH]\n\nResponse: [YOUR_RESPONSE]\n"。You are required to use the following voice in this conversation.'
|
| 21 |
|
| 22 |
class ChatState:
|
| 23 |
def __init__(self):
|
|
@@ -54,17 +57,6 @@ class ChatState:
|
|
| 54 |
def clear_fn():
|
| 55 |
return [], ChatState(), None, None, None
|
| 56 |
|
| 57 |
-
def wav_chunk_header(sample_rate=44100, bit_depth=16, channels=1):
|
| 58 |
-
buffer = io.BytesIO()
|
| 59 |
-
|
| 60 |
-
with wave.open(buffer, "wb") as wav_file:
|
| 61 |
-
wav_file.setnchannels(channels)
|
| 62 |
-
wav_file.setsampwidth(bit_depth // 8)
|
| 63 |
-
wav_file.setframerate(sample_rate)
|
| 64 |
-
|
| 65 |
-
wav_header_bytes = buffer.getvalue()
|
| 66 |
-
buffer.close()
|
| 67 |
-
return wav_header_bytes
|
| 68 |
|
| 69 |
async def process_audio_input(
|
| 70 |
sys_audio_input, sys_text_input, audio_input, state: ChatState, text_input: str
|
|
@@ -117,16 +109,16 @@ async def process_audio_input(
|
|
| 117 |
):
|
| 118 |
if event.type == FishE2EEventType.USER_CODES:
|
| 119 |
append_to_chat_ctx(ServeVQPart(codes=event.vq_codes), role="user")
|
| 120 |
-
|
| 121 |
elif event.type == FishE2EEventType.SPEECH_SEGMENT:
|
|
|
|
|
|
|
| 122 |
append_to_chat_ctx(ServeVQPart(codes=event.vq_codes))
|
| 123 |
-
yield state.get_history(),
|
| 124 |
-
|
| 125 |
elif event.type == FishE2EEventType.TEXT_SEGMENT:
|
| 126 |
append_to_chat_ctx(ServeTextPart(text=event.text))
|
| 127 |
-
yield state.get_history(),
|
| 128 |
|
| 129 |
-
yield state.get_history(),
|
| 130 |
|
| 131 |
|
| 132 |
async def process_text_input(
|
|
@@ -153,16 +145,28 @@ def create_demo():
|
|
| 153 |
type="messages",
|
| 154 |
)
|
| 155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
notes = gr.Markdown(
|
| 157 |
"""
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
"""
|
| 167 |
)
|
| 168 |
|
|
@@ -175,20 +179,18 @@ def create_demo():
|
|
| 175 |
)
|
| 176 |
sys_text_input = gr.Textbox(
|
| 177 |
label="What is your assistant's role?",
|
| 178 |
-
value=
|
| 179 |
type="text",
|
| 180 |
)
|
| 181 |
audio_input = gr.Audio(
|
| 182 |
sources=["microphone"], type="numpy", label="Speak your message"
|
| 183 |
)
|
| 184 |
|
| 185 |
-
text_input = gr.Textbox(label="Or type your message", type="text"
|
| 186 |
|
| 187 |
output_audio = gr.Audio(
|
| 188 |
-
label="Assistant's Voice",
|
| 189 |
-
|
| 190 |
-
autoplay=True,
|
| 191 |
-
interactive=False,
|
| 192 |
)
|
| 193 |
|
| 194 |
send_button = gr.Button("Send", variant="primary")
|
|
@@ -224,18 +226,7 @@ def create_demo():
|
|
| 224 |
|
| 225 |
return demo
|
| 226 |
|
| 227 |
-
def run_api():
|
| 228 |
-
subprocess.run([sys.executable, "-m", "tools.api"])
|
| 229 |
|
| 230 |
if __name__ == "__main__":
|
| 231 |
-
|
| 232 |
-
# 创建并启动 API 线程
|
| 233 |
-
api_thread = threading.Thread(target=run_api, daemon=True)
|
| 234 |
-
api_thread.start()
|
| 235 |
-
|
| 236 |
-
# 给 API 一些时间启动
|
| 237 |
-
time.sleep(90)
|
| 238 |
-
|
| 239 |
-
# 创建并启动 Gradio demo
|
| 240 |
demo = create_demo()
|
| 241 |
-
demo.launch(share=True)
|
|
|
|
| 1 |
+
import io
|
| 2 |
import re
|
| 3 |
+
import wave
|
| 4 |
+
|
| 5 |
import gradio as gr
|
| 6 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
from .fish_e2e import FishE2EAgent, FishE2EEventType
|
| 9 |
+
from .schema import ServeMessage, ServeTextPart, ServeVQPart
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def wav_chunk_header(sample_rate=44100, bit_depth=16, channels=1):
|
| 13 |
+
buffer = io.BytesIO()
|
| 14 |
+
|
| 15 |
+
with wave.open(buffer, "wb") as wav_file:
|
| 16 |
+
wav_file.setnchannels(channels)
|
| 17 |
+
wav_file.setsampwidth(bit_depth // 8)
|
| 18 |
+
wav_file.setframerate(sample_rate)
|
| 19 |
+
|
| 20 |
+
wav_header_bytes = buffer.getvalue()
|
| 21 |
+
buffer.close()
|
| 22 |
+
return wav_header_bytes
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
class ChatState:
|
| 26 |
def __init__(self):
|
|
|
|
| 57 |
def clear_fn():
|
| 58 |
return [], ChatState(), None, None, None
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
async def process_audio_input(
|
| 62 |
sys_audio_input, sys_text_input, audio_input, state: ChatState, text_input: str
|
|
|
|
| 109 |
):
|
| 110 |
if event.type == FishE2EEventType.USER_CODES:
|
| 111 |
append_to_chat_ctx(ServeVQPart(codes=event.vq_codes), role="user")
|
|
|
|
| 112 |
elif event.type == FishE2EEventType.SPEECH_SEGMENT:
|
| 113 |
+
np_audio = np.frombuffer(event.frame.data, dtype=np.int16)
|
| 114 |
+
result_audio += np_audio
|
| 115 |
append_to_chat_ctx(ServeVQPart(codes=event.vq_codes))
|
| 116 |
+
yield state.get_history(), (44100, result_audio), None, None
|
|
|
|
| 117 |
elif event.type == FishE2EEventType.TEXT_SEGMENT:
|
| 118 |
append_to_chat_ctx(ServeTextPart(text=event.text))
|
| 119 |
+
yield state.get_history(), (44100, result_audio), None, None
|
| 120 |
|
| 121 |
+
yield state.get_history(), (44100, result_audio), None, None
|
| 122 |
|
| 123 |
|
| 124 |
async def process_text_input(
|
|
|
|
| 145 |
type="messages",
|
| 146 |
)
|
| 147 |
|
| 148 |
+
# notes = gr.Markdown(
|
| 149 |
+
# """
|
| 150 |
+
# # Fish Agent
|
| 151 |
+
# 1. 此Demo为Fish Audio自研端到端语言模型Fish Agent 3B版本.
|
| 152 |
+
# 2. 你可以在我们的官方仓库找到代码以及权重,但是相关内容全部基于 CC BY-NC-SA 4.0 许可证发布.
|
| 153 |
+
# 3. Demo为早期灰度测试版本,推理速度尚待优化.
|
| 154 |
+
# # 特色
|
| 155 |
+
# 1. 该模型自动集成ASR与TTS部分,不需要外挂其它模型,即真正的端到端,而非三段式(ASR+LLM+TTS).
|
| 156 |
+
# 2. 模型可以使用reference audio控制说话音色.
|
| 157 |
+
# 3. 可以生成具有较强情感与韵律的音频.
|
| 158 |
+
# """
|
| 159 |
+
# )
|
| 160 |
notes = gr.Markdown(
|
| 161 |
"""
|
| 162 |
+
# Fish Agent
|
| 163 |
+
1. This demo is Fish Audio's self-researh end-to-end language model, Fish Agent version 3B.
|
| 164 |
+
2. You can find the code and weights in our official repo in [gitub](https://github.com/fishaudio/fish-speech) and [hugging face](https://huggingface.co/fishaudio/fish-agent-v0.1-3b), but the content is released under a CC BY-NC-SA 4.0 licence.
|
| 165 |
+
3. The demo is an early alpha test version, the inference speed needs to be optimised.
|
| 166 |
+
# Features
|
| 167 |
+
1. The model automatically integrates ASR and TTS parts, no need to plug-in other models, i.e., true end-to-end, not three-stage (ASR+LLM+TTS).
|
| 168 |
+
2. The model can use reference audio to control the speech timbre.
|
| 169 |
+
3. The model can generate speech with strong emotion.
|
| 170 |
"""
|
| 171 |
)
|
| 172 |
|
|
|
|
| 179 |
)
|
| 180 |
sys_text_input = gr.Textbox(
|
| 181 |
label="What is your assistant's role?",
|
| 182 |
+
value="You are a voice assistant created by Fish Audio, offering end-to-end voice interaction for a seamless user experience. You are required to first transcribe the user's speech, then answer it in the following format: 'Question: [USER_SPEECH]\n\nAnswer: [YOUR_RESPONSE]\n'. You are required to use the following voice in this conversation.",
|
| 183 |
type="text",
|
| 184 |
)
|
| 185 |
audio_input = gr.Audio(
|
| 186 |
sources=["microphone"], type="numpy", label="Speak your message"
|
| 187 |
)
|
| 188 |
|
| 189 |
+
text_input = gr.Textbox(label="Or type your message", type="text")
|
| 190 |
|
| 191 |
output_audio = gr.Audio(
|
| 192 |
+
label="Assistant's Voice",
|
| 193 |
+
type="numpy",
|
|
|
|
|
|
|
| 194 |
)
|
| 195 |
|
| 196 |
send_button = gr.Button("Send", variant="primary")
|
|
|
|
| 226 |
|
| 227 |
return demo
|
| 228 |
|
|
|
|
|
|
|
| 229 |
|
| 230 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
demo = create_demo()
|
| 232 |
+
demo.launch(server_name="127.0.0.1", server_port=7860, share=True)
|