Spaces:
Running
Running
优化聊天预测功能,移除预览模式状态信息并调整界面元素布局
Browse files
app.py
CHANGED
|
@@ -153,9 +153,9 @@ def chat_predict(
|
|
| 153 |
# 如果是预览模式,返回模拟响应
|
| 154 |
if not ENABLE_VLLM:
|
| 155 |
user_display = user_text if user_text else "[音频输入]"
|
| 156 |
-
mock_response = f"
|
| 157 |
chat_history.append((user_display, mock_response))
|
| 158 |
-
return chat_history, "
|
| 159 |
|
| 160 |
messages = format_messages(system_prompt, chat_history, user_text, audio_file)
|
| 161 |
if not messages:
|
|
@@ -178,12 +178,11 @@ def chat_predict(
|
|
| 178 |
assistant_message = outputs[0].outputs[0].text
|
| 179 |
user_display = user_text if user_text else "[音频输入]"
|
| 180 |
chat_history.append((user_display, assistant_message))
|
| 181 |
-
|
| 182 |
-
return chat_history, status
|
| 183 |
except Exception as e:
|
| 184 |
import traceback
|
| 185 |
traceback.print_exc()
|
| 186 |
-
return chat_history,
|
| 187 |
|
| 188 |
|
| 189 |
def _get_llm() -> LLM:
|
|
@@ -210,52 +209,28 @@ def _set_llm_args(**kwargs) -> None:
|
|
| 210 |
_llm = None # 确保使用新配置重新加载
|
| 211 |
|
| 212 |
|
| 213 |
-
def check_model_status() -> str:
|
| 214 |
-
"""返回模型当前加载状态"""
|
| 215 |
-
if not ENABLE_VLLM:
|
| 216 |
-
return "⚙ 界面预览模式(vLLM 未启用)"
|
| 217 |
-
|
| 218 |
-
model_path = LLM_ARGS["model"]
|
| 219 |
-
if _llm is None:
|
| 220 |
-
return f"等待加载:{model_path}"
|
| 221 |
-
return f"✓ 已加载模型:{model_path}"
|
| 222 |
|
| 223 |
|
| 224 |
-
def warmup_model() -> str:
|
| 225 |
-
"""主动加载模型"""
|
| 226 |
-
if not ENABLE_VLLM:
|
| 227 |
-
return "⚙ 界面预览模式(vLLM 未启用)"
|
| 228 |
-
|
| 229 |
-
try:
|
| 230 |
-
_get_llm()
|
| 231 |
-
return check_model_status()
|
| 232 |
-
except Exception as exc:
|
| 233 |
-
import traceback
|
| 234 |
-
traceback.print_exc()
|
| 235 |
-
return f"✗ 模型加载失败: {exc}"
|
| 236 |
-
|
| 237 |
|
| 238 |
# 构建 Gradio 界面
|
| 239 |
with gr.Blocks(title="Step Audio 2 Chat", theme=gr.themes.Soft()) as demo:
|
| 240 |
gr.Markdown(
|
| 241 |
"""
|
| 242 |
-
# Step Audio
|
| 243 |
-
|
| 244 |
-
支持文本和音频输入的聊天界面,直接在本地 vLLM 引擎上推理。
|
| 245 |
"""
|
| 246 |
)
|
| 247 |
|
| 248 |
-
# 模型状态
|
| 249 |
-
with gr.Row():
|
| 250 |
-
status_text = gr.Textbox(
|
| 251 |
-
label="模型状态",
|
| 252 |
-
value="检查中...",
|
| 253 |
-
interactive=False
|
| 254 |
-
)
|
| 255 |
-
check_btn = gr.Button("加载/检查模型", variant="secondary")
|
| 256 |
-
|
| 257 |
with gr.Row():
|
| 258 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
with gr.Column(scale=1):
|
| 260 |
gr.Markdown("### 输入设置")
|
| 261 |
|
|
@@ -275,15 +250,15 @@ with gr.Blocks(title="Step Audio 2 Chat", theme=gr.themes.Soft()) as demo:
|
|
| 275 |
audio_file = gr.Audio(
|
| 276 |
label="音频输入",
|
| 277 |
type="filepath",
|
| 278 |
-
sources=["
|
| 279 |
)
|
| 280 |
|
| 281 |
with gr.Row():
|
| 282 |
max_tokens = gr.Slider(
|
| 283 |
label="Max Tokens",
|
| 284 |
minimum=1,
|
| 285 |
-
maximum=
|
| 286 |
-
value=
|
| 287 |
step=1
|
| 288 |
)
|
| 289 |
|
|
@@ -306,19 +281,10 @@ with gr.Blocks(title="Step Audio 2 Chat", theme=gr.themes.Soft()) as demo:
|
|
| 306 |
|
| 307 |
submit_btn = gr.Button("提交", variant="primary", size="lg")
|
| 308 |
clear_btn = gr.Button("清空", variant="secondary")
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
with gr.Column(scale=1):
|
| 312 |
-
gr.Markdown("### 聊天历史")
|
| 313 |
-
chatbot = gr.Chatbot(
|
| 314 |
-
label="对话",
|
| 315 |
-
height=600,
|
| 316 |
-
show_copy_button=True
|
| 317 |
-
)
|
| 318 |
|
| 319 |
# 事件绑定
|
| 320 |
-
check_btn.click(fn=warmup_model, outputs=status_text)
|
| 321 |
-
|
| 322 |
submit_btn.click(
|
| 323 |
fn=chat_predict,
|
| 324 |
inputs=[
|
|
@@ -337,9 +303,6 @@ with gr.Blocks(title="Step Audio 2 Chat", theme=gr.themes.Soft()) as demo:
|
|
| 337 |
fn=lambda: ([], "", None),
|
| 338 |
outputs=[chatbot, user_text, audio_file]
|
| 339 |
)
|
| 340 |
-
|
| 341 |
-
# 页面加载时显示状态
|
| 342 |
-
demo.load(fn=check_model_status, outputs=status_text)
|
| 343 |
|
| 344 |
|
| 345 |
if __name__ == "__main__":
|
|
|
|
| 153 |
# 如果是预览模式,返回模拟响应
|
| 154 |
if not ENABLE_VLLM:
|
| 155 |
user_display = user_text if user_text else "[音频输入]"
|
| 156 |
+
mock_response = f"这是一个模拟回复。您说: {user_text[:50] if user_text else '音频'}"
|
| 157 |
chat_history.append((user_display, mock_response))
|
| 158 |
+
return chat_history, ""
|
| 159 |
|
| 160 |
messages = format_messages(system_prompt, chat_history, user_text, audio_file)
|
| 161 |
if not messages:
|
|
|
|
| 178 |
assistant_message = outputs[0].outputs[0].text
|
| 179 |
user_display = user_text if user_text else "[音频输入]"
|
| 180 |
chat_history.append((user_display, assistant_message))
|
| 181 |
+
return chat_history, ""
|
|
|
|
| 182 |
except Exception as e:
|
| 183 |
import traceback
|
| 184 |
traceback.print_exc()
|
| 185 |
+
return chat_history, ""
|
| 186 |
|
| 187 |
|
| 188 |
def _get_llm() -> LLM:
|
|
|
|
| 209 |
_llm = None # 确保使用新配置重新加载
|
| 210 |
|
| 211 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
# 构建 Gradio 界面
|
| 216 |
with gr.Blocks(title="Step Audio 2 Chat", theme=gr.themes.Soft()) as demo:
|
| 217 |
gr.Markdown(
|
| 218 |
"""
|
| 219 |
+
# Step Audio R1 Demo
|
|
|
|
|
|
|
| 220 |
"""
|
| 221 |
)
|
| 222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
with gr.Row():
|
| 224 |
+
# 左侧:聊天历史
|
| 225 |
+
with gr.Column(scale=1):
|
| 226 |
+
gr.Markdown("### 聊天历史")
|
| 227 |
+
chatbot = gr.Chatbot(
|
| 228 |
+
label="对话",
|
| 229 |
+
height=600,
|
| 230 |
+
show_copy_button=True
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
# 右侧:输入区域
|
| 234 |
with gr.Column(scale=1):
|
| 235 |
gr.Markdown("### 输入设置")
|
| 236 |
|
|
|
|
| 250 |
audio_file = gr.Audio(
|
| 251 |
label="音频输入",
|
| 252 |
type="filepath",
|
| 253 |
+
sources=["microphone", "upload"]
|
| 254 |
)
|
| 255 |
|
| 256 |
with gr.Row():
|
| 257 |
max_tokens = gr.Slider(
|
| 258 |
label="Max Tokens",
|
| 259 |
minimum=1,
|
| 260 |
+
maximum=16384,
|
| 261 |
+
value=8192,
|
| 262 |
step=1
|
| 263 |
)
|
| 264 |
|
|
|
|
| 281 |
|
| 282 |
submit_btn = gr.Button("提交", variant="primary", size="lg")
|
| 283 |
clear_btn = gr.Button("清空", variant="secondary")
|
| 284 |
+
|
| 285 |
+
status_text = gr.Textbox(label="状态", interactive=False, visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
|
| 287 |
# 事件绑定
|
|
|
|
|
|
|
| 288 |
submit_btn.click(
|
| 289 |
fn=chat_predict,
|
| 290 |
inputs=[
|
|
|
|
| 303 |
fn=lambda: ([], "", None),
|
| 304 |
outputs=[chatbot, user_text, audio_file]
|
| 305 |
)
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
|
| 308 |
if __name__ == "__main__":
|