Spaces:
Runtime error
Runtime error
cody-charles commited on
Commit ·
b8baefc
1
Parent(s): e5c7bdb
Fix Lexi native prompt framing and Gradio 6 history
Browse files
app.py
CHANGED
|
@@ -21,24 +21,72 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 21 |
model.eval()
|
| 22 |
print("Model ready.")
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
@spaces.GPU(duration=120)
|
| 25 |
def chat(message, history):
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
for item in history or []:
|
| 29 |
-
if isinstance(item, dict):
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
messages.append({
|
| 40 |
"role": "user",
|
| 41 |
-
"content":
|
| 42 |
})
|
| 43 |
|
| 44 |
inputs = tokenizer.apply_chat_template(
|
|
@@ -68,6 +116,7 @@ def chat(message, history):
|
|
| 68 |
)
|
| 69 |
|
| 70 |
prompt_length = inputs["input_ids"].shape[-1]
|
|
|
|
| 71 |
new_tokens = output[0, prompt_length:]
|
| 72 |
|
| 73 |
response = tokenizer.decode(
|
|
@@ -77,10 +126,11 @@ def chat(message, history):
|
|
| 77 |
|
| 78 |
return response.strip()
|
| 79 |
|
|
|
|
| 80 |
demo = gr.ChatInterface(
|
| 81 |
fn=chat,
|
| 82 |
title="Lexi 8B — OBLITERATED",
|
| 83 |
-
description="
|
| 84 |
)
|
| 85 |
|
| 86 |
demo.launch()
|
|
|
|
| 21 |
model.eval()
|
| 22 |
print("Model ready.")
|
| 23 |
|
| 24 |
+
|
| 25 |
+
def extract_text(content):
|
| 26 |
+
if content is None:
|
| 27 |
+
return ""
|
| 28 |
+
|
| 29 |
+
if isinstance(content, str):
|
| 30 |
+
return content
|
| 31 |
+
|
| 32 |
+
if isinstance(content, list):
|
| 33 |
+
parts = []
|
| 34 |
+
|
| 35 |
+
for block in content:
|
| 36 |
+
text = extract_text(block)
|
| 37 |
+
|
| 38 |
+
if text:
|
| 39 |
+
parts.append(text)
|
| 40 |
+
|
| 41 |
+
return "\n".join(parts)
|
| 42 |
+
|
| 43 |
+
if isinstance(content, dict):
|
| 44 |
+
if content.get("type") == "text":
|
| 45 |
+
return extract_text(content.get("text"))
|
| 46 |
+
|
| 47 |
+
if "text" in content:
|
| 48 |
+
return extract_text(content["text"])
|
| 49 |
+
|
| 50 |
+
if "content" in content:
|
| 51 |
+
return extract_text(content["content"])
|
| 52 |
+
|
| 53 |
+
return ""
|
| 54 |
+
|
| 55 |
+
|
| 56 |
@spaces.GPU(duration=120)
|
| 57 |
def chat(message, history):
|
| 58 |
+
|
| 59 |
+
# Lexi V2 expects a system turn to exist.
|
| 60 |
+
# "." is the model author's recommended effectively-empty value.
|
| 61 |
+
messages = [
|
| 62 |
+
{
|
| 63 |
+
"role": "system",
|
| 64 |
+
"content": "."
|
| 65 |
+
}
|
| 66 |
+
]
|
| 67 |
|
| 68 |
for item in history or []:
|
| 69 |
+
if not isinstance(item, dict):
|
| 70 |
+
continue
|
| 71 |
+
|
| 72 |
+
role = item.get("role")
|
| 73 |
+
|
| 74 |
+
if role not in ("user", "assistant"):
|
| 75 |
+
continue
|
| 76 |
|
| 77 |
+
text = extract_text(item.get("content"))
|
| 78 |
+
|
| 79 |
+
if text:
|
| 80 |
+
messages.append({
|
| 81 |
+
"role": role,
|
| 82 |
+
"content": text
|
| 83 |
+
})
|
| 84 |
+
|
| 85 |
+
user_text = extract_text(message)
|
| 86 |
|
| 87 |
messages.append({
|
| 88 |
"role": "user",
|
| 89 |
+
"content": user_text
|
| 90 |
})
|
| 91 |
|
| 92 |
inputs = tokenizer.apply_chat_template(
|
|
|
|
| 116 |
)
|
| 117 |
|
| 118 |
prompt_length = inputs["input_ids"].shape[-1]
|
| 119 |
+
|
| 120 |
new_tokens = output[0, prompt_length:]
|
| 121 |
|
| 122 |
response = tokenizer.decode(
|
|
|
|
| 126 |
|
| 127 |
return response.strip()
|
| 128 |
|
| 129 |
+
|
| 130 |
demo = gr.ChatInterface(
|
| 131 |
fn=chat,
|
| 132 |
title="Lexi 8B — OBLITERATED",
|
| 133 |
+
description="Native Lexi framing • temp 1.0 • top-p 1.0 • 4000 max tokens",
|
| 134 |
)
|
| 135 |
|
| 136 |
demo.launch()
|