Nafiul Huda commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,7 +36,7 @@ print(f"[model] loaded on: {next(model.parameters()).device}")
|
|
| 36 |
|
| 37 |
|
| 38 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 39 |
-
# TTS β persistent event loop
|
| 40 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 41 |
_tts_loop = None
|
| 42 |
|
|
@@ -56,22 +56,20 @@ def text_to_speech(text: str) -> str:
|
|
| 56 |
|
| 57 |
|
| 58 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 59 |
-
# INFERENCE
|
| 60 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 61 |
@spaces.GPU(duration=120)
|
| 62 |
def chat_and_speak(
|
| 63 |
user_message: str,
|
| 64 |
-
history: list,
|
| 65 |
system_prompt: str,
|
| 66 |
max_new_tokens: int,
|
| 67 |
temperature: float,
|
| 68 |
enable_thinking: bool,
|
| 69 |
enable_tts: bool,
|
| 70 |
):
|
| 71 |
-
# Build messages
|
| 72 |
messages = [{"role": "system", "content": system_prompt}]
|
| 73 |
-
|
| 74 |
-
# history is already in the correct format (list of role/content dicts)
|
| 75 |
messages.extend(history)
|
| 76 |
messages.append({"role": "user", "content": user_message})
|
| 77 |
|
|
@@ -85,7 +83,7 @@ def chat_and_speak(
|
|
| 85 |
return_tensors = "pt",
|
| 86 |
).to(model.device)
|
| 87 |
|
| 88 |
-
#
|
| 89 |
streamer = TextIteratorStreamer(
|
| 90 |
tokenizer,
|
| 91 |
skip_prompt = True,
|
|
@@ -104,7 +102,7 @@ def chat_and_speak(
|
|
| 104 |
thread = Thread(target=model.generate, kwargs=gen_kwargs)
|
| 105 |
thread.start()
|
| 106 |
|
| 107 |
-
# Stream
|
| 108 |
partial_reply = ""
|
| 109 |
current_history = history + [
|
| 110 |
{"role": "user", "content": user_message},
|
|
@@ -118,7 +116,7 @@ def chat_and_speak(
|
|
| 118 |
|
| 119 |
thread.join()
|
| 120 |
|
| 121 |
-
#
|
| 122 |
clean_reply = re.sub(
|
| 123 |
r"<think>.*?</think>", "", partial_reply, flags=re.DOTALL
|
| 124 |
).strip()
|
|
@@ -138,7 +136,7 @@ def clear_all():
|
|
| 138 |
|
| 139 |
|
| 140 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 141 |
-
#
|
| 142 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 143 |
with gr.Blocks(title="MiniCPM5-1B Chat") as demo:
|
| 144 |
|
|
@@ -158,7 +156,6 @@ with gr.Blocks(title="MiniCPM5-1B Chat") as demo:
|
|
| 158 |
chatbot = gr.Chatbot(
|
| 159 |
elem_id="chatbot",
|
| 160 |
label="Conversation",
|
| 161 |
-
type="messages", # β Gradio 5 explicit messages format
|
| 162 |
)
|
| 163 |
with gr.Row():
|
| 164 |
user_input = gr.Textbox(
|
|
|
|
| 36 |
|
| 37 |
|
| 38 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 39 |
+
# TTS β persistent event loop
|
| 40 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 41 |
_tts_loop = None
|
| 42 |
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 59 |
+
# INFERENCE
|
| 60 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 61 |
@spaces.GPU(duration=120)
|
| 62 |
def chat_and_speak(
|
| 63 |
user_message: str,
|
| 64 |
+
history: list,
|
| 65 |
system_prompt: str,
|
| 66 |
max_new_tokens: int,
|
| 67 |
temperature: float,
|
| 68 |
enable_thinking: bool,
|
| 69 |
enable_tts: bool,
|
| 70 |
):
|
| 71 |
+
# Build messages β Gradio 5 uses [{"role": ..., "content": ...}, ...]
|
| 72 |
messages = [{"role": "system", "content": system_prompt}]
|
|
|
|
|
|
|
| 73 |
messages.extend(history)
|
| 74 |
messages.append({"role": "user", "content": user_message})
|
| 75 |
|
|
|
|
| 83 |
return_tensors = "pt",
|
| 84 |
).to(model.device)
|
| 85 |
|
| 86 |
+
# Streaming
|
| 87 |
streamer = TextIteratorStreamer(
|
| 88 |
tokenizer,
|
| 89 |
skip_prompt = True,
|
|
|
|
| 102 |
thread = Thread(target=model.generate, kwargs=gen_kwargs)
|
| 103 |
thread.start()
|
| 104 |
|
| 105 |
+
# Stream
|
| 106 |
partial_reply = ""
|
| 107 |
current_history = history + [
|
| 108 |
{"role": "user", "content": user_message},
|
|
|
|
| 116 |
|
| 117 |
thread.join()
|
| 118 |
|
| 119 |
+
# Strip <think> blocks
|
| 120 |
clean_reply = re.sub(
|
| 121 |
r"<think>.*?</think>", "", partial_reply, flags=re.DOTALL
|
| 122 |
).strip()
|
|
|
|
| 136 |
|
| 137 |
|
| 138 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 139 |
+
# UI
|
| 140 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 141 |
with gr.Blocks(title="MiniCPM5-1B Chat") as demo:
|
| 142 |
|
|
|
|
| 156 |
chatbot = gr.Chatbot(
|
| 157 |
elem_id="chatbot",
|
| 158 |
label="Conversation",
|
|
|
|
| 159 |
)
|
| 160 |
with gr.Row():
|
| 161 |
user_input = gr.Textbox(
|