Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,7 +19,7 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 19 |
subfolder=SUBFOLDER,
|
| 20 |
dtype=torch.float16,
|
| 21 |
low_cpu_mem_usage=True,
|
| 22 |
-
device_map="cpu",
|
| 23 |
)
|
| 24 |
model.eval()
|
| 25 |
|
|
@@ -41,23 +41,19 @@ STYLE_SYSTEM_PROMPTS = {
|
|
| 41 |
|
| 42 |
def build_prompt(message, history, style):
|
| 43 |
"""
|
| 44 |
-
history
|
| 45 |
-
[
|
| 46 |
-
|
| 47 |
-
|
| 48 |
"""
|
| 49 |
messages = []
|
| 50 |
|
| 51 |
-
# System / style prompt
|
| 52 |
system_prompt = STYLE_SYSTEM_PROMPTS.get(style, STYLE_SYSTEM_PROMPTS["Default"])
|
| 53 |
messages.append({"role": "system", "content": system_prompt})
|
| 54 |
|
| 55 |
-
# Tidigare dialog
|
| 56 |
-
|
| 57 |
-
if user_msg:
|
| 58 |
-
messages.append({"role": "user", "content": user_msg})
|
| 59 |
-
if bot_msg:
|
| 60 |
-
messages.append({"role": "assistant", "content": bot_msg})
|
| 61 |
|
| 62 |
# Nuvarande användarmeddelande
|
| 63 |
messages.append({"role": "user", "content": message})
|
|
@@ -71,7 +67,7 @@ def build_prompt(message, history, style):
|
|
| 71 |
|
| 72 |
|
| 73 |
def chat_fn(message, history, max_new_tokens, temperature, top_p, repetition_penalty, style):
|
| 74 |
-
#
|
| 75 |
prompt = build_prompt(message, history, style)
|
| 76 |
|
| 77 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
|
@@ -110,9 +106,14 @@ def chat_fn(message, history, max_new_tokens, temperature, top_p, repetition_pen
|
|
| 110 |
skip_special_tokens=True,
|
| 111 |
).strip()
|
| 112 |
|
| 113 |
-
#
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
|
| 118 |
with gr.Blocks() as demo:
|
|
@@ -127,7 +128,7 @@ with gr.Blocks() as demo:
|
|
| 127 |
with gr.Row():
|
| 128 |
# Left: chat
|
| 129 |
with gr.Column(scale=3):
|
| 130 |
-
chatbot = gr.Chatbot(label="Chat")
|
| 131 |
msg = gr.Textbox(
|
| 132 |
label="Your message",
|
| 133 |
placeholder="Ask the model something...",
|
|
@@ -180,7 +181,7 @@ with gr.Blocks() as demo:
|
|
| 180 |
label="Answer style",
|
| 181 |
)
|
| 182 |
|
| 183 |
-
# Koppla
|
| 184 |
send_btn.click(
|
| 185 |
chat_fn,
|
| 186 |
inputs=[msg, chatbot, max_new_tokens, temperature, top_p, repetition_penalty, style],
|
|
|
|
| 19 |
subfolder=SUBFOLDER,
|
| 20 |
dtype=torch.float16,
|
| 21 |
low_cpu_mem_usage=True,
|
| 22 |
+
device_map="cpu",
|
| 23 |
)
|
| 24 |
model.eval()
|
| 25 |
|
|
|
|
| 41 |
|
| 42 |
def build_prompt(message, history, style):
|
| 43 |
"""
|
| 44 |
+
history är i 'messages'-format:
|
| 45 |
+
[{"role": "user", "content": "..."},
|
| 46 |
+
{"role": "assistant", "content": "..."}, ...]
|
| 47 |
+
Vi lägger till en systemprompt + tidigare meddelanden + nuvarande fråga.
|
| 48 |
"""
|
| 49 |
messages = []
|
| 50 |
|
| 51 |
+
# System / style prompt (läggs bara i prompten, inte i history)
|
| 52 |
system_prompt = STYLE_SYSTEM_PROMPTS.get(style, STYLE_SYSTEM_PROMPTS["Default"])
|
| 53 |
messages.append({"role": "system", "content": system_prompt})
|
| 54 |
|
| 55 |
+
# Tidigare dialog (bara user/assistant)
|
| 56 |
+
messages.extend(history)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
# Nuvarande användarmeddelande
|
| 59 |
messages.append({"role": "user", "content": message})
|
|
|
|
| 67 |
|
| 68 |
|
| 69 |
def chat_fn(message, history, max_new_tokens, temperature, top_p, repetition_penalty, style):
|
| 70 |
+
# history är redan i messages-format, vi gör inte om det
|
| 71 |
prompt = build_prompt(message, history, style)
|
| 72 |
|
| 73 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
|
|
|
| 106 |
skip_special_tokens=True,
|
| 107 |
).strip()
|
| 108 |
|
| 109 |
+
# Uppdatera history i messages-format:
|
| 110 |
+
new_history = history + [
|
| 111 |
+
{"role": "user", "content": message},
|
| 112 |
+
{"role": "assistant", "content": generated},
|
| 113 |
+
]
|
| 114 |
+
|
| 115 |
+
# Töm textboxen, uppdatera chatten
|
| 116 |
+
return "", new_history
|
| 117 |
|
| 118 |
|
| 119 |
with gr.Blocks() as demo:
|
|
|
|
| 128 |
with gr.Row():
|
| 129 |
# Left: chat
|
| 130 |
with gr.Column(scale=3):
|
| 131 |
+
chatbot = gr.Chatbot(label="Chat", type="messages")
|
| 132 |
msg = gr.Textbox(
|
| 133 |
label="Your message",
|
| 134 |
placeholder="Ask the model something...",
|
|
|
|
| 181 |
label="Answer style",
|
| 182 |
)
|
| 183 |
|
| 184 |
+
# Koppla knappar / enter
|
| 185 |
send_btn.click(
|
| 186 |
chat_fn,
|
| 187 |
inputs=[msg, chatbot, max_new_tokens, temperature, top_p, repetition_penalty, style],
|