Update app.py
Browse files
app.py
CHANGED
|
@@ -134,13 +134,15 @@ SYSTEM_PROMPT = (
|
|
| 134 |
STOP_STRINGS = ["\nUser:", "\nAssistant:"]
|
| 135 |
|
| 136 |
def build_prompt(message, history):
|
| 137 |
-
"""Build prompt from message history (list of
|
| 138 |
prompt = SYSTEM_PROMPT
|
| 139 |
-
for
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
if
|
| 143 |
-
prompt += f"
|
|
|
|
|
|
|
| 144 |
prompt += f"User: {message}\nAssistant:"
|
| 145 |
return prompt
|
| 146 |
|
|
@@ -178,30 +180,35 @@ def generate_stream(prompt, max_tokens=256):
|
|
| 178 |
with gr.Blocks() as demo:
|
| 179 |
gr.Markdown("# 🌠 OLM North Star")
|
| 180 |
|
| 181 |
-
#
|
|
|
|
| 182 |
chat = gr.Chatbot(height=550)
|
| 183 |
box = gr.Textbox(placeholder="Say hi…", label="Input")
|
| 184 |
|
| 185 |
def user_step(msg, hist):
|
| 186 |
if hist is None:
|
| 187 |
hist = []
|
| 188 |
-
#
|
| 189 |
-
|
|
|
|
| 190 |
|
| 191 |
def bot_step(hist):
|
| 192 |
-
if not hist or hist[-1][
|
| 193 |
yield hist
|
| 194 |
return
|
| 195 |
|
| 196 |
-
user_msg = hist[-1][
|
| 197 |
-
context = hist[:-1]
|
| 198 |
|
| 199 |
prompt = build_prompt(user_msg, context)
|
| 200 |
|
|
|
|
|
|
|
|
|
|
| 201 |
bot_text = ""
|
| 202 |
for chunk in generate_stream(prompt):
|
| 203 |
bot_text = chunk
|
| 204 |
-
hist[-1][
|
| 205 |
yield hist
|
| 206 |
|
| 207 |
box.submit(user_step, [box, chat], [box, chat], queue=False).then(
|
|
|
|
| 134 |
STOP_STRINGS = ["\nUser:", "\nAssistant:"]
|
| 135 |
|
| 136 |
def build_prompt(message, history):
|
| 137 |
+
"""Build prompt from message history (list of dictionaries)."""
|
| 138 |
prompt = SYSTEM_PROMPT
|
| 139 |
+
for msg in history:
|
| 140 |
+
role = msg.get("role")
|
| 141 |
+
content = msg.get("content")
|
| 142 |
+
if role == "user":
|
| 143 |
+
prompt += f"User: {content}\n"
|
| 144 |
+
elif role == "assistant" and content:
|
| 145 |
+
prompt += f"Assistant: {content}\n"
|
| 146 |
prompt += f"User: {message}\nAssistant:"
|
| 147 |
return prompt
|
| 148 |
|
|
|
|
| 180 |
with gr.Blocks() as demo:
|
| 181 |
gr.Markdown("# 🌠 OLM North Star")
|
| 182 |
|
| 183 |
+
# We do not pass type="messages" to avoid the TypeError
|
| 184 |
+
# But we provide dictionary-based data to satisfy the internal postprocess check
|
| 185 |
chat = gr.Chatbot(height=550)
|
| 186 |
box = gr.Textbox(placeholder="Say hi…", label="Input")
|
| 187 |
|
| 188 |
def user_step(msg, hist):
|
| 189 |
if hist is None:
|
| 190 |
hist = []
|
| 191 |
+
# Modern Gradio requires dictionaries for history even if 'type' isn't set
|
| 192 |
+
hist.append({"role": "user", "content": msg})
|
| 193 |
+
return "", hist
|
| 194 |
|
| 195 |
def bot_step(hist):
|
| 196 |
+
if not hist or hist[-1]["role"] != "user":
|
| 197 |
yield hist
|
| 198 |
return
|
| 199 |
|
| 200 |
+
user_msg = hist[-1]["content"]
|
| 201 |
+
context = hist[:-1]
|
| 202 |
|
| 203 |
prompt = build_prompt(user_msg, context)
|
| 204 |
|
| 205 |
+
# Append assistant response dictionary
|
| 206 |
+
hist.append({"role": "assistant", "content": ""})
|
| 207 |
+
|
| 208 |
bot_text = ""
|
| 209 |
for chunk in generate_stream(prompt):
|
| 210 |
bot_text = chunk
|
| 211 |
+
hist[-1]["content"] = bot_text
|
| 212 |
yield hist
|
| 213 |
|
| 214 |
box.submit(user_step, [box, chat], [box, chat], queue=False).then(
|