wesjos commited on
Commit
203de2a
·
verified ·
1 Parent(s): e9e9c38

update: wesjos/Spark-x2.6-4B-HTML

Browse files
.gitattributes CHANGED
@@ -1,35 +1,50 @@
1
  *.7z filter=lfs diff=lfs merge=lfs -text
2
  *.arrow filter=lfs diff=lfs merge=lfs -text
3
  *.bin filter=lfs diff=lfs merge=lfs -text
 
4
  *.bz2 filter=lfs diff=lfs merge=lfs -text
5
- *.ckpt filter=lfs diff=lfs merge=lfs -text
6
  *.ftz filter=lfs diff=lfs merge=lfs -text
7
  *.gz filter=lfs diff=lfs merge=lfs -text
8
  *.h5 filter=lfs diff=lfs merge=lfs -text
9
  *.joblib filter=lfs diff=lfs merge=lfs -text
10
  *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
- *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
  *.model filter=lfs diff=lfs merge=lfs -text
13
  *.msgpack filter=lfs diff=lfs merge=lfs -text
14
- *.npy filter=lfs diff=lfs merge=lfs -text
15
- *.npz filter=lfs diff=lfs merge=lfs -text
16
  *.onnx filter=lfs diff=lfs merge=lfs -text
17
  *.ot filter=lfs diff=lfs merge=lfs -text
18
  *.parquet filter=lfs diff=lfs merge=lfs -text
19
  *.pb filter=lfs diff=lfs merge=lfs -text
20
- *.pickle filter=lfs diff=lfs merge=lfs -text
21
- *.pkl filter=lfs diff=lfs merge=lfs -text
22
  *.pt filter=lfs diff=lfs merge=lfs -text
23
  *.pth filter=lfs diff=lfs merge=lfs -text
24
  *.rar filter=lfs diff=lfs merge=lfs -text
25
- *.safetensors filter=lfs diff=lfs merge=lfs -text
26
  saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
  *.tar.* filter=lfs diff=lfs merge=lfs -text
28
- *.tar filter=lfs diff=lfs merge=lfs -text
29
  *.tflite filter=lfs diff=lfs merge=lfs -text
30
  *.tgz filter=lfs diff=lfs merge=lfs -text
31
- *.wasm filter=lfs diff=lfs merge=lfs -text
32
  *.xz filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
1
  *.7z filter=lfs diff=lfs merge=lfs -text
2
  *.arrow filter=lfs diff=lfs merge=lfs -text
3
  *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bin.* filter=lfs diff=lfs merge=lfs -text
5
  *.bz2 filter=lfs diff=lfs merge=lfs -text
 
6
  *.ftz filter=lfs diff=lfs merge=lfs -text
7
  *.gz filter=lfs diff=lfs merge=lfs -text
8
  *.h5 filter=lfs diff=lfs merge=lfs -text
9
  *.joblib filter=lfs diff=lfs merge=lfs -text
10
  *.lfs.* filter=lfs diff=lfs merge=lfs -text
 
11
  *.model filter=lfs diff=lfs merge=lfs -text
12
  *.msgpack filter=lfs diff=lfs merge=lfs -text
 
 
13
  *.onnx filter=lfs diff=lfs merge=lfs -text
14
  *.ot filter=lfs diff=lfs merge=lfs -text
15
  *.parquet filter=lfs diff=lfs merge=lfs -text
16
  *.pb filter=lfs diff=lfs merge=lfs -text
 
 
17
  *.pt filter=lfs diff=lfs merge=lfs -text
18
  *.pth filter=lfs diff=lfs merge=lfs -text
19
  *.rar filter=lfs diff=lfs merge=lfs -text
 
20
  saved_model/**/* filter=lfs diff=lfs merge=lfs -text
21
  *.tar.* filter=lfs diff=lfs merge=lfs -text
 
22
  *.tflite filter=lfs diff=lfs merge=lfs -text
23
  *.tgz filter=lfs diff=lfs merge=lfs -text
 
24
  *.xz filter=lfs diff=lfs merge=lfs -text
25
  *.zip filter=lfs diff=lfs merge=lfs -text
26
+ *.zstandard filter=lfs diff=lfs merge=lfs -text
27
+ *.tfevents* filter=lfs diff=lfs merge=lfs -text
28
+ *.db* filter=lfs diff=lfs merge=lfs -text
29
+ *.ark* filter=lfs diff=lfs merge=lfs -text
30
+ **/*ckpt*data* filter=lfs diff=lfs merge=lfs -text
31
+ **/*ckpt*.meta filter=lfs diff=lfs merge=lfs -text
32
+ **/*ckpt*.index filter=lfs diff=lfs merge=lfs -text
33
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
34
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
35
+ *.gguf* filter=lfs diff=lfs merge=lfs -text
36
+ *.ggml filter=lfs diff=lfs merge=lfs -text
37
+ *.llamafile* filter=lfs diff=lfs merge=lfs -text
38
+ *.pt2 filter=lfs diff=lfs merge=lfs -text
39
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
40
+ *.npy filter=lfs diff=lfs merge=lfs -text
41
+ *.npz filter=lfs diff=lfs merge=lfs -text
42
+ *.pickle filter=lfs diff=lfs merge=lfs -text
43
+ *.pkl filter=lfs diff=lfs merge=lfs -text
44
+ *.tar filter=lfs diff=lfs merge=lfs -text
45
+ *.wasm filter=lfs diff=lfs merge=lfs -text
46
  *.zst filter=lfs diff=lfs merge=lfs -text
47
  *tfevents* filter=lfs diff=lfs merge=lfs -text
48
+ "/tokenizer.json" filter=lfs diff=lfs merge=lfs -text
49
+ "/merges.txt" filter=lfs diff=lfs merge=lfs -text
50
+ "/vocab.json" filter=lfs diff=lfs merge=lfs -text
chat_template.jinja ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- if not messages %}
2
+ {{- raise_exception('No messages provided.') }}
3
+ {%- endif %}
4
+ {%- set enable_thinking = enable_thinking | default(true) %}
5
+ {%- set reasoning_instructions = '' %}
6
+ {%- if enable_thinking is true %}
7
+ {%- set raw_effort = reasoning_effort|default('high') %}
8
+ {%- if raw_effort in ('low', 'none', 'off') %}
9
+ {%- set resolved_reasoning_effort = 'low' %}
10
+ {%- else %}
11
+ {%- set resolved_reasoning_effort = 'high' %}
12
+ {%- endif %}
13
+ {%- if resolved_reasoning_effort == 'high' %}
14
+ {%- set reasoning_instructions = '\n`thinking_effort` guides on how much to think in your thinking channel (not including the response channel), supported values include `low` and `high`.\nNow the system is invoked with `thinking_effort=high`.' %}
15
+ {%- elif resolved_reasoning_effort == 'low' %}
16
+ {%- set reasoning_instructions = '\n`thinking_effort` guides on how much to think in your thinking channel (not including the response channel), supported values include `low` and `high`.\nNow the system is invoked with `thinking_effort=low`.' %}
17
+ {%- endif %}
18
+ {%- endif %}
19
+
20
+ {#- Render a string or a list of text blocks. -#}
21
+ {%- macro render_content(content, context_name) %}
22
+ {%- if content is string %}
23
+ {{- content }}
24
+ {%- elif content is none or content is undefined %}
25
+ {{- '' }}
26
+ {%- elif content is iterable and content is not mapping %}
27
+ {%- for block in content %}
28
+ {%- if block.type == 'text' %}
29
+ {{- block.text }}
30
+ {%- else %}
31
+ {{- raise_exception('Unsupported ' ~ context_name ~ ' content block type: ' ~ (block.type | string)) }}
32
+ {%- endif %}
33
+ {%- endfor %}
34
+ {%- else %}
35
+ {{- raise_exception(context_name ~ ' content must be a string or a list of text blocks') }}
36
+ {%- endif %}
37
+ {%- endmacro %}
38
+
39
+ {#- Default system prompt -#}
40
+ {%- set default_system = "You are Mario-html. You are a helpful assistant who generates nice visual content." + reasoning_instructions %}
41
+
42
+ {#- The first message-level system is placed in the initial system block. -#}
43
+ {%- set ns = namespace(initial_system='') %}
44
+ {%- if messages[0].role == "system" %}
45
+ {%- set ns.initial_system = render_content(messages[0].content, 'system') %}
46
+ {%- endif %}
47
+
48
+ {#- System block -#}
49
+ {{- '<|start▁of▁sentence|><|System|>' + '\n' + default_system }}
50
+ {%- if tools %}
51
+ {{- '\n\n' + '## Tools' + '\n' + 'You have access to the following functions:' + '\n' + '<tools>' }}
52
+ {%- for tool in tools %}
53
+ {{- '\n' + (tool.function | tojson(separators=(',', ':'))) }}
54
+ {%- endfor %}
55
+ {{- '\n' + '</tools>' }}
56
+ {%- endif %}
57
+ {%- if ns.initial_system %}
58
+ {{- '\n\n' + ns.initial_system }}
59
+ {%- endif %}
60
+ {{- '<|end▁of▁sentence|>'}}
61
+
62
+ {#- Conversation turns -#}
63
+ {%- for message in messages %}
64
+ {%- if message.role == "system" %}
65
+ {#- The first system message was consumed by the initial block. -#}
66
+ {%- if not loop.first %}
67
+ {{- '<|start▁of▁sentence|><|System|>\n' + render_content(message.content, 'system') + '<|end▁of▁sentence|>' }}
68
+ {%- endif %}
69
+ {%- elif message.role == "user" %}
70
+ {{- '<|start▁of▁sentence|><|User|>' + render_content(message.content, 'user') + '<|end▁of▁sentence|>' }}
71
+ {%- elif message.role == "assistant" %}
72
+ {%- set assistant_content = render_content(message.content, 'assistant') %}
73
+ {%- if message.reasoning_content is defined and message.reasoning_content %}
74
+ {%- set reasoning_content = message.reasoning_content %}
75
+ {%- else %}
76
+ {%- set reasoning_content = '' %}
77
+ {%- endif %}
78
+ {{- '<|start▁of▁sentence|><|Bot|>'}}
79
+ {%- if reasoning_content %}
80
+ {{- '<think>' + reasoning_content + '</think>'}}
81
+ {%- else %}
82
+ {{- '</think>' }}
83
+ {%- endif %}
84
+ {%- if assistant_content %}
85
+ {{- assistant_content }}
86
+ {%- endif %}
87
+ {%- if message.tool_calls is defined and message.tool_calls is not none %}
88
+ {%- for tool_call in message.tool_calls %}
89
+ {%- if tool_call.function.arguments is not mapping %}
90
+ {{- raise_exception('tool_call.function.arguments must be a dictionary; normalize JSON strings before apply_chat_template') }}
91
+ {%- endif %}
92
+ {%- set args = tool_call.function.arguments %}
93
+ {{- '<tool_call>' + tool_call.function.name }}
94
+ {%- for k, v in args.items() %}
95
+ {{- '<arg_key>' ~ k ~ '</arg_key><arg_value>' ~ (v if v is string else v | tojson(separators=(', ', ': '))) ~ '</arg_value>' }}
96
+ {%- endfor %}
97
+ {{- '</tool_call>' }}
98
+ {%- endfor %}
99
+ {%- endif %}
100
+ {{- '<|end▁of▁sentence|>' }}
101
+ {%- elif message.role == "tool" %}
102
+ {%- if loop.previtem is undefined or loop.previtem.role != "tool" %}
103
+ {{- '<���start▁of▁sentence|><|Tool|>' }}
104
+ {%- endif %}
105
+ {{- '<tool_response>' ~ message.content ~ '</tool_response>' }}
106
+ {%- if loop.nextitem is undefined or loop.nextitem.role != "tool" %}
107
+ {{- '<|end▁of▁sentence|>' }}
108
+ {%- endif %}
109
+ {%- else %}
110
+ {{- raise_exception('Unsupported message role: ' ~ message.role) }}
111
+ {%- endif %}
112
+ {%- endfor %}
113
+
114
+ {#- Generation prompt -#}
115
+ {%- if add_generation_prompt %}
116
+ {{- '<|start▁of▁sentence|><|Bot|>' }}
117
+ {%- if enable_thinking is defined and enable_thinking %}
118
+ {{- '<think>' }}
119
+ {%- endif %}
120
+ {%- if enable_thinking is defined and not enable_thinking %}
121
+ {{- '</think>' }}
122
+ {%- endif %}
123
+ {%- endif %}
config.json ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "Spark2_5ForCausalLM"
4
+ ],
5
+ "attention_bias": false,
6
+ "attention_dropout": 0.0,
7
+ "auto_map": {
8
+ "AutoConfig": "configuration_spark.Spark2_5Config",
9
+ "AutoModel": "modeling_spark.Spark2_5Model",
10
+ "AutoModelForCausalLM": "modeling_spark.Spark2_5ForCausalLM"
11
+ },
12
+ "bos_token_id": 0,
13
+ "dtype": "bfloat16",
14
+ "eos_token_id": 1,
15
+ "gate_attn_act_mode": "sigmoid",
16
+ "head_dim": 256,
17
+ "headwise_attn_output_gate": true,
18
+ "hidden_act": "gelu",
19
+ "hidden_size": 2560,
20
+ "initializer_range": 0.01976,
21
+ "intermediate_size": 10240,
22
+ "layer_types": [
23
+ "sliding_attention",
24
+ "sliding_attention",
25
+ "sliding_attention",
26
+ "full_attention",
27
+ "sliding_attention",
28
+ "sliding_attention",
29
+ "sliding_attention",
30
+ "full_attention",
31
+ "sliding_attention",
32
+ "sliding_attention",
33
+ "sliding_attention",
34
+ "full_attention",
35
+ "sliding_attention",
36
+ "sliding_attention",
37
+ "sliding_attention",
38
+ "full_attention",
39
+ "sliding_attention",
40
+ "sliding_attention",
41
+ "sliding_attention",
42
+ "full_attention",
43
+ "sliding_attention",
44
+ "sliding_attention",
45
+ "sliding_attention",
46
+ "full_attention",
47
+ "sliding_attention",
48
+ "sliding_attention",
49
+ "sliding_attention",
50
+ "full_attention",
51
+ "sliding_attention",
52
+ "sliding_attention",
53
+ "sliding_attention",
54
+ "full_attention",
55
+ "sliding_attention",
56
+ "sliding_attention",
57
+ "sliding_attention",
58
+ "full_attention"
59
+ ],
60
+ "max_position_embeddings": 1048576,
61
+ "mlp_bias": false,
62
+ "model_type": "spark2_5",
63
+ "num_attention_heads": 16,
64
+ "num_hidden_layers": 36,
65
+ "num_key_value_heads": 4,
66
+ "pad_token_id": 2,
67
+ "rms_norm_eps": 1e-06,
68
+ "rope_parameters": {
69
+ "full_attention": {
70
+ "partial_rotary_factor": 0.25,
71
+ "rope_theta": 5000000
72
+ },
73
+ "sliding_attention": {
74
+ "partial_rotary_factor": 1.0,
75
+ "rope_theta": 10000
76
+ }
77
+ },
78
+ "sliding_window": 512,
79
+ "tie_word_embeddings": true,
80
+ "transformers_version": "4.57.1",
81
+ "use_cache": true,
82
+ "vocab_size": 131072
83
+ }
configuration.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"task":"text-generation"}
configuration_spark.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2026 The XHToken team and the HuggingFace Inc. team. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ from transformers import PretrainedConfig
17
+
18
+
19
+ class Spark2_5Config(PretrainedConfig):
20
+ model_type = "spark2_5"
21
+ keys_to_ignore_at_inference = ["past_key_values"]
22
+
23
+ base_model_tp_plan = {
24
+ "layers.*.self_attn.q_k_v_proj": "colwise",
25
+ "layers.*.self_attn.g_proj": "colwise",
26
+ "layers.*.self_attn.out_proj": "rowwise",
27
+ "layers.*.mlp.gate_proj": "colwise",
28
+ "layers.*.mlp.up_proj": "colwise",
29
+ "layers.*.mlp.down_proj": "rowwise",
30
+ }
31
+ base_model_pp_plan = {
32
+ "embedding": (["input_ids"], ["inputs_embeds"]),
33
+ "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
34
+ "norm": (["hidden_states"], ["hidden_states"]),
35
+ }
36
+
37
+ def __init__(
38
+ self,
39
+ vocab_size=32000,
40
+ hidden_size=4096,
41
+ intermediate_size=11008,
42
+ num_hidden_layers=32,
43
+ num_attention_heads=32,
44
+ num_key_value_heads=None,
45
+ hidden_act="gelu",
46
+ max_position_embeddings=2048,
47
+ initializer_range=0.02,
48
+ rms_norm_eps=1e-6,
49
+ use_cache=True,
50
+ pad_token_id=None,
51
+ bos_token_id=1,
52
+ eos_token_id=2,
53
+ tie_word_embeddings=False,
54
+ rope_parameters=None,
55
+ attention_bias=False,
56
+ attention_dropout=0.0,
57
+ mlp_bias=False,
58
+ head_dim=None,
59
+ headwise_attn_output_gate=False,
60
+ gate_attn_act_mode="sigmoid",
61
+ sliding_window=None,
62
+ layer_types=None,
63
+ **kwargs,
64
+ ):
65
+ self.vocab_size = vocab_size
66
+ self.max_position_embeddings = max_position_embeddings
67
+ self.hidden_size = hidden_size
68
+ self.intermediate_size = intermediate_size
69
+ self.num_hidden_layers = num_hidden_layers
70
+ self.num_attention_heads = num_attention_heads
71
+
72
+ if num_key_value_heads is None:
73
+ num_key_value_heads = num_attention_heads
74
+ if num_attention_heads % num_key_value_heads != 0:
75
+ raise ValueError(
76
+ f"num_attention_heads ({num_attention_heads}) must be divisible by num_key_value_heads ({num_key_value_heads})"
77
+ )
78
+ self.num_key_value_heads = num_key_value_heads
79
+
80
+ self.hidden_act = hidden_act
81
+ self.initializer_range = initializer_range
82
+ self.rms_norm_eps = rms_norm_eps
83
+ self.use_cache = use_cache
84
+ self.attention_bias = attention_bias
85
+ self.attention_dropout = attention_dropout
86
+ self.mlp_bias = mlp_bias
87
+ self.head_dim = head_dim if head_dim is not None else self.hidden_size // self.num_attention_heads
88
+ self.headwise_attn_output_gate = headwise_attn_output_gate
89
+ self.gate_attn_act_mode = gate_attn_act_mode
90
+ self.sliding_window = sliding_window
91
+ self.rope_parameters = rope_parameters
92
+
93
+ if layer_types is None:
94
+ layer_types = ["full_attention"] * num_hidden_layers
95
+ if len(layer_types) != num_hidden_layers:
96
+ raise ValueError(
97
+ f"layer_types length ({len(layer_types)}) must match num_hidden_layers ({num_hidden_layers})"
98
+ )
99
+ self.layer_types = layer_types
100
+
101
+ super().__init__(
102
+ pad_token_id=pad_token_id,
103
+ bos_token_id=bos_token_id,
104
+ eos_token_id=eos_token_id,
105
+ tie_word_embeddings=tie_word_embeddings,
106
+ **kwargs,
107
+ )
108
+
109
+ def get_rope_theta(self, layer_type):
110
+ params = self.rope_parameters.get(layer_type, {})
111
+ return params.get("rope_theta", 10000)
112
+
113
+ def get_partial_rotary_factor(self, layer_type):
114
+ params = self.rope_parameters.get(layer_type, {})
115
+ return params.get("partial_rotary_factor", 1.0)
116
+
117
+
118
+ __all__ = ["Spark2_5Config"]
generation_config.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token_id": 0,
3
+ "eos_token_id": 1,
4
+ "pad_token_id": 2,
5
+ "max_tokens": 1048576,
6
+ "temperature": 0.7,
7
+ "top_p": 0.8,
8
+ "top_k": 40,
9
+ "repetition_penalty": 1.05,
10
+ "presence_penalty": 0.0,
11
+ "frequency_penalty": 0.0,
12
+ "do_sample": true,
13
+ "transformers_version": "4.57.1",
14
+ "max_new_tokens": 8192
15
+ }
merges.txt ADDED
The diff for this file is too large to render. See raw diff
 
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4eb6e934f1fb51eb6ce6daf1589b4c1bed81d58365abd7fe55f95ce633c7950b
3
+ size 8224192576
modeling_spark.py ADDED
@@ -0,0 +1,483 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+
3
+ import torch
4
+ import torch.nn.functional as F
5
+ from torch import nn
6
+ from transformers.activations import ACT2FN
7
+ from transformers.cache_utils import Cache, DynamicCache
8
+ from transformers.generation import GenerationMixin
9
+ from transformers.masking_utils import create_causal_mask, create_sliding_window_causal_mask
10
+ from transformers.modeling_outputs import (
11
+ BaseModelOutputWithPast,
12
+ CausalLMOutputWithPast,
13
+ )
14
+ from transformers.modeling_utils import PreTrainedModel
15
+ from transformers.processing_utils import Unpack
16
+ from transformers.pytorch_utils import ALL_LAYERNORM_LAYERS
17
+ from transformers.utils import TransformersKwargs, can_return_tuple, logging
18
+
19
+ from .configuration_spark import Spark2_5Config
20
+
21
+ logger = logging.get_logger(__name__)
22
+
23
+ _CONFIG_FOR_DOC = "Spark2_5Config"
24
+
25
+ def rotate_half(x):
26
+ x1 = x[..., : x.shape[-1] // 2]
27
+ x2 = x[..., x.shape[-1] // 2 :]
28
+ return torch.cat((-x2, x1), dim=-1)
29
+
30
+
31
+ def compute_rope_cos_sin(positions, head_dim, rope_theta, partial_rotary_factor=1.0, device="cpu"):
32
+ rope_head_dim = int(head_dim * partial_rotary_factor)
33
+ inv_freq = 1.0 / (rope_theta ** (torch.arange(0, rope_head_dim, 2, dtype=torch.int64).to(device="cpu", dtype=torch.float) / rope_head_dim))
34
+ inv_freq = inv_freq.to(device)
35
+ t = positions.to(device=device, dtype=torch.float32)
36
+ freqs = torch.outer(t, inv_freq)
37
+ freqs = torch.cat([freqs, freqs], dim=-1)
38
+ cos = freqs.cos()
39
+ sin = freqs.sin()
40
+ return cos, sin
41
+
42
+
43
+ def apply_rotary_pos_emb(x, cos, sin):
44
+ rope_head_dim = cos.shape[-1]
45
+ x_f32 = x.float()
46
+ if x_f32.shape[-1] > rope_head_dim:
47
+ x_rot = x_f32[..., :rope_head_dim]
48
+ x_pass = x_f32[..., rope_head_dim:]
49
+ c = cos.unsqueeze(0).unsqueeze(0)
50
+ s = sin.unsqueeze(0).unsqueeze(0)
51
+ x_rot = x_rot * c + rotate_half(x_rot) * s
52
+ result = torch.cat([x_rot, x_pass], dim=-1)
53
+ else:
54
+ c = cos.unsqueeze(0).unsqueeze(0)
55
+ s = sin.unsqueeze(0).unsqueeze(0)
56
+ result = x_f32 * c + rotate_half(x_f32) * s
57
+ return result.to(x.dtype)
58
+
59
+
60
+ def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
61
+ batch, num_key_value_heads, slen, head_dim = hidden_states.shape
62
+ if n_rep == 1:
63
+ return hidden_states
64
+ hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim)
65
+ return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
66
+
67
+
68
+ def eager_attention_forward(
69
+ module: nn.Module,
70
+ query: torch.Tensor,
71
+ key: torch.Tensor,
72
+ value: torch.Tensor,
73
+ attention_mask: torch.Tensor | None = None,
74
+ scaling: float | None = None,
75
+ dropout: float = 0.0,
76
+ **kwargs: Unpack[TransformersKwargs],
77
+ ):
78
+ key = repeat_kv(key, module.num_key_value_groups)
79
+ value = repeat_kv(value, module.num_key_value_groups)
80
+
81
+ if scaling is None:
82
+ scaling = 1.0 / math.sqrt(query.shape[-1])
83
+
84
+ attn_weights = torch.matmul(query, key.transpose(2, 3)) * scaling
85
+ if attention_mask is not None:
86
+ causal_mask = attention_mask[:, :, :, : key.shape[-2]]
87
+ attn_weights = attn_weights + causal_mask
88
+
89
+ attn_weights = attn_weights - attn_weights.max(dim=-1, keepdim=True).values
90
+ attn_weights = F.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query.dtype)
91
+ attn_weights = nn.functional.dropout(attn_weights, p=dropout, training=module.training)
92
+ attn_output = torch.matmul(attn_weights, value)
93
+ return attn_output, attn_weights
94
+
95
+
96
+ class Spark2_5RMSNorm(nn.Module):
97
+ def __init__(self, hidden_size, eps=1e-6):
98
+ super().__init__()
99
+ self.weight = nn.Parameter(torch.ones(hidden_size))
100
+ self.variance_epsilon = eps
101
+
102
+ def forward(self, hidden_states):
103
+ input_dtype = hidden_states.dtype
104
+ hidden_states = hidden_states.to(torch.float32)
105
+ variance = hidden_states.pow(2).mean(-1, keepdim=True)
106
+ hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
107
+ return (self.weight.float() * hidden_states).to(input_dtype)
108
+
109
+ def extra_repr(self):
110
+ return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}"
111
+
112
+
113
+ ALL_LAYERNORM_LAYERS.append(Spark2_5RMSNorm)
114
+
115
+
116
+ class Spark2_5MLP(nn.Module):
117
+ def __init__(self, config):
118
+ super().__init__()
119
+ self.config = config
120
+ self.hidden_size = config.hidden_size
121
+ self.intermediate_size = config.intermediate_size
122
+ self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=config.mlp_bias)
123
+ self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=config.mlp_bias)
124
+ self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=config.mlp_bias)
125
+
126
+ if config.hidden_act != "gelu":
127
+ raise ValueError(f"只支持hidden_act='gelu',当前传入:{config.hidden_act}")
128
+
129
+ self.act_fn = ACT2FN[config.hidden_act]
130
+
131
+ def forward(self, x):
132
+ return self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
133
+
134
+
135
+ class Spark2_5Attention(nn.Module):
136
+ def __init__(self, config: Spark2_5Config, layer_idx: int | None = None):
137
+ super().__init__()
138
+ self.config = config
139
+ self.layer_idx = layer_idx
140
+ self.attention_dropout = config.attention_dropout
141
+ self.hidden_size = config.hidden_size
142
+ self.num_heads = config.num_attention_heads
143
+ self.head_dim = config.head_dim
144
+ self.num_key_value_heads = config.num_key_value_heads
145
+ self.num_key_value_groups = self.num_heads // self.num_key_value_heads
146
+ self.scaling = 1.0 / math.sqrt(self.head_dim)
147
+ self.headwise_attn_output_gate = config.headwise_attn_output_gate
148
+ self.gate_attn_act_mode = config.gate_attn_act_mode
149
+ self.q_dim = self.num_heads * self.head_dim
150
+ self.kv_dim = self.num_key_value_heads * self.head_dim
151
+
152
+ qkv_out_dim = self.q_dim + 2 * self.kv_dim
153
+ self.q_k_v_proj = nn.Linear(self.hidden_size, qkv_out_dim, bias=config.attention_bias)
154
+ self.g_proj = nn.Linear(self.hidden_size, self.num_heads, bias=config.attention_bias) if self.headwise_attn_output_gate else None
155
+ self.out_proj = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=config.attention_bias)
156
+ self.sliding_window = None
157
+
158
+ def forward(
159
+ self,
160
+ hidden_states: torch.Tensor,
161
+ position_embeddings: tuple[torch.Tensor, torch.Tensor],
162
+ attention_mask: torch.Tensor | None = None,
163
+ past_key_values: Cache | None = None,
164
+ cache_position: torch.LongTensor | None = None,
165
+ **kwargs: Unpack[TransformersKwargs],
166
+ ) -> tuple[torch.Tensor, torch.Tensor]:
167
+ input_shape = hidden_states.shape[:-1]
168
+ bsz, seq_len = input_shape
169
+
170
+ qkv = self.q_k_v_proj(hidden_states)
171
+ q = qkv[..., :self.q_dim]
172
+ k = qkv[..., self.q_dim:self.q_dim + self.kv_dim]
173
+ v = qkv[..., self.q_dim + self.kv_dim:]
174
+ gate_score = self.g_proj(hidden_states) if self.g_proj is not None else None
175
+
176
+ q = q.view(bsz, seq_len, self.num_heads, self.head_dim).transpose(1, 2)
177
+ k = k.view(bsz, seq_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
178
+ v = v.view(bsz, seq_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
179
+ if gate_score is not None:
180
+ gate_score = gate_score.view(bsz, seq_len, self.num_heads, 1).transpose(1, 2)
181
+
182
+ cos, sin = position_embeddings
183
+ q = apply_rotary_pos_emb(q, cos, sin)
184
+ k = apply_rotary_pos_emb(k, cos, sin)
185
+
186
+
187
+ if past_key_values is not None:
188
+ cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position}
189
+ k, v = past_key_values.update(k, v, self.layer_idx, cache_kwargs)
190
+
191
+ attn_output, attn_weights = eager_attention_forward(
192
+ self, q, k, v,
193
+ attention_mask=attention_mask,
194
+ scaling=self.scaling,
195
+ dropout=self.attention_dropout if self.training else 0.0,
196
+ )
197
+
198
+ if gate_score is not None:
199
+ if self.gate_attn_act_mode == "sigmoid":
200
+ gate = torch.sigmoid(gate_score.float())
201
+ elif self.gate_attn_act_mode == "silu":
202
+ gate = F.silu(gate_score.float())
203
+ else:
204
+ raise ValueError(f"Unsupported gate_attn_act_mode: {self.gate_attn_act_mode}")
205
+ gate = gate.to(attn_output.dtype)
206
+ attn_output = attn_output * gate
207
+
208
+ attn_output = attn_output.transpose(1, 2).contiguous().view(bsz, seq_len, -1)
209
+ attn_output = self.out_proj(attn_output)
210
+
211
+ return attn_output, attn_weights
212
+
213
+
214
+ class Spark2_5DecoderLayer(nn.Module):
215
+ def __init__(self, config: Spark2_5Config, layer_idx: int):
216
+ super().__init__()
217
+ self.hidden_size = config.hidden_size
218
+
219
+ self.self_attn = Spark2_5Attention(config=config, layer_idx=layer_idx)
220
+ self.mlp = Spark2_5MLP(config)
221
+ self.input_layernorm = Spark2_5RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
222
+ self.post_attention_layernorm = Spark2_5RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
223
+
224
+ self.layer_type = config.layer_types[layer_idx] if layer_idx < len(config.layer_types) else "full_attention"
225
+ if self.layer_type == "sliding_attention" and config.sliding_window is not None:
226
+ self.self_attn.sliding_window = config.sliding_window
227
+ else:
228
+ self.self_attn.sliding_window = None
229
+ self.self_attn.partial_rotary_factor = config.get_partial_rotary_factor(self.layer_type)
230
+
231
+ def forward(
232
+ self,
233
+ hidden_states: torch.Tensor,
234
+ position_embeddings: tuple[torch.Tensor, torch.Tensor],
235
+ attention_mask: torch.Tensor | None = None,
236
+ past_key_values: Cache | None = None,
237
+ cache_position: torch.LongTensor | None = None,
238
+ position_ids: torch.LongTensor | None = None,
239
+ **kwargs: Unpack[TransformersKwargs]
240
+ ) -> torch.Tensor:
241
+
242
+ residual = hidden_states
243
+ hidden_states = self.input_layernorm(hidden_states)
244
+ hidden_states = hidden_states.to(self.mlp.gate_proj.weight.dtype)
245
+
246
+ hidden_states, _ = self.self_attn(
247
+ hidden_states=hidden_states,
248
+ position_embeddings=position_embeddings,
249
+ attention_mask=attention_mask,
250
+ past_key_values=past_key_values,
251
+ cache_position=cache_position,
252
+ position_ids=position_ids,
253
+ )
254
+ hidden_states = residual + hidden_states
255
+
256
+ residual = hidden_states
257
+ hidden_states = self.post_attention_layernorm(hidden_states)
258
+ hidden_states = hidden_states.to(self.mlp.gate_proj.weight.dtype)
259
+
260
+
261
+ hidden_states = self.mlp(hidden_states)
262
+ hidden_states = residual + hidden_states
263
+
264
+ return hidden_states
265
+
266
+
267
+ class Spark2_5PreTrainedModel(PreTrainedModel):
268
+ config_class = Spark2_5Config
269
+ base_model_prefix = "model"
270
+ supports_gradient_checkpointing = True
271
+ _no_split_modules = ["Spark2_5DecoderLayer"] # noqa: RUF012
272
+ _skip_keys_device_placement = ["past_key_values"] # noqa: RUF012
273
+
274
+ def _init_weights(self, module):
275
+ std = self.config.initializer_range
276
+ if isinstance(module, nn.Linear):
277
+ module.weight.data.normal_(mean=0.0, std=std)
278
+ if module.bias is not None:
279
+ module.bias.data.zero_()
280
+ elif isinstance(module, nn.Embedding):
281
+ module.weight.data.normal_(mean=0.0, std=std)
282
+ if module.padding_idx is not None:
283
+ module.weight.data[module.padding_idx].zero_()
284
+
285
+
286
+ class Spark2_5Model(Spark2_5PreTrainedModel):
287
+ def __init__(self, config: Spark2_5Config):
288
+ super().__init__(config)
289
+ self.padding_idx = config.pad_token_id
290
+ self.vocab_size = config.vocab_size
291
+
292
+ self.embedding = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
293
+ self.layers = nn.ModuleList(
294
+ [Spark2_5DecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
295
+ )
296
+ self.norm = Spark2_5RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
297
+ self.gradient_checkpointing = False
298
+ self.has_sliding_layers = "sliding_attention" in config.layer_types
299
+
300
+ self.post_init()
301
+
302
+ def get_input_embeddings(self):
303
+ return self.embedding
304
+
305
+ def set_input_embeddings(self, value):
306
+ self.embedding = value
307
+ def forward(
308
+ self,
309
+ input_ids: torch.LongTensor = None,
310
+ attention_mask: torch.Tensor | None = None,
311
+ position_ids: torch.LongTensor | None = None,
312
+ past_key_values: Cache | list[torch.FloatTensor] | None = None,
313
+ inputs_embeds: torch.FloatTensor | None = None,
314
+ use_cache: bool | None = None,
315
+ cache_position: torch.LongTensor | None = None,
316
+ token_type_ids: torch.LongTensor | None = None,
317
+ **kwargs: Unpack[TransformersKwargs],
318
+ ) -> BaseModelOutputWithPast:
319
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
320
+ if (input_ids is None) ^ (inputs_embeds is not None):
321
+ raise ValueError(
322
+ "You cannot specify both input_ids and inputs_embeds at the same time, and must specify either one"
323
+ )
324
+
325
+ if self.gradient_checkpointing and self.training and use_cache:
326
+ logger.warning_once(
327
+ "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`."
328
+ )
329
+ use_cache = False
330
+
331
+ if inputs_embeds is None:
332
+ inputs_embeds = self.embedding(input_ids)
333
+
334
+ if use_cache and past_key_values is None:
335
+ past_key_values = DynamicCache(config=self.config)
336
+
337
+ if cache_position is None:
338
+ past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0
339
+ cache_position = torch.arange(
340
+ past_seen_tokens, past_seen_tokens + inputs_embeds.shape[1], device=inputs_embeds.device
341
+ )
342
+
343
+ if position_ids is None:
344
+ position_ids = cache_position.unsqueeze(0)
345
+
346
+ if not isinstance(attention_mask, dict):
347
+ mask_kwargs = {
348
+ "config": self.config,
349
+ "input_embeds": inputs_embeds,
350
+ "attention_mask": attention_mask,
351
+ "cache_position": cache_position,
352
+ "past_key_values": past_key_values,
353
+ "position_ids": position_ids,
354
+ }
355
+ causal_mask_mapping = {
356
+ "full_attention": create_causal_mask(**mask_kwargs),
357
+ }
358
+ if self.has_sliding_layers:
359
+ causal_mask_mapping["sliding_attention"] = create_sliding_window_causal_mask(**mask_kwargs)
360
+ else:
361
+ causal_mask_mapping = attention_mask
362
+
363
+ hidden_states = inputs_embeds.float()
364
+
365
+ device = hidden_states.device
366
+ dtype = self.embedding.weight.dtype
367
+
368
+ head_dim = self.config.head_dim
369
+ rope_cache = {}
370
+ for lt in set(self.config.layer_types):
371
+ rope_theta = self.config.get_rope_theta(lt)
372
+ prf = self.config.get_partial_rotary_factor(lt)
373
+ cos, sin = compute_rope_cos_sin(cache_position, head_dim, rope_theta, partial_rotary_factor=prf, device=device)
374
+ rope_cache[lt] = (cos, sin)
375
+
376
+ for decoder_layer in self.layers:
377
+ layer_type = decoder_layer.layer_type
378
+ position_embeddings = rope_cache.get(layer_type, rope_cache.get("full_attention"))
379
+ layer_attention_mask = causal_mask_mapping.get(layer_type, causal_mask_mapping.get("full_attention"))
380
+
381
+ if self.gradient_checkpointing and self.training:
382
+ layer_outputs = self._gradient_checkpointing_func(
383
+ decoder_layer.__call__,
384
+ hidden_states,
385
+ position_embeddings,
386
+ layer_attention_mask,
387
+ )
388
+ hidden_states = layer_outputs[0] if isinstance(layer_outputs, tuple) else layer_outputs
389
+ else:
390
+ hidden_states = decoder_layer(
391
+ hidden_states,
392
+ position_embeddings=position_embeddings,
393
+ attention_mask=layer_attention_mask,
394
+ past_key_values=past_key_values,
395
+ cache_position=cache_position,
396
+ position_ids=position_ids,
397
+ )
398
+
399
+ hidden_states = self.norm(hidden_states)
400
+ hidden_states = hidden_states.to(dtype)
401
+
402
+ return BaseModelOutputWithPast(
403
+ last_hidden_state=hidden_states,
404
+ past_key_values=past_key_values if use_cache else None,
405
+ )
406
+
407
+ class Spark2_5ForCausalLM(Spark2_5PreTrainedModel, GenerationMixin):
408
+ _tied_weights_keys = ["lm_head.weight"] # noqa: RUF012
409
+
410
+ def __init__(self, config):
411
+ super().__init__(config)
412
+ self.model = Spark2_5Model(config)
413
+ self.vocab_size = config.vocab_size
414
+ self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
415
+ self.post_init()
416
+
417
+ def get_input_embeddings(self):
418
+ return self.model.embedding
419
+
420
+ def set_input_embeddings(self, value):
421
+ self.model.embedding = value
422
+
423
+ def get_output_embeddings(self):
424
+ return self.lm_head
425
+
426
+ def set_output_embeddings(self, new_embeddings):
427
+ self.lm_head = new_embeddings
428
+
429
+ def set_decoder(self, decoder):
430
+ self.model = decoder
431
+
432
+ def get_decoder(self):
433
+ return self.model
434
+
435
+ @can_return_tuple
436
+ def forward(
437
+ self,
438
+ input_ids: torch.LongTensor = None,
439
+ attention_mask: torch.Tensor | None = None,
440
+ position_ids: torch.LongTensor | None = None,
441
+ past_key_values: Cache | list[torch.FloatTensor] | None = None,
442
+ inputs_embeds: torch.FloatTensor | None = None,
443
+ labels: torch.LongTensor | None = None,
444
+ use_cache: bool | None = None,
445
+ cache_position: torch.LongTensor | None = None,
446
+ logits_to_keep: int = 0,
447
+ token_type_ids: torch.LongTensor | None = None,
448
+ **kwargs: Unpack[TransformersKwargs],
449
+ ) -> CausalLMOutputWithPast:
450
+ outputs: BaseModelOutputWithPast = self.model(
451
+ input_ids=input_ids,
452
+ attention_mask=attention_mask,
453
+ position_ids=position_ids,
454
+ past_key_values=past_key_values,
455
+ inputs_embeds=inputs_embeds,
456
+ use_cache=use_cache,
457
+ cache_position=cache_position,
458
+ )
459
+
460
+ hidden_states = outputs.last_hidden_state
461
+ slice_indices = slice(-logits_to_keep, None) if isinstance(logits_to_keep, int) else logits_to_keep
462
+ hidden_states = hidden_states[:, slice_indices, :]
463
+
464
+ if self.config.tie_word_embeddings:
465
+ embed_weight = self.model.embedding.weight
466
+ logits = F.linear(hidden_states, embed_weight)
467
+ else:
468
+ logits = self.lm_head(hidden_states)
469
+
470
+ loss = None
471
+ if labels is not None:
472
+ loss = self.loss_function(logits=logits, labels=labels, vocab_size=self.config.vocab_size, **kwargs)
473
+
474
+ return CausalLMOutputWithPast(
475
+ loss=loss,
476
+ logits=logits,
477
+ past_key_values=outputs.past_key_values,
478
+ hidden_states=outputs.hidden_states,
479
+ attentions=outputs.attentions,
480
+ )
481
+
482
+
483
+ __all__ = ["Spark2_5Config", "Spark2_5ForCausalLM", "Spark2_5Model"]
special_tokens_map.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": "<|start▁of▁sentence|>",
3
+ "eos_token": "<|end▁of▁sentence|>",
4
+ "unk_token": "<unk>",
5
+ "pad_token": "<|▁pad▁|>"
6
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "backend": "tokenizers",
3
+ "bos_token": "<|start▁of▁sentence|>",
4
+ "clean_up_tokenization_spaces": false,
5
+ "eos_token": "<|end▁of▁sentence|>",
6
+ "is_local": true,
7
+ "legacy": true,
8
+ "local_files_only": false,
9
+ "model_max_length": 131072,
10
+ "pad_token": "<|▁pad▁|>",
11
+ "sp_model_kwargs": {},
12
+ "tokenizer_class": "TokenizersBackend",
13
+ "unk_token": "<unk>"
14
+ }
vocab.json ADDED
The diff for this file is too large to render. See raw diff
 
webui/config.json ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "backend_mode": "api",
3
+ "api_url": "http://127.0.0.1:18080",
4
+ "model_path": "/mnt/sdb/models/Spark-x2.6-4B-HTML/Spark-x2.6-4B-HTML-Q8_0.gguf",
5
+ "model_alias": "Spark-x2.6-4B-HTML",
6
+ "context_size": 131072,
7
+ "gpu_layers": 99,
8
+ "do_sample": true,
9
+ "temperature": 0.7,
10
+ "top_p": 0.8,
11
+ "top_k": 40,
12
+ "min_p": 0.05,
13
+ "presence_penalty": 0.0,
14
+ "repetition_penalty": 1.05,
15
+ "max_tokens": 8192,
16
+ "enable_thinking": true,
17
+ "theme_preset": "notion",
18
+ "custom_css": "/* Notion Minimal */\n.html-canvas {\n border-radius: 4px !important;\n border: 1px solid #37352f !important;\n box-shadow: none !important;\n background: #ffffff !important;\n}\n.card {\n border-radius: 4px !important;\n border: 1px solid #e3e2e0 !important;\n box-shadow: none !important;\n}\n.summary-card {\n border-radius: 4px !important;\n border-left-color: #37352f !important;\n}\n.callout {\n border-radius: 4px !important;\n border-left-width: 4px !important;\n}\n.badge {\n border-radius: 3px !important;\n font-weight: 500 !important;\n}",
19
+ "system_prompt": "You are Mario-html. You are a helpful assistant who generates nice visual content.",
20
+ "reasoning_effort": "low",
21
+ "frequency_penalty": 0.0
22
+ }
webui/requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi>=0.100.0
2
+ uvicorn>=0.22.0
3
+ httpx>=0.24.0
4
+ pydantic>=2.0.0
webui/run_webui.sh ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ # ---------------------------------------------------------------------------
3
+ # run_webui.sh — WebUI frontend/API for this folder (self-contained)
4
+ #
5
+ # ./run_webui.sh # config.json decides; env vars override
6
+ # WEBUI_PORT=8795 ./run_webui.sh
7
+ #
8
+ # Precedence: explicitly-set env > config.json > built-in defaults.
9
+ # API_URL / MODEL_ALIAS are NOT defaulted here on purpose — config.json is
10
+ # the single source of truth so editing it is enough.
11
+ # ---------------------------------------------------------------------------
12
+ set -euo pipefail
13
+
14
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
15
+ cd "$SCRIPT_DIR"
16
+
17
+ LOG="${LOG_FILE:-$SCRIPT_DIR/webui.log}"
18
+
19
+ export PYTHONUNBUFFERED=1
20
+ export TMPDIR="${TMPDIR:-/mnt/sdb/tmp}"
21
+ export HF_HUB_OFFLINE="${HF_HUB_OFFLINE:-1}"
22
+ export TRANSFORMERS_OFFLINE="${TRANSFORMERS_OFFLINE:-1}"
23
+
24
+ # ---- interpreter: pick one that actually has fastapi+uvicorn ---------------
25
+ PY="${WEBUI_PY:-}"
26
+ if [ -z "$PY" ]; then
27
+ for cand in "$SCRIPT_DIR/../venv/bin/python" \
28
+ /mnt/sdb/data/conda-env/envs/unsloth-vlm/bin/python \
29
+ /mnt/sdb/data/conda-env/envs/StructBERT/bin/python \
30
+ "$(command -v python3)"; do
31
+ [ -x "$cand" ] || continue
32
+ if "$cand" -c 'import fastapi, uvicorn, httpx' 2>/dev/null; then PY="$cand"; break; fi
33
+ done
34
+ fi
35
+ if [ -z "$PY" ]; then
36
+ echo "ERROR: no interpreter with fastapi/uvicorn found. Set WEBUI_PY=/path/to/python" >&2
37
+ echo " or: pip install -r $SCRIPT_DIR/requirements.txt" >&2
38
+ exit 1
39
+ fi
40
+
41
+ PORT="${WEBUI_PORT:-8798}"
42
+
43
+ # ---- backend health hint (advisory only, never blocks startup) -------------
44
+ api_from_config="$(/mnt/sdb/data/conda-env/envs/unsloth-vlm/bin/python - "$SCRIPT_DIR/config.json" <<'EOF' 2>/dev/null || true
45
+ import json, sys
46
+ try:
47
+ print(json.load(open(sys.argv[1])).get("api_url", ""))
48
+ except Exception:
49
+ pass
50
+ EOF
51
+ )"
52
+ api_check="${API_URL:-${api_from_config:-http://127.0.0.1:18085}}"
53
+ if ! curl -sSf -m 2 "$api_check/health" >/dev/null 2>&1; then
54
+ echo "WARN: backend $api_check not responding — start the backend first (./start.sh)" >&2
55
+ fi
56
+
57
+ echo "[$(date '+%F %T')] WebUI http://0.0.0.0:$PORT -> backend $api_check"
58
+ exec "$PY" server.py --port "$PORT" > "$LOG" 2>&1
webui/server.py ADDED
@@ -0,0 +1,442 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Minimalist Qwen-HTML WebUI Server (:8791)
5
+ - English-only minimal interface
6
+ - Backend options: Llama.cpp API Server or Direct Model Path
7
+ - Real-time streaming for Reasoning Trace (<think>) & Rendered HTML (<div class="ai-view">)
8
+ """
9
+
10
+ import os, re
11
+ import json
12
+ import time
13
+ import argparse
14
+ import subprocess
15
+ import threading
16
+ from pathlib import Path
17
+ from typing import List, Dict, Optional, Union, Any
18
+ import httpx
19
+ import uvicorn
20
+ from fastapi import FastAPI, Request
21
+ from fastapi.responses import FileResponse, JSONResponse, StreamingResponse
22
+ from fastapi.staticfiles import StaticFiles
23
+ from pydantic import BaseModel
24
+
25
+ # --- Self-contained layout: everything resolved relative to this file, env-overridable ---
26
+ BASE_DIR = Path(__file__).resolve().parent
27
+ STATIC_DIR = str(BASE_DIR / "static")
28
+ CONFIG_FILE = str(BASE_DIR / "config.json")
29
+ HISTORY_FILE = str(BASE_DIR / "chat_history.jsonl")
30
+
31
+ PORT = int(os.environ.get("WEBUI_PORT", "8791"))
32
+ DEFAULT_API_URL = os.environ.get("API_URL", "http://127.0.0.1:18085")
33
+ DEFAULT_MODEL_PATH = os.environ.get("MODEL_PATH", str(BASE_DIR.parent))
34
+ DEFAULT_MODEL_ALIAS = os.environ.get("MODEL_ALIAS", "qwen-3.8-27b")
35
+
36
+ # Direct (llama.cpp GGUF) backend — only used by POST /api/model/load
37
+ LLAMA_SERVER_BIN = os.environ.get(
38
+ "LLAMA_SERVER_BIN", "/mnt/sdb/ubuntu/llama.cpp-clef/build/bin/llama-server"
39
+ )
40
+ LLAMA_SCOPE = os.environ.get("LLAMA_SCOPE", "llama-server-q8")
41
+ LLAMA_BACKEND_PORT = int(os.environ.get("LLAMA_BACKEND_PORT", "18085"))
42
+
43
+ DEFAULT_CONFIG = {
44
+ "backend_mode": "api", # 'api' = talk to vLLM/OpenAI backend; 'direct' = managed llama.cpp server
45
+ "api_url": DEFAULT_API_URL,
46
+ "model_path": DEFAULT_MODEL_PATH,
47
+ "model_alias": DEFAULT_MODEL_ALIAS,
48
+ "context_size": 81920,
49
+ "gpu_layers": 99,
50
+ "do_sample": True,
51
+ "temperature": 1.0,
52
+ "top_p": 0.95,
53
+ "top_k": 20,
54
+ "min_p": 0.0,
55
+ "presence_penalty": 1.5,
56
+ "repetition_penalty": 1.0,
57
+ "max_tokens": 8192,
58
+ "enable_thinking": True,
59
+ "theme_preset": "default",
60
+ "custom_css": "",
61
+ "system_prompt": "",
62
+ }
63
+
64
+ config_lock = threading.Lock()
65
+
66
+ def load_config() -> dict:
67
+ cfg = dict(DEFAULT_CONFIG)
68
+ if os.path.exists(CONFIG_FILE):
69
+ try:
70
+ with open(CONFIG_FILE, "r", encoding="utf-8") as f:
71
+ cfg.update(json.load(f))
72
+ except Exception:
73
+ pass
74
+ # Env (set by run_webui.sh, resolved relative to this folder) wins over a
75
+ # stale config.json so a copied folder never points at the old model.
76
+ for key, env in (("api_url", "API_URL"),
77
+ ("model_path", "MODEL_PATH"),
78
+ ("model_alias", "MODEL_ALIAS")):
79
+ if os.environ.get(env):
80
+ cfg[key] = os.environ[env]
81
+ return cfg
82
+
83
+ def save_config(cfg: dict):
84
+ with config_lock:
85
+ with open(CONFIG_FILE, "w", encoding="utf-8") as f:
86
+ json.dump(cfg, f, indent=2, ensure_ascii=False)
87
+
88
+ current_config = load_config()
89
+
90
+ history_lock = threading.Lock()
91
+
92
+ def log_chat(record: dict):
93
+ with history_lock:
94
+ with open(HISTORY_FILE, "a", encoding="utf-8") as f:
95
+ f.write(json.dumps(record, ensure_ascii=False) + "\n")
96
+
97
+ app = FastAPI(title="Qwen-HTML WebUI")
98
+ app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
99
+
100
+ @app.get("/")
101
+ async def root():
102
+ return FileResponse(os.path.join(STATIC_DIR, "index.html"))
103
+
104
+ @app.get("/api/config")
105
+ async def get_config_endpoint():
106
+ return current_config
107
+
108
+ class UpdateConfigRequest(BaseModel):
109
+ backend_mode: Optional[str] = None
110
+ api_url: Optional[str] = None
111
+ model_path: Optional[str] = None
112
+ context_size: Optional[int] = None
113
+ gpu_layers: Optional[int] = None
114
+ do_sample: Optional[bool] = None
115
+ temperature: Optional[float] = None
116
+ top_p: Optional[float] = None
117
+ top_k: Optional[int] = None
118
+ min_p: Optional[float] = None
119
+ presence_penalty: Optional[float] = None
120
+ repetition_penalty: Optional[float] = None
121
+ max_tokens: Optional[int] = None
122
+ enable_thinking: Optional[bool] = None
123
+ theme_preset: Optional[str] = None
124
+ custom_css: Optional[str] = None
125
+ system_prompt: Optional[str] = None
126
+
127
+ @app.post("/api/config")
128
+ async def update_config_endpoint(req: UpdateConfigRequest):
129
+ updates = req.dict(exclude_unset=True)
130
+ current_config.update(updates)
131
+ save_config(current_config)
132
+ return {"status": "ok", "config": current_config}
133
+
134
+ @app.get("/api/status")
135
+ async def status_endpoint():
136
+ api_url = current_config.get("api_url", DEFAULT_API_URL)
137
+ is_alive = False
138
+ model_info = "Unknown"
139
+ latency_ms = None
140
+
141
+ t0 = time.time()
142
+ try:
143
+ async with httpx.AsyncClient(timeout=2.0) as client:
144
+ resp = await client.get(f"{api_url}/health")
145
+ if resp.status_code == 200:
146
+ is_alive = True
147
+ latency_ms = round((time.time() - t0) * 1000, 1)
148
+ m_resp = await client.get(f"{api_url}/v1/models")
149
+ if m_resp.status_code == 200:
150
+ models = m_resp.json().get("data", [])
151
+ if models:
152
+ model_info = models[0].get("id", DEFAULT_MODEL_ALIAS)
153
+ except Exception:
154
+ pass
155
+
156
+ return {
157
+ "status": "ready" if is_alive else "disconnected",
158
+ "backend_mode": current_config.get("backend_mode", "api"),
159
+ "api_url": api_url,
160
+ "model_path": current_config.get("model_path"),
161
+ "model_info": model_info,
162
+ "latency_ms": latency_ms,
163
+ "enable_thinking": current_config.get("enable_thinking", True)
164
+ }
165
+
166
+ class LoadModelRequest(BaseModel):
167
+ model_path: str
168
+ context_size: Optional[int] = 65536
169
+ gpu_layers: Optional[int] = 99
170
+
171
+ @app.post("/api/model/load")
172
+ async def load_model_endpoint(req: LoadModelRequest):
173
+ model_path = req.model_path.strip()
174
+
175
+ # 支持三种写法:
176
+ # 1) 本地路径 /path/to/model.gguf -> -m
177
+ # 2) HuggingFace 仓库 user/repo[:quant] -> -hf(国内可 export HF_ENDPOINT=https://hf-mirror.com)
178
+ # 3) 直链 https://.../model.gguf -> -mu
179
+ if model_path.startswith(("http://", "https://")):
180
+ source, model_arg = "url", ["-mu", model_path]
181
+ elif re.fullmatch(r"[\w.-]+/[\w.-]+(:\w+)?", model_path) and not os.path.exists(model_path):
182
+ source, model_arg = "hf", ["-hf", model_path]
183
+ elif os.path.exists(model_path):
184
+ source, model_arg = "local", ["-m", model_path]
185
+ else:
186
+ return JSONResponse(status_code=400, content={
187
+ "error": f"Not found: {model_path}(既不是本地文件,也不是 user/repo 形式的 HF 仓库名)"})
188
+
189
+ ctx = req.context_size or DEFAULT_CONFIG["context_size"]
190
+ ngl = req.gpu_layers if req.gpu_layers is not None else 99
191
+
192
+ # Restart managed llama-server scope
193
+ subprocess.run(["systemctl", "--user", "stop", f"{LLAMA_SCOPE}.scope"], check=False)
194
+ time.sleep(1.5)
195
+
196
+ start_cmd = [
197
+ "systemd-run", "--user", "--scope", f"--unit={LLAMA_SCOPE}",
198
+ LLAMA_SERVER_BIN,
199
+ "--host", "0.0.0.0",
200
+ "--port", str(LLAMA_BACKEND_PORT),
201
+ *model_arg,
202
+ "-c", str(ctx),
203
+ "-ngl", str(ngl),
204
+ "-fa", "on",
205
+ "--alias", DEFAULT_MODEL_ALIAS,
206
+ "-t", "8"
207
+ ]
208
+ # Vision projector: sibling of the chosen model, else any well-known sibling dir
209
+ mmproj_dirs = [os.path.dirname(model_path)] if source == "local" else []
210
+ for extra in (current_config.get("mmproj_path"), DEFAULT_MODEL_PATH):
211
+ if extra and extra not in mmproj_dirs:
212
+ mmproj_dirs.append(extra if os.path.isdir(extra) else os.path.dirname(extra) or ".")
213
+ for d in mmproj_dirs:
214
+ candidate = os.path.join(d, "mmproj-Qwen3.5-4B-BF16.gguf")
215
+ if os.path.exists(candidate):
216
+ start_cmd.extend(["--mmproj", candidate])
217
+ break
218
+
219
+ proc = subprocess.Popen(start_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
220
+
221
+ # Poll for health check
222
+ ready = False
223
+ for _ in range(25):
224
+ time.sleep(0.5)
225
+ try:
226
+ with httpx.Client(timeout=1.0) as client:
227
+ res = client.get(f"http://127.0.0.1:{LLAMA_BACKEND_PORT}/health")
228
+ if res.status_code == 200:
229
+ ready = True
230
+ break
231
+ except Exception:
232
+ pass
233
+
234
+ if ready:
235
+ current_config["model_path"] = model_path
236
+ current_config["context_size"] = ctx
237
+ current_config["gpu_layers"] = ngl
238
+ current_config["backend_mode"] = "direct"
239
+ save_config(current_config)
240
+ return {"status": "ok", "message": f"Successfully loaded [{source}] {model_path}"}
241
+ else:
242
+ return JSONResponse(status_code=500, content={"error": "Failed to start llama-server with the specified model."})
243
+
244
+ class ChatMessage(BaseModel):
245
+ role: str
246
+ content: Union[str, List[Any], Dict[str, Any]]
247
+
248
+ class ChatStreamRequest(BaseModel):
249
+ messages: List[ChatMessage]
250
+ enable_thinking: Optional[bool] = None
251
+ reasoning_effort: Optional[str] = None
252
+ do_sample: Optional[bool] = None
253
+ temperature: Optional[float] = None
254
+ top_p: Optional[float] = None
255
+ top_k: Optional[int] = None
256
+ min_p: Optional[float] = None
257
+ presence_penalty: Optional[float] = None
258
+ repetition_penalty: Optional[float] = None
259
+ max_tokens: Optional[int] = None
260
+ system_prompt: Optional[str] = None
261
+
262
+ @app.post("/api/chat/stream")
263
+ async def chat_stream_endpoint(req: ChatStreamRequest, request: Request):
264
+ client_ip = request.client.host if request.client else "unknown"
265
+ t0 = time.time()
266
+
267
+ enable_thinking = req.enable_thinking if req.enable_thinking is not None else current_config.get("enable_thinking", True)
268
+ do_sample = req.do_sample if req.do_sample is not None else current_config.get("do_sample", True)
269
+ temp = req.temperature if req.temperature is not None else current_config.get("temperature", 1.0)
270
+ if not do_sample:
271
+ temp = 0.0
272
+ top_p = req.top_p if req.top_p is not None else current_config.get("top_p", 0.95)
273
+ top_k = req.top_k if req.top_k is not None else current_config.get("top_k", 20)
274
+ min_p = req.min_p if req.min_p is not None else current_config.get("min_p", 0.0)
275
+ pres_penalty = req.presence_penalty if req.presence_penalty is not None else current_config.get("presence_penalty", 1.5)
276
+ rep_penalty = req.repetition_penalty if req.repetition_penalty is not None else current_config.get("repetition_penalty", 1.0)
277
+ max_tokens = req.max_tokens if req.max_tokens is not None else current_config.get("max_tokens", 8192)
278
+ sys_prompt = req.system_prompt if req.system_prompt is not None else current_config.get("system_prompt", "")
279
+ effort = req.reasoning_effort if req.reasoning_effort is not None else current_config.get("reasoning_effort")
280
+ if effort in ("low", "medium"):
281
+ effort = "low"
282
+ elif effort in ("high", "xhigh"):
283
+ effort = "high"
284
+ else:
285
+ effort = "low"
286
+
287
+ messages_payload = []
288
+ if sys_prompt and sys_prompt.strip():
289
+ messages_payload.append({"role": "system", "content": sys_prompt.strip()})
290
+
291
+ for msg in req.messages:
292
+ messages_payload.append({"role": msg.role, "content": msg.content})
293
+
294
+ api_url = current_config.get("api_url", DEFAULT_API_URL)
295
+
296
+ # Resolve the model id: use configured alias, but fall back to whatever
297
+ # the backend actually serves (aliases drift when the user edits
298
+ # config.json; a stale name would 404 every chat).
299
+ model_alias = current_config.get("model_alias") or ""
300
+ async with httpx.AsyncClient(timeout=3.0) as probe:
301
+ try:
302
+ r = await probe.get(f"{api_url}/v1/models")
303
+ if r.status_code == 200:
304
+ served = [m.get("id", "") for m in r.json().get("data", [])]
305
+ if served and model_alias not in served:
306
+ model_alias = served[0]
307
+ except Exception:
308
+ pass
309
+ if not model_alias:
310
+ model_alias = "qwen-3.8-27b"
311
+
312
+ payload = {
313
+ "model": model_alias,
314
+ "messages": messages_payload,
315
+ "temperature": temp,
316
+ "top_p": top_p,
317
+ "max_tokens": max_tokens,
318
+ "stream": True,
319
+ "stop": ["<|end▁of▁sentence|>", "<|im_end|>", "</div>\n</div>\n</div>\n</div>"],
320
+ "chat_template_kwargs": {"enable_thinking": enable_thinking,
321
+ **({"reasoning_effort": effort} if effort else {})},
322
+ }
323
+ if top_k is not None and top_k > 0:
324
+ payload["top_k"] = top_k
325
+ if min_p is not None and min_p > 0:
326
+ payload["min_p"] = min_p
327
+ if pres_penalty is not None:
328
+ payload["presence_penalty"] = pres_penalty
329
+ if rep_penalty is not None and rep_penalty > 0:
330
+ payload["repetition_penalty"] = rep_penalty
331
+
332
+ async def event_generator():
333
+ thought_chunks = []
334
+ content_chunks = []
335
+ token_count = 0
336
+
337
+ in_thinking_mode = False
338
+
339
+ try:
340
+ async with httpx.AsyncClient(timeout=180.0) as client:
341
+ async with client.stream(
342
+ "POST",
343
+ f"{api_url}/v1/chat/completions",
344
+ json=payload,
345
+ headers={"Content-Type": "application/json"}
346
+ ) as response:
347
+ if response.status_code != 200:
348
+ err_text = await response.aread()
349
+ yield f"data: {json.dumps({'type': 'error', 'error': err_text.decode('utf-8')}, ensure_ascii=False)}\n\n"
350
+ return
351
+
352
+ async for line in response.aiter_lines():
353
+ if not line or not line.startswith("data: "):
354
+ continue
355
+ data_str = line[6:].strip()
356
+ if data_str == "[DONE]":
357
+ break
358
+ try:
359
+ chunk_json = json.loads(data_str)
360
+ choice = chunk_json.get("choices", [{}])[0]
361
+ delta = choice.get("delta", {})
362
+
363
+ # Reasoning trace
364
+ reasoning = delta.get("reasoning_content") or delta.get("reasoning")
365
+ if reasoning:
366
+ thought_chunks.append(reasoning)
367
+ token_count += 1
368
+ out_data = json.dumps({"type": "thought", "content": reasoning}, ensure_ascii=False)
369
+ yield f"data: {out_data}\n\n"
370
+
371
+ # Main content (HTML)
372
+ if "content" in delta and delta["content"]:
373
+ text = delta["content"]
374
+ token_count += 1
375
+
376
+ # Intercept and route raw <think> tags if present in content
377
+ cur = text
378
+ while cur:
379
+ if not in_thinking_mode:
380
+ if "<think>" in cur:
381
+ before, cur = cur.split("<think>", 1)
382
+ if before:
383
+ content_chunks.append(before)
384
+ yield f"data: {json.dumps({'type': 'content', 'content': before}, ensure_ascii=False)}\n\n"
385
+ in_thinking_mode = True
386
+ else:
387
+ content_chunks.append(cur)
388
+ yield f"data: {json.dumps({'type': 'content', 'content': cur}, ensure_ascii=False)}\n\n"
389
+ break
390
+ else:
391
+ if "</think>" in cur:
392
+ thought_part, cur = cur.split("</think>", 1)
393
+ if thought_part:
394
+ thought_chunks.append(thought_part)
395
+ yield f"data: {json.dumps({'type': 'thought', 'content': thought_part}, ensure_ascii=False)}\n\n"
396
+ in_thinking_mode = False
397
+ else:
398
+ thought_chunks.append(cur)
399
+ yield f"data: {json.dumps({'type': 'thought', 'content': cur}, ensure_ascii=False)}\n\n"
400
+ break
401
+ except Exception:
402
+ continue
403
+
404
+ elapsed = round(time.time() - t0, 2)
405
+ tok_per_sec = round(token_count / elapsed, 1) if elapsed > 0 else 0
406
+
407
+ full_thought = "".join(thought_chunks).strip()
408
+ full_content = "".join(content_chunks).strip()
409
+
410
+ done_payload = json.dumps({
411
+ "type": "done",
412
+ "tokens": token_count,
413
+ "elapsed": elapsed,
414
+ "tok_per_sec": tok_per_sec,
415
+ }, ensure_ascii=False)
416
+ yield f"data: {done_payload}\n\n"
417
+
418
+ # Log chat
419
+ log_chat({
420
+ "timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
421
+ "client_ip": client_ip,
422
+ "messages": [m.dict() for m in req.messages],
423
+ "tokens": token_count,
424
+ "elapsed": elapsed,
425
+ "tok_per_sec": tok_per_sec,
426
+ "enable_thinking": enable_thinking,
427
+ "thought_len": len(full_thought),
428
+ "content_len": len(full_content)
429
+ })
430
+
431
+ except Exception as ex:
432
+ err_data = json.dumps({"type": "error", "error": str(ex)}, ensure_ascii=False)
433
+ yield f"data: {err_data}\n\n"
434
+
435
+ return StreamingResponse(event_generator(), media_type="text/event-stream")
436
+
437
+ if __name__ == "__main__":
438
+ ap = argparse.ArgumentParser(description="Qwen-HTML WebUI")
439
+ ap.add_argument("--port", type=int, default=PORT, help=f"listen port (default {PORT}, env WEBUI_PORT)")
440
+ args = ap.parse_args()
441
+ print(f"Starting Qwen-HTML WebUI on http://0.0.0.0:{args.port} (api={DEFAULT_API_URL})")
442
+ uvicorn.run(app, host="0.0.0.0", port=args.port, log_level="info")
webui/static/index.html ADDED
@@ -0,0 +1,1613 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Mario-HTML Explorer</title>
7
+ <link rel="stylesheet" href="/static/theme.css">
8
+ <style id="custom-theme-style"></style>
9
+ <style>
10
+ :root {
11
+ --bg-page: #f8fafc;
12
+ --bg-surface: #ffffff;
13
+ --bg-surface-elevated: #f1f5f9;
14
+ --border-color: #e2e8f0;
15
+ --border-focus: #2563eb;
16
+ --text-main: #0f172a;
17
+ --text-sub: #64748b;
18
+ --accent: #2563eb;
19
+ --accent-hover: #1d4ed8;
20
+ --accent-subtle: #eff6ff;
21
+ }
22
+
23
+ * { box-sizing: border-box; margin: 0; padding: 0; }
24
+ body {
25
+ background-color: var(--bg-page);
26
+ color: var(--text-main);
27
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
28
+ height: 100vh;
29
+ display: flex;
30
+ flex-direction: column;
31
+ overflow: hidden;
32
+ padding: 0;
33
+ }
34
+
35
+ /* Header */
36
+ header {
37
+ background-color: rgba(255, 255, 255, 0.95);
38
+ border-bottom: 1px solid var(--border-color);
39
+ padding: 12px 24px;
40
+ display: flex;
41
+ align-items: center;
42
+ justify-content: space-between;
43
+ backdrop-filter: blur(8px);
44
+ box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
45
+ z-index: 20;
46
+ }
47
+ .brand {
48
+ display: flex;
49
+ align-items: center;
50
+ gap: 10px;
51
+ font-size: 1.1rem;
52
+ font-weight: 700;
53
+ color: var(--text-main);
54
+ letter-spacing: -0.02em;
55
+ }
56
+ .brand-badge {
57
+ background: var(--accent-subtle);
58
+ color: var(--accent);
59
+ font-size: 0.75rem;
60
+ padding: 2px 8px;
61
+ border-radius: 999px;
62
+ font-weight: 600;
63
+ border: 1px solid #bfdbfe;
64
+ }
65
+ .header-actions {
66
+ display: flex;
67
+ align-items: center;
68
+ gap: 12px;
69
+ }
70
+ .status-pill {
71
+ display: flex;
72
+ align-items: center;
73
+ gap: 6px;
74
+ font-size: 0.8rem;
75
+ padding: 4px 12px;
76
+ border-radius: 999px;
77
+ background: var(--bg-surface);
78
+ border: 1px solid var(--border-color);
79
+ color: var(--text-sub);
80
+ cursor: pointer;
81
+ box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
82
+ transition: all 0.2s;
83
+ }
84
+ .status-pill:hover { border-color: var(--accent); color: var(--text-main); }
85
+ .status-dot {
86
+ width: 8px;
87
+ height: 8px;
88
+ border-radius: 50%;
89
+ background: #10b981;
90
+ }
91
+ .status-dot.error { background: #ef4444; }
92
+
93
+ .btn-icon {
94
+ background: var(--bg-surface);
95
+ border: 1px solid var(--border-color);
96
+ color: #334155;
97
+ padding: 6px 12px;
98
+ border-radius: 6px;
99
+ font-size: 0.85rem;
100
+ font-weight: 500;
101
+ cursor: pointer;
102
+ display: inline-flex;
103
+ align-items: center;
104
+ gap: 6px;
105
+ box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
106
+ transition: all 0.2s;
107
+ }
108
+ .btn-icon:hover {
109
+ background: var(--bg-surface-elevated);
110
+ border-color: #cbd5e1;
111
+ color: var(--text-main);
112
+ }
113
+
114
+ /* Main Chat Container */
115
+ #chat-container {
116
+ flex: 1;
117
+ overflow-y: auto;
118
+ padding: 24px 16px 220px 16px;
119
+ scroll-behavior: smooth;
120
+ }
121
+ .chat-inner {
122
+ max-width: 920px;
123
+ margin: 0 auto;
124
+ display: flex;
125
+ flex-direction: column;
126
+ gap: 28px;
127
+ padding-bottom: 40px;
128
+ }
129
+
130
+ /* Welcome Banner */
131
+ .welcome-card {
132
+ background: linear-gradient(180deg, #ffffff 0%, #f8fafc 100%);
133
+ border: 1px solid var(--border-color);
134
+ border-radius: 14px;
135
+ padding: 32px 24px;
136
+ text-align: center;
137
+ margin-top: 16px;
138
+ box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
139
+ }
140
+ .welcome-title {
141
+ font-size: 1.45rem;
142
+ font-weight: 700;
143
+ margin-bottom: 8px;
144
+ color: var(--text-main);
145
+ }
146
+ .welcome-desc {
147
+ color: var(--text-sub);
148
+ font-size: 0.95rem;
149
+ max-width: 620px;
150
+ margin: 0 auto 24px auto;
151
+ line-height: 1.6;
152
+ }
153
+ .chips-grid {
154
+ display: flex;
155
+ flex-wrap: wrap;
156
+ gap: 10px;
157
+ justify-content: center;
158
+ }
159
+ .prompt-chip {
160
+ background: var(--bg-surface);
161
+ border: 1px solid var(--border-color);
162
+ padding: 7px 16px;
163
+ border-radius: 20px;
164
+ font-size: 0.85rem;
165
+ color: var(--text-main);
166
+ cursor: pointer;
167
+ box-shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
168
+ transition: all 0.2s;
169
+ }
170
+ .prompt-chip:hover {
171
+ background: var(--accent-subtle);
172
+ border-color: #93c5fd;
173
+ color: var(--accent);
174
+ transform: translateY(-1px);
175
+ }
176
+
177
+ /* Message Bubbles */
178
+ .message-row {
179
+ display: flex;
180
+ flex-direction: column;
181
+ gap: 8px;
182
+ animation: fadeIn 0.25s ease-out;
183
+ }
184
+ @keyframes fadeIn {
185
+ from { opacity: 0; transform: translateY(6px); }
186
+ to { opacity: 1; transform: translateY(0); }
187
+ }
188
+ .message-user {
189
+ align-self: flex-end;
190
+ max-width: 80%;
191
+ }
192
+ .user-bubble {
193
+ background: var(--accent);
194
+ color: #ffffff;
195
+ padding: 12px 18px;
196
+ border-radius: 14px 14px 2px 14px;
197
+ font-size: 0.95rem;
198
+ line-height: 1.5;
199
+ word-break: break-word;
200
+ box-shadow: 0 2px 4px rgba(37, 99, 235, 0.2);
201
+ }
202
+ .message-assistant {
203
+ align-self: stretch;
204
+ max-width: 100%;
205
+ }
206
+
207
+ /* Thinking Trace Block */
208
+ .thinking-box {
209
+ background: #f8fafc;
210
+ border: 1px solid var(--border-color);
211
+ border-left: 3px solid #6366f1;
212
+ border-radius: 8px;
213
+ margin-bottom: 12px;
214
+ overflow: hidden;
215
+ box-shadow: 0 1px 2px rgba(0, 0, 0, 0.02);
216
+ }
217
+ .thinking-summary {
218
+ padding: 9px 14px;
219
+ display: flex;
220
+ align-items: center;
221
+ gap: 8px;
222
+ font-size: 0.82rem;
223
+ font-weight: 600;
224
+ color: #475569;
225
+ font-family: var(--font-mono, monospace);
226
+ cursor: pointer;
227
+ user-select: none;
228
+ background: #f1f5f9;
229
+ }
230
+ .thinking-summary:hover {
231
+ color: var(--text-main);
232
+ }
233
+ .thinking-summary::marker { display: none; }
234
+ .thinking-content {
235
+ padding: 14px 18px;
236
+ font-family: var(--font-mono, monospace);
237
+ font-size: 0.82rem;
238
+ color: #475569;
239
+ line-height: 1.6;
240
+ white-space: pre-wrap;
241
+ word-break: break-word;
242
+ border-top: 1px solid var(--border-color);
243
+ max-height: 380px;
244
+ overflow-y: auto;
245
+ background: #f8fafc;
246
+ }
247
+
248
+ /* HTML Content Canvas */
249
+ .html-canvas {
250
+ background: #ffffff;
251
+ color: #1e293b;
252
+ border-radius: 14px;
253
+ padding: 24px;
254
+ box-shadow: 0 4px 16px rgba(0, 0, 0, 0.06);
255
+ border: 1px solid var(--border-color);
256
+ overflow-x: auto;
257
+ }
258
+ .raw-source-view {
259
+ background: #0f172a;
260
+ color: #38bdf8;
261
+ font-family: var(--font-mono, monospace);
262
+ font-size: 0.82rem;
263
+ padding: 16px;
264
+ border-radius: 8px;
265
+ white-space: pre-wrap;
266
+ word-break: break-all;
267
+ max-height: 400px;
268
+ overflow-y: auto;
269
+ border: 1px solid #1e293b;
270
+ display: none;
271
+ margin-top: 10px;
272
+ }
273
+
274
+ /* Action bar below message */
275
+ .msg-footer {
276
+ display: flex;
277
+ align-items: center;
278
+ justify-content: space-between;
279
+ margin-top: 10px;
280
+ padding: 8px 14px;
281
+ background: var(--bg-surface);
282
+ border: 1px solid var(--border-color);
283
+ border-radius: 8px;
284
+ font-size: 0.78rem;
285
+ color: var(--text-sub);
286
+ box-shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
287
+ }
288
+ .msg-stats {
289
+ display: flex;
290
+ align-items: center;
291
+ gap: 12px;
292
+ font-family: var(--font-mono, monospace);
293
+ font-weight: 500;
294
+ }
295
+ .msg-actions {
296
+ display: flex;
297
+ align-items: center;
298
+ gap: 8px;
299
+ }
300
+ .action-btn {
301
+ background: var(--bg-surface-elevated);
302
+ border: 1px solid var(--border-color);
303
+ color: #334155;
304
+ font-size: 0.75rem;
305
+ font-weight: 500;
306
+ padding: 4px 10px;
307
+ border-radius: 6px;
308
+ cursor: pointer;
309
+ display: inline-flex;
310
+ align-items: center;
311
+ gap: 4px;
312
+ transition: all 0.15s;
313
+ }
314
+ .action-btn:hover {
315
+ background: #e2e8f0;
316
+ color: var(--accent);
317
+ border-color: #cbd5e1;
318
+ }
319
+
320
+ /* Input Floating Bar */
321
+ .input-wrapper {
322
+ position: fixed;
323
+ bottom: 0;
324
+ left: 0;
325
+ right: 0;
326
+ background: linear-gradient(180deg, rgba(248, 250, 252, 0) 0%, rgba(248, 250, 252, 0.95) 25%, #f8fafc 100%);
327
+ padding: 16px 16px 22px 16px;
328
+ z-index: 10;
329
+ }
330
+ .input-box {
331
+ max-width: 920px;
332
+ margin: 0 auto;
333
+ background: var(--bg-surface);
334
+ border: 1px solid #cbd5e1;
335
+ border-radius: 14px;
336
+ padding: 12px 16px;
337
+ display: flex;
338
+ flex-direction: column;
339
+ gap: 8px;
340
+ box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.08), 0 8px 10px -6px rgba(0, 0, 0, 0.04);
341
+ transition: border-color 0.2s, box-shadow 0.2s;
342
+ }
343
+ .input-box:focus-within {
344
+ border-color: var(--accent);
345
+ box-shadow: 0 10px 25px -5px rgba(37, 99, 235, 0.12), 0 8px 10px -6px rgba(37, 99, 235, 0.08);
346
+ }
347
+ textarea {
348
+ width: 100%;
349
+ background: transparent;
350
+ border: none;
351
+ outline: none;
352
+ color: var(--text-main);
353
+ font-size: 0.95rem;
354
+ font-family: inherit;
355
+ resize: none;
356
+ min-height: 44px;
357
+ max-height: 180px;
358
+ line-height: 1.5;
359
+ }
360
+ textarea::placeholder {
361
+ color: #94a3b8;
362
+ }
363
+ .input-bottom-row {
364
+ display: flex;
365
+ align-items: center;
366
+ justify-content: space-between;
367
+ border-top: 1px solid #f1f5f9;
368
+ padding-top: 8px;
369
+ }
370
+ .toggle-label {
371
+ display: inline-flex;
372
+ align-items: center;
373
+ gap: 8px;
374
+ font-size: 0.8rem;
375
+ color: var(--text-sub);
376
+ cursor: pointer;
377
+ user-select: none;
378
+ font-weight: 500;
379
+ }
380
+ .toggle-switch {
381
+ position: relative;
382
+ width: 34px;
383
+ height: 18px;
384
+ background: #cbd5e1;
385
+ border-radius: 999px;
386
+ transition: background 0.2s;
387
+ }
388
+ .toggle-switch.active { background: #6366f1; }
389
+ .toggle-knob {
390
+ position: absolute;
391
+ top: 2px;
392
+ left: 2px;
393
+ width: 14px;
394
+ height: 14px;
395
+ background: #fff;
396
+ border-radius: 50%;
397
+ transition: transform 0.2s;
398
+ }
399
+ .toggle-switch.active .toggle-knob {
400
+ transform: translateX(16px);
401
+ }
402
+
403
+ .btn-send {
404
+ background: var(--accent);
405
+ color: #ffffff;
406
+ border: none;
407
+ padding: 7px 18px;
408
+ border-radius: 8px;
409
+ font-weight: 600;
410
+ font-size: 0.85rem;
411
+ cursor: pointer;
412
+ display: inline-flex;
413
+ align-items: center;
414
+ gap: 6px;
415
+ transition: all 0.2s;
416
+ }
417
+ .btn-send:hover { background: var(--accent-hover); }
418
+ .btn-send:disabled {
419
+ background: #e2e8f0;
420
+ color: #94a3b8;
421
+ cursor: not-allowed;
422
+ }
423
+
424
+ /* Settings Modal */
425
+ .modal-overlay {
426
+ position: fixed;
427
+ inset: 0;
428
+ background: rgba(15, 23, 42, 0.4);
429
+ backdrop-filter: blur(4px);
430
+ display: none;
431
+ align-items: center;
432
+ justify-content: center;
433
+ z-index: 100;
434
+ }
435
+ .modal-overlay.open { display: flex; }
436
+ .modal-card {
437
+ background: #ffffff;
438
+ border: 1px solid var(--border-color);
439
+ border-radius: 14px;
440
+ width: 90%;
441
+ max-width: 620px;
442
+ max-height: 85vh;
443
+ display: flex;
444
+ flex-direction: column;
445
+ box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
446
+ animation: modalIn 0.2s ease-out;
447
+ }
448
+ @keyframes modalIn {
449
+ from { transform: scale(0.96); opacity: 0; }
450
+ to { transform: scale(1); opacity: 1; }
451
+ }
452
+ .modal-header {
453
+ padding: 16px 20px;
454
+ border-bottom: 1px solid var(--border-color);
455
+ display: flex;
456
+ align-items: center;
457
+ justify-content: space-between;
458
+ }
459
+ .modal-header h3 { font-size: 1.1rem; color: var(--text-main); }
460
+ .modal-close {
461
+ background: none;
462
+ border: none;
463
+ color: var(--text-sub);
464
+ font-size: 1.2rem;
465
+ cursor: pointer;
466
+ }
467
+ .modal-close:hover { color: var(--text-main); }
468
+ .modal-body {
469
+ padding: 20px;
470
+ overflow-y: auto;
471
+ display: flex;
472
+ flex-direction: column;
473
+ gap: 20px;
474
+ }
475
+ .modal-footer {
476
+ padding: 14px 20px;
477
+ border-top: 1px solid var(--border-color);
478
+ display: flex;
479
+ justify-content: flex-end;
480
+ gap: 10px;
481
+ }
482
+
483
+ .form-group {
484
+ display: flex;
485
+ flex-direction: column;
486
+ gap: 6px;
487
+ }
488
+ .form-group label {
489
+ font-size: 0.85rem;
490
+ font-weight: 600;
491
+ color: var(--text-main);
492
+ }
493
+ .form-desc {
494
+ font-size: 0.75rem;
495
+ color: var(--text-sub);
496
+ }
497
+ .input-text, select {
498
+ background: #ffffff;
499
+ border: 1px solid #cbd5e1;
500
+ color: var(--text-main);
501
+ padding: 8px 12px;
502
+ border-radius: 6px;
503
+ font-size: 0.85rem;
504
+ font-family: inherit;
505
+ outline: none;
506
+ }
507
+ .input-text:focus, select:focus {
508
+ border-color: var(--accent);
509
+ }
510
+ .tab-pills {
511
+ display: flex;
512
+ gap: 6px;
513
+ background: #f1f5f9;
514
+ padding: 4px;
515
+ border-radius: 8px;
516
+ }
517
+ .tab-pill {
518
+ flex: 1;
519
+ text-align: center;
520
+ padding: 6px 12px;
521
+ font-size: 0.8rem;
522
+ font-weight: 600;
523
+ border-radius: 6px;
524
+ cursor: pointer;
525
+ color: var(--text-sub);
526
+ transition: all 0.2s;
527
+ }
528
+ .tab-pill.active {
529
+ background: #ffffff;
530
+ color: var(--text-main);
531
+ box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
532
+ }
533
+ .modal-nav-tabs {
534
+ display: flex;
535
+ border-bottom: 1px solid var(--border-color);
536
+ background: #f8fafc;
537
+ border-top-left-radius: 14px;
538
+ border-top-right-radius: 14px;
539
+ }
540
+ .modal-nav-tab {
541
+ flex: 1;
542
+ text-align: center;
543
+ padding: 11px 14px;
544
+ font-size: 0.82rem;
545
+ font-weight: 600;
546
+ color: var(--text-sub);
547
+ cursor: pointer;
548
+ border-bottom: 2px solid transparent;
549
+ transition: all 0.2s;
550
+ }
551
+ .modal-nav-tab:hover {
552
+ color: var(--text-main);
553
+ background: #f1f5f9;
554
+ }
555
+ .modal-nav-tab.active {
556
+ color: var(--accent);
557
+ border-bottom-color: var(--accent);
558
+ background: #ffffff;
559
+ }
560
+ .preset-grid {
561
+ display: grid;
562
+ grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
563
+ gap: 8px;
564
+ }
565
+ .preset-btn {
566
+ background: #f8fafc;
567
+ border: 1px solid var(--border-color);
568
+ border-radius: 8px;
569
+ padding: 9px 12px;
570
+ font-size: 0.8rem;
571
+ font-weight: 600;
572
+ color: #334155;
573
+ cursor: pointer;
574
+ text-align: center;
575
+ transition: all 0.15s;
576
+ }
577
+ .preset-btn:hover {
578
+ background: var(--accent-subtle);
579
+ border-color: #93c5fd;
580
+ color: var(--accent);
581
+ }
582
+ .preset-btn.active {
583
+ background: var(--accent-subtle);
584
+ border-color: var(--accent);
585
+ color: var(--accent);
586
+ box-shadow: 0 0 0 1px var(--accent);
587
+ }
588
+ .snippet-pills {
589
+ display: flex;
590
+ flex-wrap: wrap;
591
+ gap: 6px;
592
+ margin-top: 6px;
593
+ }
594
+ .snippet-pill {
595
+ background: #f1f5f9;
596
+ border: 1px solid var(--border-color);
597
+ color: #475569;
598
+ font-size: 0.72rem;
599
+ padding: 3px 8px;
600
+ border-radius: 4px;
601
+ cursor: pointer;
602
+ font-family: var(--font-mono, monospace);
603
+ }
604
+ .snippet-pill:hover {
605
+ background: #e2e8f0;
606
+ color: var(--accent);
607
+ }
608
+ .slider-row {
609
+ display: flex;
610
+ align-items: center;
611
+ gap: 12px;
612
+ }
613
+ .slider-row input[type="range"] { flex: 1; }
614
+ .slider-val {
615
+ font-family: var(--font-mono, monospace);
616
+ font-size: 0.85rem;
617
+ width: 50px;
618
+ text-align: right;
619
+ color: var(--text-main);
620
+ }
621
+ </style>
622
+ </head>
623
+ <body>
624
+
625
+ <!-- Top Navigation -->
626
+ <header>
627
+ <div class="brand">
628
+ <span>🍄 Mario-HTML Explorer</span>
629
+ <span class="brand-badge">RL Math &amp; HTML Native</span>
630
+ </div>
631
+ <div class="header-actions">
632
+ <div class="status-pill" id="status-pill" onclick="openSettings()">
633
+ <span class="status-dot" id="status-dot"></span>
634
+ <span id="status-text">Connecting...</span>
635
+ </div>
636
+ <button class="btn-icon" onclick="clearChat()">🧹 Clear</button>
637
+ <button class="btn-icon" onclick="openSettings()">⚙️ Settings</button>
638
+ </div>
639
+ </header>
640
+
641
+ <!-- Chat View -->
642
+ <main id="chat-container">
643
+ <div class="chat-inner" id="messages-list">
644
+
645
+ <!-- Welcome Card -->
646
+ <div class="welcome-card" id="welcome-card">
647
+ <div class="welcome-title">Spark-x2.6-4B · Mario-HTML</div>
648
+ <p class="welcome-desc">
649
+ Spark-x2.6-4B + LoRA 在 830 条马里奥 HTML 数据上 SFT(1.4 Epoch)。思维链保留、正文输出规范卡片,思考强度可在输入框左侧切换。
650
+ </p>
651
+ <div class="chips-grid">
652
+ <div class="prompt-chip" onclick="sendPreset('Solve the linear equation: 2x + 5 = 17 step by step')">📐 2x + 5 = 17 Equation</div>
653
+ <div class="prompt-chip" onclick="sendPreset('Explain Binary Search algorithm with complexity and steps')">⚡ Binary Search</div>
654
+ <div class="prompt-chip" onclick="sendPreset('Compare 9.11 and 9.9 — which one is larger and why?')">⚖️ 9.11 vs 9.9</div>
655
+ <div class="prompt-chip" onclick="sendPreset('Compare Microservices vs Monolith architecture in a modern comparison grid')">🏛️ Microservices vs Monolith</div>
656
+ </div>
657
+ </div>
658
+
659
+ </div>
660
+ </main>
661
+
662
+ <!-- Floating Input Bar -->
663
+ <div class="input-wrapper">
664
+ <div class="input-box">
665
+ <!-- Attached image preview bar -->
666
+ <div id="image-preview-bar" style="display:none; padding:8px 12px; align-items:center; gap:10px; background:#f1f5f9; border-radius:8px; margin-bottom:8px; border:1px solid #e2e8f0;">
667
+ <img id="attached-img-thumb" style="max-height:48px; max-width:64px; border-radius:6px; object-fit:cover; border:1px solid #cbd5e1;">
668
+ <span id="attached-img-name" style="font-size:0.8rem; color:#475569; flex-grow:1; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;">image.png</span>
669
+ <button onclick="removeAttachedImage()" type="button" style="background:none; border:none; color:#ef4444; font-size:1.1rem; cursor:pointer; padding:0 4px; font-weight:bold;" title="Remove image">✕</button>
670
+ </div>
671
+
672
+ <textarea id="prompt-input" placeholder="Ask a question or request a structured HTML card presentation..." rows="1" onkeydown="handleKeyDown(event)"></textarea>
673
+ <div class="input-bottom-row">
674
+ <div style="display:flex; align-items:center; gap:10px;">
675
+ <label class="toggle-label" onclick="toggleThinking()">
676
+ <span class="toggle-switch active" id="thinking-switch">
677
+ <span class="toggle-knob"></span>
678
+ </span>
679
+ <span id="thinking-label">🧠 CoT: ON</span>
680
+ </label>
681
+ <select id="effort-select" onchange="setEffort(this.value)" title="思考强度:低 = 快速出卡,高 = 深度推演"
682
+ style="background:#ffffff;border:1px solid #cbd5e1;border-radius:8px;padding:5px 8px;font-size:0.82rem;color:#334155;cursor:pointer;">
683
+ <option value="low" selected>思考: 低(快速出卡,推荐)</option>
684
+ <option value="medium">思考: 中(标准推演)</option>
685
+ <option value="xhigh">思考: 高(深度推演)</option>
686
+ </select>
687
+ </div>
688
+ <button class="btn-send" id="send-btn" onclick="handleSendOrStop()">Send ↵</button>
689
+ </div>
690
+ </div>
691
+ </div>
692
+
693
+ <!-- Settings Modal -->
694
+ <div class="modal-overlay" id="settings-modal">
695
+ <div class="modal-card">
696
+ <div class="modal-header">
697
+ <h3>Model &amp; Inference Settings</h3>
698
+ <button class="modal-close" onclick="closeSettings()">✕</button>
699
+ </div>
700
+ <div class="modal-nav-tabs">
701
+ <div class="modal-nav-tab active" id="nav-tab-conn" onclick="switchModalNav('conn')">🔌 Model &amp; Engine</div>
702
+ <div class="modal-nav-tab" id="nav-tab-theme" onclick="switchModalNav('theme')">🎨 Styling &amp; Theme</div>
703
+ <div class="modal-nav-tab" id="nav-tab-params" onclick="switchModalNav('params')">⚙️ Parameters</div>
704
+ </div>
705
+ <div class="modal-body">
706
+
707
+ <!-- SECTION 1: Model & Engine -->
708
+ <div id="modal-sec-conn" style="display: flex; flex-direction: column; gap: 16px;">
709
+ <div class="form-group">
710
+ <label>Backend Provider Mode</label>
711
+ <div class="tab-pills">
712
+ <div class="tab-pill active" id="tab-api" onclick="switchBackendTab('api')">Llama.cpp API Server</div>
713
+ <div class="tab-pill" id="tab-direct" onclick="switchBackendTab('direct')">Direct Model Path</div>
714
+ </div>
715
+ </div>
716
+
717
+ <!-- API Mode Section -->
718
+ <div id="section-api" style="display: flex; flex-direction: column; gap: 12px;">
719
+ <div class="form-group">
720
+ <label>Llama.cpp Server URL</label>
721
+ <input type="text" class="input-text" id="cfg-api-url" placeholder="http://127.0.0.1:18085">
722
+ <span class="form-desc">Compatible with standard OpenAI-style <code>/v1/chat/completions</code> endpoint.</span>
723
+ </div>
724
+ </div>
725
+
726
+ <!-- Direct Mode Section -->
727
+ <div id="section-direct" style="display: none; flex-direction: column; gap: 12px;">
728
+ <div class="form-group">
729
+ <label>Model Preset</label>
730
+ <select id="cfg-model-preset" onchange="onPresetChange()">
731
+ <option value="/mnt/sdb/models/qwen3.5-4b-html-v4-grpo-math-gguf/Qwen3.5-4B-HTML-v4-GRPO-Math-Q8_0.gguf">Qwen3.5-4B-HTML-Q8_0 (Recommended, 4.3GB)</option>
732
+ <option value="/mnt/sdb/models/qwen3.5-4b-html-v4-grpo-math-gguf/Qwen3.5-4B-HTML-v4-GRPO-Math-Q4_K_M.gguf">Qwen3.5-4B-HTML-Q4_K_M (Fast, 2.6GB)</option>
733
+ <option value="custom">Custom Path...</option>
734
+ </select>
735
+ </div>
736
+ <div class="form-group">
737
+ <label>GGUF Model Path</label>
738
+ <input type="text" class="input-text" id="cfg-model-path" placeholder="/mnt/sdb/models/...">
739
+ </div>
740
+ <div style="display: flex; gap: 12px;">
741
+ <div class="form-group" style="flex:1;">
742
+ <label>Context Window</label>
743
+ <input type="number" class="input-text" id="cfg-ctx" value="65536">
744
+ </div>
745
+ <div class="form-group" style="flex:1;">
746
+ <label>GPU Offload Layers</label>
747
+ <input type="number" class="input-text" id="cfg-ngl" value="99">
748
+ </div>
749
+ </div>
750
+ <button class="btn-icon" style="background:#2563eb; color:#fff; justify-content:center; padding:8px;" onclick="loadDirectModel()">
751
+ 🚀 Load Model &amp; Restart Llama Server
752
+ </button>
753
+ </div>
754
+ </div>
755
+
756
+ <!-- SECTION 2: Styling & Theme -->
757
+ <div id="modal-sec-theme" style="display: none; flex-direction: column; gap: 16px;">
758
+ <div class="form-group">
759
+ <label>Theme Presets (One-Click Style Switch)</label>
760
+ <div class="preset-grid">
761
+ <button type="button" class="preset-btn active" id="btn-theme-default" onclick="selectThemePreset('default')">Default Modern</button>
762
+ <button type="button" class="preset-btn" id="btn-theme-apple" onclick="selectThemePreset('apple')">Apple Clean</button>
763
+ <button type="button" class="preset-btn" id="btn-theme-notion" onclick="selectThemePreset('notion')">Notion Minimal</button>
764
+ <button type="button" class="preset-btn" id="btn-theme-cyber" onclick="selectThemePreset('cyber')">Cyber Neon</button>
765
+ <button type="button" class="preset-btn" id="btn-theme-editorial" onclick="selectThemePreset('editorial')">Editorial Serif</button>
766
+ </div>
767
+ </div>
768
+
769
+ <div class="form-group">
770
+ <div style="display:flex; justify-content:space-between; align-items:center;">
771
+ <label>Custom CSS Overrides</label>
772
+ <button type="button" class="action-link" style="font-size:0.75rem;" onclick="resetCustomCss()">Reset CSS</button>
773
+ </div>
774
+ <textarea class="input-text" id="cfg-custom-css" rows="8" placeholder="/* Enter custom CSS rules to customize .ai-view and cards */" oninput="applyLiveCustomCss(this.value)" style="font-size:0.78rem; font-family:var(--font-mono, monospace); line-height:1.5;"></textarea>
775
+ <span class="form-desc">Injected live in real-time. Overrides apply instantly to all cards &amp; responses.</span>
776
+
777
+ <div class="snippet-pills">
778
+ <span style="font-size:0.72rem; color:var(--text-sub); align-self:center;">Quick Snippets:</span>
779
+ <button type="button" class="snippet-pill" onclick="insertCssSnippet('cards_rounded')">+ 24px Round Cards</button>
780
+ <button type="button" class="snippet-pill" onclick="insertCssSnippet('glassmorphism')">+ Frosted Glass</button>
781
+ <button type="button" class="snippet-pill" onclick="insertCssSnippet('soft_shadows')">+ Subtle Glow</button>
782
+ <button type="button" class="snippet-pill" onclick="insertCssSnippet('compact')">+ Compact Density</button>
783
+ </div>
784
+ </div>
785
+ </div>
786
+
787
+ <!-- SECTION 3: Parameters -->
788
+ <div id="modal-sec-params" style="display: none; flex-direction: column; gap: 16px;">
789
+
790
+ <div style="display:flex; justify-content:space-between; align-items:center; background:var(--bg-secondary); padding:10px 14px; border-radius:var(--radius-sm); border:1px solid var(--border);">
791
+ <div>
792
+ <div style="font-weight:600; font-size:0.85rem;">Sampling Decoding (Do Sample)</div>
793
+ <div style="font-size:0.72rem; color:var(--text-sub);">Enable probabilistic sampling. If disabled, uses greedy decoding (temp=0).</div>
794
+ </div>
795
+ <input type="checkbox" id="cfg-do-sample" style="width:18px; height:18px; accent-color:var(--accent); cursor:pointer;">
796
+ </div>
797
+
798
+ <div class="form-group">
799
+ <div style="display:flex; justify-content:space-between;">
800
+ <label>Temperature</label>
801
+ <span class="slider-val" id="temp-val">1.00</span>
802
+ </div>
803
+ <div class="slider-row">
804
+ <input type="range" id="cfg-temp" min="0" max="2.0" step="0.05" oninput="document.getElementById('temp-val').innerText = parseFloat(this.value).toFixed(2)">
805
+ </div>
806
+ <span class="form-desc">Higher = more creative; lower = more focused and deterministic. Default: 1.0.</span>
807
+ </div>
808
+
809
+ <div class="form-group">
810
+ <div style="display:flex; justify-content:space-between;">
811
+ <label>Top-P (Nucleus Sampling)</label>
812
+ <span class="slider-val" id="topp-val">0.95</span>
813
+ </div>
814
+ <div class="slider-row">
815
+ <input type="range" id="cfg-top-p" min="0" max="1.0" step="0.05" oninput="document.getElementById('topp-val').innerText = parseFloat(this.value).toFixed(2)">
816
+ </div>
817
+ <span class="form-desc">Limits cumulative probability mass of top candidates. Default: 0.95.</span>
818
+ </div>
819
+
820
+ <div class="form-group">
821
+ <div style="display:flex; justify-content:space-between;">
822
+ <label>Top-K Sampling</label>
823
+ <span class="slider-val" id="topk-val">20</span>
824
+ </div>
825
+ <div class="slider-row">
826
+ <input type="range" id="cfg-top-k" min="0" max="100" step="1" oninput="document.getElementById('topk-val').innerText = this.value === '0' ? 'Off' : this.value">
827
+ </div>
828
+ <span class="form-desc">Sample only from top K tokens. Set to 0 to disable. Default: 20.</span>
829
+ </div>
830
+
831
+ <div class="form-group">
832
+ <div style="display:flex; justify-content:space-between;">
833
+ <label>Min-P Sampling</label>
834
+ <span class="slider-val" id="minp-val">0.00</span>
835
+ </div>
836
+ <div class="slider-row">
837
+ <input type="range" id="cfg-min-p" min="0.0" max="1.0" step="0.01" oninput="document.getElementById('minp-val').innerText = parseFloat(this.value).toFixed(2)">
838
+ </div>
839
+ <span class="form-desc">Minimum probability threshold relative to most likely token. Default: 0.00.</span>
840
+ </div>
841
+
842
+ <div class="form-group">
843
+ <div style="display:flex; justify-content:space-between;">
844
+ <label>Presence Penalty (存在惩罚)</label>
845
+ <span class="slider-val" id="pres-val">1.50</span>
846
+ </div>
847
+ <div class="slider-row">
848
+ <input type="range" id="cfg-pres-penalty" min="-2.0" max="2.0" step="0.1" oninput="document.getElementById('pres-val').innerText = parseFloat(this.value).toFixed(2)">
849
+ </div>
850
+ <span class="form-desc">Penalize tokens based on presence in text to encourage new topics. Default: 1.50.</span>
851
+ </div>
852
+
853
+ <div class="form-group">
854
+ <div style="display:flex; justify-content:space-between;">
855
+ <label>Repetition Penalty (重复性惩罚)</label>
856
+ <span class="slider-val" id="rep-val">1.00</span>
857
+ </div>
858
+ <div class="slider-row">
859
+ <input type="range" id="cfg-rep-penalty" min="1.0" max="2.0" step="0.02" oninput="document.getElementById('rep-val').innerText = parseFloat(this.value).toFixed(2)">
860
+ </div>
861
+ <span class="form-desc">Penalize recurring tokens to prevent repetitive n-gram loops. Default: 1.00.</span>
862
+ </div>
863
+
864
+ <div class="form-group">
865
+ <div style="display:flex; justify-content:space-between;">
866
+ <label>Max Output Tokens (提高至最大 Context 80K)</label>
867
+ <span class="slider-val" id="max-val">8192</span>
868
+ </div>
869
+ <div class="slider-row">
870
+ <input type="range" id="cfg-max-tokens" min="512" max="81920" step="512" oninput="document.getElementById('max-val').innerText = this.value">
871
+ </div>
872
+ <div class="snippet-pills" style="margin-top:6px;">
873
+ <span style="font-size:0.72rem; color:var(--text-sub); align-self:center;">Presets:</span>
874
+ <button type="button" class="snippet-pill" onclick="setMaxTokensPreset(4096)">4K</button>
875
+ <button type="button" class="snippet-pill" onclick="setMaxTokensPreset(8192)">8K</button>
876
+ <button type="button" class="snippet-pill" onclick="setMaxTokensPreset(16384)">16K</button>
877
+ <button type="button" class="snippet-pill" onclick="setMaxTokensPreset(32768)">32K</button>
878
+ <button type="button" class="snippet-pill" onclick="setMaxTokensPreset(65536)">64K</button>
879
+ <button type="button" class="snippet-pill" onclick="setMaxTokensPreset(81920)">80K Max</button>
880
+ </div>
881
+ <span class="form-desc">Maximum generation token limit per response (up to full 80K context window).</span>
882
+ </div>
883
+
884
+ <div class="form-group">
885
+ <div style="display:flex; justify-content:space-between; align-items:center;">
886
+ <label>System Prompt (留空默认 Mario Zero-Prompt)</label>
887
+ <button type="button" class="action-link" style="font-size:0.72rem;" onclick="document.getElementById('cfg-system-prompt').value=''">Clear Prompt</button>
888
+ </div>
889
+ <textarea class="input-text" id="cfg-system-prompt" rows="4" placeholder="Leave empty to use native Mario card-generating persona" style="font-size:0.75rem; font-family:var(--font-mono, monospace);"></textarea>
890
+ <span class="form-desc">Default is empty. Mario natively formats visual HTML cards without manual prompts.</span>
891
+ </div>
892
+ </div>
893
+
894
+ </div>
895
+ <div class="modal-footer">
896
+ <button class="btn-icon" onclick="closeSettings()">Cancel</button>
897
+ <button class="btn-icon" style="background:var(--accent); color:#ffffff;" onclick="saveSettings()">Save Settings</button>
898
+ </div>
899
+ </div>
900
+ </div>
901
+
902
+ <script>
903
+ let appConfig = {};
904
+ let isGenerating = false;
905
+ let conversationHistory = [];
906
+
907
+ const THEME_PRESETS = {
908
+ default: `/* Default Modern Theme */`,
909
+ apple: `/* Apple Clean */
910
+ .html-canvas {
911
+ border-radius: 20px !important;
912
+ background: #fbfbfd !important;
913
+ border: 1px solid rgba(0,0,0,0.06) !important;
914
+ box-shadow: 0 10px 30px rgba(0,0,0,0.05) !important;
915
+ font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", sans-serif !important;
916
+ }
917
+ .card {
918
+ border-radius: 16px !important;
919
+ border: 1px solid rgba(0,0,0,0.06) !important;
920
+ background: #ffffff !important;
921
+ box-shadow: 0 2px 8px rgba(0,0,0,0.04) !important;
922
+ }
923
+ .summary-card {
924
+ border-radius: 16px !important;
925
+ border-left-width: 6px !important;
926
+ }
927
+ .callout {
928
+ border-radius: 14px !important;
929
+ }
930
+ .badge {
931
+ border-radius: 8px !important;
932
+ font-weight: 600 !important;
933
+ }`,
934
+ notion: `/* Notion Minimal */
935
+ .html-canvas {
936
+ border-radius: 4px !important;
937
+ border: 1px solid #37352f !important;
938
+ box-shadow: none !important;
939
+ background: #ffffff !important;
940
+ }
941
+ .card {
942
+ border-radius: 4px !important;
943
+ border: 1px solid #e3e2e0 !important;
944
+ box-shadow: none !important;
945
+ }
946
+ .summary-card {
947
+ border-radius: 4px !important;
948
+ border-left-color: #37352f !important;
949
+ }
950
+ .callout {
951
+ border-radius: 4px !important;
952
+ border-left-width: 4px !important;
953
+ }
954
+ .badge {
955
+ border-radius: 3px !important;
956
+ font-weight: 500 !important;
957
+ }`,
958
+ cyber: `/* Cyber Neon */
959
+ .html-canvas {
960
+ background: #090d16 !important;
961
+ color: #e2e8f0 !important;
962
+ border: 1px solid #00f2ff !important;
963
+ box-shadow: 0 0 25px rgba(0, 242, 255, 0.25) !important;
964
+ }
965
+ .card {
966
+ background: #111827 !important;
967
+ border: 1px solid #1f2937 !important;
968
+ color: #f8fafc !important;
969
+ }
970
+ .summary-card {
971
+ background: #111827 !important;
972
+ border-left-color: #00f2ff !important;
973
+ }
974
+ .modern-table th {
975
+ background: #1e293b !important;
976
+ color: #00f2ff !important;
977
+ }
978
+ .badge-accent {
979
+ background: rgba(0, 242, 255, 0.2) !important;
980
+ color: #00f2ff !important;
981
+ border: 1px solid #00f2ff !important;
982
+ }`,
983
+ editorial: `/* Editorial Serif */
984
+ .html-canvas {
985
+ background: #fdfbf7 !important;
986
+ color: #2c2523 !important;
987
+ font-family: "Georgia", "Times New Roman", serif !important;
988
+ border: 1px solid #e7dfd5 !important;
989
+ }
990
+ .card {
991
+ background: #ffffff !important;
992
+ border: 1px solid #e7dfd5 !important;
993
+ box-shadow: 0 2px 4px rgba(44, 37, 35, 0.04) !important;
994
+ }
995
+ h2, h3, h4 {
996
+ font-family: "Georgia", serif !important;
997
+ font-weight: 600 !important;
998
+ }
999
+ .modern-table {
1000
+ border-top: 2px solid #2c2523 !important;
1001
+ border-bottom: 2px solid #2c2523 !important;
1002
+ }`
1003
+ };
1004
+
1005
+ const CSS_SNIPPETS = {
1006
+ cards_rounded: `\n/* Rounded Cards */\n.card, .html-canvas { border-radius: 24px !important; }\n`,
1007
+ glassmorphism: `\n/* Frosted Glass */\n.card {\n background: rgba(255, 255, 255, 0.8) !important;\n backdrop-filter: blur(12px) !important;\n border: 1px solid rgba(255, 255, 255, 0.6) !important;\n}\n`,
1008
+ soft_shadows: `\n/* Subtle Glow */\n.card:hover {\n box-shadow: 0 10px 25px -5px rgba(37, 99, 235, 0.15) !important;\n border-color: #93c5fd !important;\n}\n`,
1009
+ compact: `\n/* Compact Density */\n.ai-view { gap: 12px !important; }\n.card { padding: 14px 18px !important; }\n`
1010
+ };
1011
+
1012
+ function switchModalNav(tab) {
1013
+ document.getElementById("nav-tab-conn").className = tab === 'conn' ? "modal-nav-tab active" : "modal-nav-tab";
1014
+ document.getElementById("nav-tab-theme").className = tab === 'theme' ? "modal-nav-tab active" : "modal-nav-tab";
1015
+ document.getElementById("nav-tab-params").className = tab === 'params' ? "modal-nav-tab active" : "modal-nav-tab";
1016
+
1017
+ document.getElementById("modal-sec-conn").style.display = tab === 'conn' ? 'flex' : 'none';
1018
+ document.getElementById("modal-sec-theme").style.display = tab === 'theme' ? 'flex' : 'none';
1019
+ document.getElementById("modal-sec-params").style.display = tab === 'params' ? 'flex' : 'none';
1020
+ }
1021
+
1022
+ function applyLiveCustomCss(cssText) {
1023
+ let tag = document.getElementById("custom-theme-style");
1024
+ if (!tag) {
1025
+ tag = document.createElement("style");
1026
+ tag.id = "custom-theme-style";
1027
+ document.head.appendChild(tag);
1028
+ }
1029
+ tag.textContent = cssText || "";
1030
+ }
1031
+
1032
+ function selectThemePreset(name) {
1033
+ appConfig.theme_preset = name;
1034
+ document.querySelectorAll(".preset-btn").forEach(b => b.classList.remove("active"));
1035
+ const activeBtn = document.getElementById("btn-theme-" + name);
1036
+ if (activeBtn) activeBtn.classList.add("active");
1037
+
1038
+ const css = THEME_PRESETS[name] || "";
1039
+ document.getElementById("cfg-custom-css").value = css;
1040
+ applyLiveCustomCss(css);
1041
+ }
1042
+
1043
+ function insertCssSnippet(key) {
1044
+ const snip = CSS_SNIPPETS[key] || "";
1045
+ const tx = document.getElementById("cfg-custom-css");
1046
+ tx.value = (tx.value + "\n" + snip).trim();
1047
+ applyLiveCustomCss(tx.value);
1048
+ }
1049
+
1050
+ function resetCustomCss() {
1051
+ selectThemePreset("default");
1052
+ }
1053
+
1054
+ // Initialize
1055
+ window.addEventListener("DOMContentLoaded", async () => {
1056
+ await fetchConfig();
1057
+ await checkStatus();
1058
+ setInterval(checkStatus, 5000);
1059
+ autoResizeTextarea();
1060
+ });
1061
+
1062
+ async function fetchConfig() {
1063
+ try {
1064
+ const res = await fetch("/api/config");
1065
+ appConfig = await res.json();
1066
+ // 思考强度:localStorage 记住上次选择,否则用 config 里的值(low)
1067
+ let eff = appConfig.reasoning_effort || "low";
1068
+ try { eff = localStorage.getItem("reasoning_effort") || eff; } catch (e) {}
1069
+ appConfig.reasoning_effort = eff;
1070
+ const effSel = document.getElementById("effort-select");
1071
+ if (effSel) effSel.value = eff;
1072
+ const preset = appConfig.theme_preset || "default";
1073
+ const css = appConfig.custom_css || (preset !== 'default' ? THEME_PRESETS[preset] : "");
1074
+ applyLiveCustomCss(css);
1075
+ updateSettingsModalForm();
1076
+ } catch (e) {
1077
+ console.error("Failed to load config", e);
1078
+ }
1079
+ }
1080
+
1081
+ async function checkStatus() {
1082
+ try {
1083
+ const res = await fetch("/api/status");
1084
+ const data = await res.json();
1085
+ const dot = document.getElementById("status-dot");
1086
+ const txt = document.getElementById("status-text");
1087
+
1088
+ if (data.status === "ready") {
1089
+ dot.className = "status-dot";
1090
+ txt.innerText = `${data.model_info} (${data.latency_ms || 0}ms)`;
1091
+ } else {
1092
+ dot.className = "status-dot error";
1093
+ txt.innerText = "Server Offline";
1094
+ }
1095
+ } catch (e) {
1096
+ document.getElementById("status-dot").className = "status-dot error";
1097
+ document.getElementById("status-text").innerText = "Connection Failed";
1098
+ }
1099
+ }
1100
+
1101
+ function setEffort(v) {
1102
+ appConfig.reasoning_effort = v;
1103
+ try { localStorage.setItem("reasoning_effort", v); } catch (e) {}
1104
+ }
1105
+
1106
+ function toggleThinking() {
1107
+ appConfig.enable_thinking = !appConfig.enable_thinking;
1108
+ const sw = document.getElementById("thinking-switch");
1109
+ const lbl = document.getElementById("thinking-label");
1110
+ if (appConfig.enable_thinking) {
1111
+ sw.className = "toggle-switch active";
1112
+ lbl.innerText = "🧠 CoT Reasoning: ON";
1113
+ } else {
1114
+ sw.className = "toggle-switch";
1115
+ lbl.innerText = "⚡ Direct HTML (No CoT)";
1116
+ }
1117
+ }
1118
+
1119
+ function switchBackendTab(mode) {
1120
+ document.getElementById("tab-api").className = mode === 'api' ? "tab-pill active" : "tab-pill";
1121
+ document.getElementById("tab-direct").className = mode === 'direct' ? "tab-pill active" : "tab-pill";
1122
+ document.getElementById("section-api").style.display = mode === 'api' ? 'flex' : 'none';
1123
+ document.getElementById("section-direct").style.display = mode === 'direct' ? 'flex' : 'none';
1124
+ appConfig.backend_mode = mode;
1125
+ }
1126
+
1127
+ function onPresetChange() {
1128
+ const sel = document.getElementById("cfg-model-preset").value;
1129
+ if (sel !== "custom") {
1130
+ document.getElementById("cfg-model-path").value = sel;
1131
+ }
1132
+ }
1133
+
1134
+ function openSettings() {
1135
+ updateSettingsModalForm();
1136
+ document.getElementById("settings-modal").classList.add("open");
1137
+ }
1138
+
1139
+ function closeSettings() {
1140
+ document.getElementById("settings-modal").classList.remove("open");
1141
+ }
1142
+
1143
+ function setMaxTokensPreset(val) {
1144
+ document.getElementById("cfg-max-tokens").value = val;
1145
+ document.getElementById("max-val").innerText = val;
1146
+ }
1147
+
1148
+ function updateSettingsModalForm() {
1149
+ document.getElementById("cfg-api-url").value = appConfig.api_url || "http://127.0.0.1:18085";
1150
+ document.getElementById("cfg-model-path").value = appConfig.model_path || "";
1151
+ document.getElementById("cfg-ctx").value = appConfig.context_size || 81920;
1152
+ document.getElementById("cfg-ngl").value = appConfig.gpu_layers || 99;
1153
+
1154
+ const doSample = appConfig.do_sample !== false;
1155
+ document.getElementById("cfg-do-sample").checked = doSample;
1156
+
1157
+ const temp = appConfig.temperature ?? 1.0;
1158
+ document.getElementById("cfg-temp").value = temp;
1159
+ document.getElementById("temp-val").innerText = parseFloat(temp).toFixed(2);
1160
+
1161
+ const topP = appConfig.top_p ?? 0.95;
1162
+ document.getElementById("cfg-top-p").value = topP;
1163
+ document.getElementById("topp-val").innerText = parseFloat(topP).toFixed(2);
1164
+
1165
+ const topK = appConfig.top_k ?? 20;
1166
+ document.getElementById("cfg-top-k").value = topK;
1167
+ document.getElementById("topk-val").innerText = topK <= 0 ? "Off" : topK;
1168
+
1169
+ const minP = appConfig.min_p ?? 0.0;
1170
+ document.getElementById("cfg-min-p").value = minP;
1171
+ document.getElementById("minp-val").innerText = parseFloat(minP).toFixed(2);
1172
+
1173
+ const presPen = appConfig.presence_penalty ?? 1.5;
1174
+ document.getElementById("cfg-pres-penalty").value = presPen;
1175
+ document.getElementById("pres-val").innerText = parseFloat(presPen).toFixed(2);
1176
+
1177
+ const repPen = appConfig.repetition_penalty ?? 1.0;
1178
+ document.getElementById("cfg-rep-penalty").value = repPen;
1179
+ document.getElementById("rep-val").innerText = parseFloat(repPen).toFixed(2);
1180
+
1181
+ const maxTok = appConfig.max_tokens ?? 8192;
1182
+ document.getElementById("cfg-max-tokens").value = maxTok;
1183
+ document.getElementById("max-val").innerText = maxTok;
1184
+
1185
+ document.getElementById("cfg-system-prompt").value = appConfig.system_prompt || "";
1186
+
1187
+ const preset = appConfig.theme_preset || "default";
1188
+ document.querySelectorAll(".preset-btn").forEach(b => b.classList.remove("active"));
1189
+ const btn = document.getElementById("btn-theme-" + preset);
1190
+ if (btn) btn.classList.add("active");
1191
+ document.getElementById("cfg-custom-css").value = appConfig.custom_css || (preset !== 'default' ? THEME_PRESETS[preset] : "");
1192
+
1193
+ switchBackendTab(appConfig.backend_mode || 'api');
1194
+ }
1195
+
1196
+ async function saveSettings() {
1197
+ const payload = {
1198
+ backend_mode: appConfig.backend_mode,
1199
+ api_url: document.getElementById("cfg-api-url").value,
1200
+ do_sample: document.getElementById("cfg-do-sample").checked,
1201
+ temperature: parseFloat(document.getElementById("cfg-temp").value),
1202
+ top_p: parseFloat(document.getElementById("cfg-top-p").value),
1203
+ top_k: parseInt(document.getElementById("cfg-top-k").value),
1204
+ min_p: parseFloat(document.getElementById("cfg-min-p").value),
1205
+ presence_penalty: parseFloat(document.getElementById("cfg-pres-penalty").value),
1206
+ repetition_penalty: parseFloat(document.getElementById("cfg-rep-penalty").value),
1207
+ max_tokens: parseInt(document.getElementById("cfg-max-tokens").value),
1208
+ system_prompt: document.getElementById("cfg-system-prompt").value,
1209
+ enable_thinking: appConfig.enable_thinking,
1210
+ theme_preset: appConfig.theme_preset || "default",
1211
+ custom_css: document.getElementById("cfg-custom-css").value
1212
+ };
1213
+ await fetch("/api/config", {
1214
+ method: "POST",
1215
+ headers: { "Content-Type": "application/json" },
1216
+ body: JSON.stringify(payload)
1217
+ });
1218
+ closeSettings();
1219
+ await fetchConfig();
1220
+ await checkStatus();
1221
+ }
1222
+
1223
+ async function loadDirectModel() {
1224
+ const path = document.getElementById("cfg-model-path").value.trim();
1225
+ const ctx = parseInt(document.getElementById("cfg-ctx").value);
1226
+ const ngl = parseInt(document.getElementById("cfg-ngl").value);
1227
+ if (!path) {
1228
+ alert("Please enter a valid model path.");
1229
+ return;
1230
+ }
1231
+ const btn = event.target;
1232
+ const origText = btn.innerText;
1233
+ btn.innerText = "⏳ Loading Model...";
1234
+ btn.disabled = true;
1235
+
1236
+ try {
1237
+ const res = await fetch("/api/model/load", {
1238
+ method: "POST",
1239
+ headers: { "Content-Type": "application/json" },
1240
+ body: JSON.stringify({ model_path: path, context_size: ctx, gpu_layers: ngl })
1241
+ });
1242
+ const data = await res.json();
1243
+ if (res.ok) {
1244
+ alert(data.message || "Model loaded successfully!");
1245
+ closeSettings();
1246
+ await fetchConfig();
1247
+ await checkStatus();
1248
+ } else {
1249
+ alert("Error: " + (data.error || "Failed to load model."));
1250
+ }
1251
+ } catch (err) {
1252
+ alert("Error connecting to server: " + err.message);
1253
+ } finally {
1254
+ btn.innerText = origText;
1255
+ btn.disabled = false;
1256
+ }
1257
+ }
1258
+
1259
+ function clearChat() {
1260
+ conversationHistory = [];
1261
+ const list = document.getElementById("messages-list");
1262
+ list.innerHTML = `
1263
+ <div class="welcome-card" id="welcome-card">
1264
+ <div class="welcome-title">Qwen3.8-27B · Mario-HTML LoRA</div>
1265
+ <p class="welcome-desc">
1266
+ Qwen3.8-27B + QLoRA(r16,仅 attention 层)在 830 条马里奥 HTML 数据上 SFT。思维链保留、只改造正文输出,思考强度可在输入框左侧切换。
1267
+ </p>
1268
+ <div class="chips-grid">
1269
+ <div class="prompt-chip" onclick="sendPreset('Solve the linear equation: 2x + 5 = 17 step by step')">📐 2x + 5 = 17 Equation</div>
1270
+ <div class="prompt-chip" onclick="sendPreset('Explain Binary Search algorithm with complexity and steps')">⚡ Binary Search</div>
1271
+ <div class="prompt-chip" onclick="sendPreset('Compare 9.11 and 9.9 — which one is larger and why?')">⚖️ 9.11 vs 9.9</div>
1272
+ <div class="prompt-chip" onclick="sendPreset('Compare Microservices vs Monolith architecture in a modern comparison grid')">🏛️ Microservices vs Monolith</div>
1273
+ </div>
1274
+ </div>
1275
+ `;
1276
+ }
1277
+
1278
+ function sendPreset(text) {
1279
+ document.getElementById("prompt-input").value = text;
1280
+ sendMessage();
1281
+ }
1282
+
1283
+ function handleKeyDown(e) {
1284
+ if (e.key === "Enter" && !e.shiftKey) {
1285
+ e.preventDefault();
1286
+ sendMessage();
1287
+ }
1288
+ }
1289
+
1290
+ function autoResizeTextarea() {
1291
+ const tx = document.getElementById("prompt-input");
1292
+ tx.addEventListener("input", () => {
1293
+ tx.style.height = "auto";
1294
+ tx.style.height = Math.min(tx.scrollHeight, 180) + "px";
1295
+ });
1296
+ }
1297
+
1298
+ let attachedImageBase64 = null;
1299
+
1300
+ function handleImageFileSelect(e) {
1301
+ const file = e.target.files[0];
1302
+ if (file) loadImageFile(file);
1303
+ e.target.value = "";
1304
+ }
1305
+
1306
+ function loadImageFile(file) {
1307
+ if (!file || !file.type.startsWith("image/")) return;
1308
+ const reader = new FileReader();
1309
+ reader.onload = function(evt) {
1310
+ attachedImageBase64 = evt.target.result;
1311
+ document.getElementById("attached-img-thumb").src = attachedImageBase64;
1312
+ document.getElementById("attached-img-name").innerText = `${file.name || 'Image'} (${(file.size / 1024).toFixed(1)} KB)`;
1313
+ document.getElementById("image-preview-bar").style.display = "flex";
1314
+ };
1315
+ reader.readAsDataURL(file);
1316
+ }
1317
+
1318
+ function removeAttachedImage() {
1319
+ attachedImageBase64 = null;
1320
+ document.getElementById("image-preview-bar").style.display = "none";
1321
+ document.getElementById("attached-img-thumb").src = "";
1322
+ }
1323
+
1324
+ // Direct clipboard paste support
1325
+ window.addEventListener("paste", function(e) {
1326
+ const items = (e.clipboardData || window.clipboardData).items;
1327
+ for (let item of items) {
1328
+ if (item.type.indexOf("image") !== -1) {
1329
+ const file = item.getAsFile();
1330
+ loadImageFile(file);
1331
+ break;
1332
+ }
1333
+ }
1334
+ });
1335
+
1336
+ let activeAbortController = null;
1337
+
1338
+ function handleSendOrStop() {
1339
+ if (isGenerating) {
1340
+ stopGenerating();
1341
+ } else {
1342
+ sendMessage();
1343
+ }
1344
+ }
1345
+
1346
+ function stopGenerating() {
1347
+ if (activeAbortController) {
1348
+ try {
1349
+ activeAbortController.abort();
1350
+ } catch (_) {}
1351
+ activeAbortController = null;
1352
+ }
1353
+ isGenerating = false;
1354
+ const sendBtn = document.getElementById("send-btn");
1355
+ if (sendBtn) {
1356
+ sendBtn.innerText = "Send ↵";
1357
+ sendBtn.style.background = "var(--accent)";
1358
+ }
1359
+ }
1360
+
1361
+ async function sendMessage() {
1362
+ if (isGenerating) return;
1363
+ const input = document.getElementById("prompt-input");
1364
+ const text = input.value.trim();
1365
+ const currentImg = attachedImageBase64;
1366
+
1367
+ if (!text && !currentImg) return;
1368
+
1369
+ input.value = "";
1370
+ input.style.height = "44px";
1371
+ removeAttachedImage();
1372
+
1373
+ // Hide welcome card if present
1374
+ const welcome = document.getElementById("welcome-card");
1375
+ if (welcome) welcome.remove();
1376
+
1377
+ // Append user bubble
1378
+ appendUserMessage(text, currentImg);
1379
+
1380
+ if (currentImg) {
1381
+ conversationHistory.push({
1382
+ role: "user",
1383
+ content: [
1384
+ { type: "text", text: text || "Please analyze and describe this image." },
1385
+ { type: "image_url", image_url: { url: currentImg } }
1386
+ ]
1387
+ });
1388
+ } else {
1389
+ conversationHistory.push({ role: "user", content: text });
1390
+ }
1391
+
1392
+ // Create assistant container
1393
+ const { row, thinkingBody, htmlCanvas, rawSource, statsSpan } = createAssistantRow();
1394
+
1395
+ isGenerating = true;
1396
+ activeAbortController = new AbortController();
1397
+ const sendBtn = document.getElementById("send-btn");
1398
+ sendBtn.innerText = "Stop ⏹";
1399
+ sendBtn.style.background = "#ef4444";
1400
+
1401
+ let accumulatedThought = "";
1402
+ let accumulatedContent = "";
1403
+
1404
+ try {
1405
+ const response = await fetch("/api/chat/stream", {
1406
+ method: "POST",
1407
+ headers: { "Content-Type": "application/json" },
1408
+ signal: activeAbortController.signal,
1409
+ body: JSON.stringify({
1410
+ messages: conversationHistory,
1411
+ enable_thinking: appConfig.enable_thinking,
1412
+ reasoning_effort: appConfig.reasoning_effort || "low",
1413
+ do_sample: appConfig.do_sample,
1414
+ temperature: appConfig.temperature,
1415
+ top_p: appConfig.top_p,
1416
+ top_k: appConfig.top_k,
1417
+ min_p: appConfig.min_p,
1418
+ presence_penalty: appConfig.presence_penalty,
1419
+ repetition_penalty: appConfig.repetition_penalty,
1420
+ max_tokens: appConfig.max_tokens,
1421
+ system_prompt: appConfig.system_prompt
1422
+ })
1423
+ });
1424
+
1425
+ const reader = response.body.getReader();
1426
+ const decoder = new TextDecoder("utf-8");
1427
+ let streamBuffer = "";
1428
+
1429
+ while (true) {
1430
+ const { done, value } = await reader.read();
1431
+ if (done) break;
1432
+
1433
+ streamBuffer += decoder.decode(value, { stream: true });
1434
+ const lines = streamBuffer.split("\n");
1435
+ // Keep the last segment in streamBuffer (it might be truncated or empty)
1436
+ streamBuffer = lines.pop();
1437
+
1438
+ for (const line of lines) {
1439
+ const trimmed = line.trim();
1440
+ if (!trimmed.startsWith("data: ")) continue;
1441
+ const dataStr = trimmed.slice(6).trim();
1442
+ if (!dataStr) continue;
1443
+
1444
+ let packet;
1445
+ try {
1446
+ packet = JSON.parse(dataStr);
1447
+ } catch (jsonErr) {
1448
+ console.warn("Skipping incomplete/malformed SSE chunk:", jsonErr, dataStr);
1449
+ continue;
1450
+ }
1451
+
1452
+ if (packet.type === "thought") {
1453
+ accumulatedThought += packet.content;
1454
+ thinkingBody.innerText = accumulatedThought;
1455
+ scrollToBottom();
1456
+ } else if (packet.type === "content") {
1457
+ accumulatedContent += packet.content;
1458
+ if (accumulatedContent.includes("<think>")) {
1459
+ const tm = accumulatedContent.match(/<think>([\s\S]*?)(?:<\/think>|$)/i);
1460
+ if (tm && tm[1]) {
1461
+ thinkingBody.innerText = tm[1].trim();
1462
+ }
1463
+ }
1464
+ renderHtmlView(htmlCanvas, rawSource, accumulatedContent);
1465
+ scrollToBottom();
1466
+ } else if (packet.type === "done") {
1467
+ statsSpan.innerText = `⏱️ ${packet.elapsed}s | ⚡ ${packet.tok_per_sec} tok/s | 📊 ${packet.tokens} tokens`;
1468
+ conversationHistory.push({ role: "assistant", content: accumulatedContent });
1469
+ } else if (packet.type === "error") {
1470
+ htmlCanvas.innerHTML = `<div style="color:#ef4444; padding:12px;"><strong>Error:</strong> ${packet.error}</div>`;
1471
+ }
1472
+ }
1473
+ }
1474
+
1475
+ // Process any residual trailing line in streamBuffer
1476
+ if (streamBuffer.trim().startsWith("data: ")) {
1477
+ try {
1478
+ const packet = JSON.parse(streamBuffer.trim().slice(6).trim());
1479
+ if (packet.type === "done") {
1480
+ statsSpan.innerText = `⏱️ ${packet.elapsed}s | ⚡ ${packet.tok_per_sec} tok/s | 📊 ${packet.tokens} tokens`;
1481
+ conversationHistory.push({ role: "assistant", content: accumulatedContent });
1482
+ }
1483
+ } catch (_) {}
1484
+ }
1485
+
1486
+ } catch (err) {
1487
+ if (err.name === "AbortError" || String(err).includes("aborted")) {
1488
+ statsSpan.innerText = (statsSpan.innerText ? statsSpan.innerText + " | " : "") + "⏹ Stopped by user";
1489
+ conversationHistory.push({ role: "assistant", content: accumulatedContent });
1490
+ } else {
1491
+ htmlCanvas.innerHTML = `<div style="color:#ef4444; padding:12px;"><strong>Network Error:</strong> ${err.message}</div>`;
1492
+ }
1493
+ } finally {
1494
+ isGenerating = false;
1495
+ activeAbortController = null;
1496
+ const sendBtn = document.getElementById("send-btn");
1497
+ if (sendBtn) {
1498
+ sendBtn.innerText = "Send ↵";
1499
+ sendBtn.style.background = "var(--accent)";
1500
+ }
1501
+ }
1502
+ }
1503
+
1504
+ function appendUserMessage(text, imgSrc) {
1505
+ const list = document.getElementById("messages-list");
1506
+ const row = document.createElement("div");
1507
+ row.className = "message-row message-user";
1508
+ let imgHtml = "";
1509
+ if (imgSrc) {
1510
+ imgHtml = `<div style="margin-bottom:8px;"><img src="${imgSrc}" style="max-width:240px; max-height:180px; border-radius:8px; border:1px solid rgba(0,0,0,0.1); display:block; object-fit:cover;"></div>`;
1511
+ }
1512
+ row.innerHTML = `<div class="user-bubble">${imgHtml}${escapeHtml(text)}</div>`;
1513
+ list.appendChild(row);
1514
+ scrollToBottom();
1515
+ }
1516
+
1517
+ function createAssistantRow() {
1518
+ const list = document.getElementById("messages-list");
1519
+ const row = document.createElement("div");
1520
+ row.className = "message-row message-assistant";
1521
+
1522
+ const showThinking = appConfig.enable_thinking;
1523
+
1524
+ row.innerHTML = `
1525
+ <details class="thinking-box" ${showThinking ? 'open' : ''} style="${showThinking ? '' : 'display:none;'}">
1526
+ <summary class="thinking-summary">
1527
+ <span>🧠 Chain of Thought</span>
1528
+ <span style="margin-left:auto; font-size:0.75rem;">▼ Toggle</span>
1529
+ </summary>
1530
+ <div class="thinking-content">Thinking in progress...</div>
1531
+ </details>
1532
+ <div class="html-canvas">Generating visual presentation...</div>
1533
+ <div class="raw-source-view"></div>
1534
+ <div class="msg-footer">
1535
+ <div class="msg-stats"><span>Generating...</span></div>
1536
+ <div class="msg-actions">
1537
+ <button class="action-btn" onclick="toggleRawView(this)">👁️ View Source</button>
1538
+ <button class="action-btn" onclick="copyHtml(this)">📋 Copy HTML</button>
1539
+ </div>
1540
+ </div>
1541
+ `;
1542
+
1543
+ list.appendChild(row);
1544
+ scrollToBottom();
1545
+
1546
+ const thinkingBody = row.querySelector(".thinking-content");
1547
+ const htmlCanvas = row.querySelector(".html-canvas");
1548
+ const rawSource = row.querySelector(".raw-source-view");
1549
+ const statsSpan = row.querySelector(".msg-stats span");
1550
+
1551
+ return { row, thinkingBody, htmlCanvas, rawSource, statsSpan };
1552
+ }
1553
+
1554
+ function renderHtmlView(canvas, rawView, text) {
1555
+ // 1. Strip any thinking block from HTML canvas view
1556
+ let clean = text.replace(/<think>[\s\S]*?<\/think>/gi, "").trim();
1557
+ clean = clean.replace(/<think>[\s\S]*$/gi, "").trim();
1558
+
1559
+ // 2. Strip code fences if model ever outputs them
1560
+ if (clean.startsWith("```html")) clean = clean.slice(7);
1561
+ if (clean.startsWith("```")) clean = clean.slice(3);
1562
+ if (clean.endsWith("```")) clean = clean.slice(0, -3);
1563
+ clean = clean.trim();
1564
+
1565
+ const startIdx = clean.indexOf('<div class="ai-view">');
1566
+ if (startIdx !== -1) {
1567
+ clean = clean.slice(startIdx);
1568
+ } else if (!clean) {
1569
+ // If content is still in thinking phase
1570
+ canvas.innerHTML = '<div style="color:var(--text-sub); font-size:0.85rem; padding:16px 8px; display:flex; align-items:center; gap:8px;"><span>🧠</span><span>Thinking &amp; formatting UI layout...</span></div>';
1571
+ rawView.innerText = "";
1572
+ return;
1573
+ }
1574
+
1575
+ rawView.innerText = clean;
1576
+ canvas.innerHTML = clean;
1577
+ }
1578
+
1579
+ function toggleRawView(btn) {
1580
+ const row = btn.closest(".message-assistant");
1581
+ const canvas = row.querySelector(".html-canvas");
1582
+ const raw = row.querySelector(".raw-source-view");
1583
+ if (raw.style.display === "block") {
1584
+ raw.style.display = "none";
1585
+ canvas.style.display = "block";
1586
+ btn.innerText = "View Source";
1587
+ } else {
1588
+ raw.style.display = "block";
1589
+ canvas.style.display = "none";
1590
+ btn.innerText = "View Preview";
1591
+ }
1592
+ }
1593
+
1594
+ function copyHtml(btn) {
1595
+ const row = btn.closest(".message-assistant");
1596
+ const raw = row.querySelector(".raw-source-view").innerText;
1597
+ navigator.clipboard.writeText(raw);
1598
+ const orig = btn.innerText;
1599
+ btn.innerText = "Copied! ✓";
1600
+ setTimeout(() => btn.innerText = orig, 1500);
1601
+ }
1602
+
1603
+ function scrollToBottom() {
1604
+ const container = document.getElementById("chat-container");
1605
+ container.scrollTop = container.scrollHeight;
1606
+ }
1607
+
1608
+ function escapeHtml(str) {
1609
+ return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
1610
+ }
1611
+ </script>
1612
+ </body>
1613
+ </html>
webui/static/theme.css ADDED
@@ -0,0 +1,593 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ :root {
2
+ --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
3
+ --font-mono: "JetBrains Mono", "Fira Code", Menlo, Monaco, Consolas, monospace;
4
+
5
+ --bg-app: #f8fafc;
6
+ --bg-card: #ffffff;
7
+ --bg-subtle: #f1f5f9;
8
+
9
+ --border-subtle: rgba(226, 232, 240, 0.8);
10
+ --border-card: #e2e8f0;
11
+
12
+ --text-main: #0f172a;
13
+ --text-body: #334155;
14
+ --text-muted: #64748b;
15
+
16
+ --accent-color: #2563eb;
17
+ --accent-bg: #eff6ff;
18
+
19
+ --success-color: #059669;
20
+ --success-bg: #ecfdf5;
21
+ --success-border: #a7f3d0;
22
+
23
+ --warning-color: #d97706;
24
+ --warning-bg: #fffbeb;
25
+ --warning-border: #fde68a;
26
+
27
+ --danger-color: #dc2626;
28
+ --danger-bg: #fef2f2;
29
+ --danger-border: #fecaca;
30
+
31
+ --info-color: #0284c7;
32
+ --info-bg: #f0f9ff;
33
+ --info-border: #bae6fd;
34
+
35
+ --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
36
+ --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.06), 0 2px 4px -2px rgba(0, 0, 0, 0.05);
37
+ --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.08), 0 4px 6px -4px rgba(0, 0, 0, 0.04);
38
+ --radius-sm: 6px;
39
+ --radius-md: 10px;
40
+ --radius-lg: 14px;
41
+ }
42
+
43
+ body {
44
+ margin: 0;
45
+ padding: 24px;
46
+ background-color: var(--bg-app);
47
+ font-family: var(--font-sans);
48
+ color: var(--text-body);
49
+ line-height: 1.6;
50
+ -webkit-font-smoothing: antialiased;
51
+ }
52
+
53
+ .ai-view {
54
+ max-width: 880px;
55
+ margin: 0 auto;
56
+ display: flex;
57
+ flex-direction: column;
58
+ gap: 20px;
59
+ }
60
+
61
+ /* Card Foundation */
62
+ .card {
63
+ background: var(--bg-card);
64
+ border: 1px solid var(--border-card);
65
+ border-radius: var(--radius-lg);
66
+ padding: 20px 24px;
67
+ box-shadow: var(--shadow-sm);
68
+ transition: box-shadow 0.2s ease, border-color 0.2s ease;
69
+ }
70
+
71
+ /* Hero / TL;DR Summary Card */
72
+ .summary-card {
73
+ background: linear-gradient(135deg, #ffffff 0%, #f8fafc 100%);
74
+ border: 1px solid var(--border-card);
75
+ border-left: 5px solid var(--accent-color);
76
+ border-radius: var(--radius-lg);
77
+ padding: 20px 24px;
78
+ box-shadow: var(--shadow-md);
79
+ }
80
+
81
+ .summary-card .lead {
82
+ font-size: 1.05rem;
83
+ font-weight: 500;
84
+ color: var(--text-main);
85
+ margin: 10px 0 0 0;
86
+ line-height: 1.6;
87
+ }
88
+
89
+ /* Badges & Pills */
90
+ .badge {
91
+ display: inline-flex;
92
+ align-items: center;
93
+ gap: 5px;
94
+ font-size: 0.75rem;
95
+ font-weight: 700;
96
+ text-transform: uppercase;
97
+ letter-spacing: 0.05em;
98
+ padding: 3px 9px;
99
+ border-radius: 9999px;
100
+ }
101
+ .badge-accent { background: var(--accent-bg); color: var(--accent-color); }
102
+ .badge-success { background: var(--success-bg); color: var(--success-color); }
103
+ .badge-warning { background: var(--warning-bg); color: var(--warning-color); }
104
+ .badge-danger { background: var(--danger-bg); color: var(--danger-color); }
105
+ .badge-info { background: var(--info-bg); color: var(--info-color); }
106
+
107
+ /* Typography */
108
+ h1, h2, h3, h4 {
109
+ color: var(--text-main);
110
+ margin-top: 0;
111
+ font-weight: 700;
112
+ letter-spacing: -0.02em;
113
+ }
114
+ h2 { font-size: 1.3rem; margin-bottom: 12px; }
115
+ h3 { font-size: 1.1rem; margin-bottom: 8px; }
116
+ p { margin: 0 0 10px 0; }
117
+ p:last-child { margin-bottom: 0; }
118
+
119
+ /* Grid Layouts (Bento) */
120
+ .grid {
121
+ display: grid;
122
+ gap: 16px;
123
+ }
124
+ .grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
125
+ .grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
126
+ @media (max-width: 680px) {
127
+ .grid-2, .grid-3 { grid-template-columns: 1fr; }
128
+ }
129
+
130
+ /* Callout Blocks */
131
+ .callout {
132
+ border-radius: var(--radius-md);
133
+ padding: 16px 20px;
134
+ border-left: 4px solid;
135
+ margin: 4px 0;
136
+ font-size: 0.95rem;
137
+ }
138
+ .callout-header {
139
+ font-weight: 700;
140
+ display: flex;
141
+ align-items: center;
142
+ gap: 8px;
143
+ margin-bottom: 6px;
144
+ }
145
+ .callout-info {
146
+ background-color: var(--info-bg);
147
+ border-color: var(--info-color);
148
+ color: #0369a1;
149
+ }
150
+ .callout-warning {
151
+ background-color: var(--warning-bg);
152
+ border-color: var(--warning-color);
153
+ color: #92400e;
154
+ }
155
+ .callout-danger {
156
+ background-color: var(--danger-bg);
157
+ border-color: var(--danger-color);
158
+ color: #991b1b;
159
+ }
160
+ .callout-success {
161
+ background-color: var(--success-bg);
162
+ border-color: var(--success-color);
163
+ color: #065f46;
164
+ }
165
+
166
+ /* Stepper / Timeline */
167
+ .stepper {
168
+ display: flex;
169
+ flex-direction: column;
170
+ gap: 16px;
171
+ position: relative;
172
+ margin: 6px 0;
173
+ }
174
+ .step-item {
175
+ display: flex;
176
+ gap: 16px;
177
+ align-items: flex-start;
178
+ }
179
+ .step-num {
180
+ flex-shrink: 0;
181
+ width: 32px;
182
+ height: 32px;
183
+ border-radius: 50%;
184
+ background: var(--accent-bg);
185
+ color: var(--accent-color);
186
+ border: 2px solid var(--accent-color);
187
+ display: flex;
188
+ align-items: center;
189
+ justify-content: center;
190
+ font-weight: 700;
191
+ font-size: 0.85rem;
192
+ }
193
+ .step-content {
194
+ flex-grow: 1;
195
+ }
196
+ .step-content h4 {
197
+ margin: 4px 0 6px 0;
198
+ font-size: 1rem;
199
+ }
200
+
201
+ /* Code Container (macOS style window) */
202
+ .code-box {
203
+ background: #0f172a;
204
+ border-radius: var(--radius-md);
205
+ overflow: hidden;
206
+ box-shadow: var(--shadow-sm);
207
+ margin: 8px 0;
208
+ }
209
+ .code-header {
210
+ background: #1e293b;
211
+ padding: 8px 14px;
212
+ display: flex;
213
+ align-items: center;
214
+ justify-content: space-between;
215
+ border-bottom: 1px solid rgba(255, 255, 255, 0.05);
216
+ }
217
+ .code-dots {
218
+ display: flex;
219
+ gap: 6px;
220
+ }
221
+ .code-dots i {
222
+ width: 10px;
223
+ height: 10px;
224
+ border-radius: 50%;
225
+ display: inline-block;
226
+ }
227
+ .code-dots i:nth-child(1) { background: #ef4444; }
228
+ .code-dots i:nth-child(2) { background: #f59e0b; }
229
+ .code-dots i:nth-child(3) { background: #10b981; }
230
+ .code-title {
231
+ color: #94a3b8;
232
+ font-family: var(--font-mono);
233
+ font-size: 0.75rem;
234
+ font-weight: 600;
235
+ text-transform: uppercase;
236
+ }
237
+ .code-box pre {
238
+ margin: 0;
239
+ padding: 14px 16px;
240
+ overflow-x: auto;
241
+ font-family: var(--font-mono);
242
+ font-size: 0.88rem;
243
+ line-height: 1.5;
244
+ color: #f1f5f9;
245
+ }
246
+ code {
247
+ font-family: var(--font-mono);
248
+ font-size: 0.88em;
249
+ background: #e2e8f0;
250
+ color: #0f172a;
251
+ padding: 2px 6px;
252
+ border-radius: 4px;
253
+ }
254
+ .code-box code {
255
+ background: transparent;
256
+ padding: 0;
257
+ color: inherit;
258
+ }
259
+
260
+ /* Metric / KPI Cards */
261
+ .metric-group {
262
+ display: grid;
263
+ grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
264
+ gap: 12px;
265
+ }
266
+ .metric-card {
267
+ background: var(--bg-card);
268
+ border: 1px solid var(--border-card);
269
+ border-radius: var(--radius-md);
270
+ padding: 14px 18px;
271
+ display: flex;
272
+ flex-direction: column;
273
+ }
274
+ .metric-label {
275
+ font-size: 0.8rem;
276
+ color: var(--text-muted);
277
+ font-weight: 600;
278
+ text-transform: uppercase;
279
+ letter-spacing: 0.04em;
280
+ }
281
+ .metric-value {
282
+ font-size: 1.6rem;
283
+ font-weight: 800;
284
+ color: var(--text-main);
285
+ letter-spacing: -0.03em;
286
+ margin: 4px 0;
287
+ }
288
+ .metric-sub {
289
+ font-size: 0.78rem;
290
+ color: var(--text-muted);
291
+ }
292
+
293
+ /* Modern Minimalist Table */
294
+ .table-container {
295
+ overflow-x: auto;
296
+ border: 1px solid var(--border-card);
297
+ border-radius: var(--radius-md);
298
+ margin: 8px 0;
299
+ background: var(--bg-card);
300
+ }
301
+ .modern-table {
302
+ width: 100%;
303
+ border-collapse: collapse;
304
+ text-align: left;
305
+ font-size: 0.92rem;
306
+ }
307
+ .modern-table th {
308
+ background: var(--bg-subtle);
309
+ color: var(--text-muted);
310
+ font-weight: 700;
311
+ font-size: 0.78rem;
312
+ text-transform: uppercase;
313
+ letter-spacing: 0.05em;
314
+ padding: 10px 14px;
315
+ border-bottom: 1px solid var(--border-card);
316
+ }
317
+ .modern-table td {
318
+ padding: 12px 14px;
319
+ border-bottom: 1px solid var(--border-subtle);
320
+ color: var(--text-body);
321
+ }
322
+ .modern-table tr:last-child td {
323
+ border-bottom: none;
324
+ }
325
+ .modern-table tr:hover td {
326
+ background: #f8fafc;
327
+ }
328
+ /* Append Thinking Card component to theme.css */
329
+ .thinking-card {
330
+ background: var(--bg-subtle);
331
+ border: 1px dashed var(--border-card);
332
+ border-radius: var(--radius-lg);
333
+ padding: 12px 18px;
334
+ margin-bottom: 8px;
335
+ transition: all 0.2s ease;
336
+ }
337
+ .thinking-card[open] {
338
+ background: var(--bg-card);
339
+ border-style: solid;
340
+ border-color: #cbd5e1;
341
+ box-shadow: var(--shadow-sm);
342
+ }
343
+ body.dark-mode .thinking-card {
344
+ border-color: #374151;
345
+ background: #11182788;
346
+ }
347
+ body.dark-mode .thinking-card[open] {
348
+ background: #1f2937;
349
+ border-color: #4b5563;
350
+ }
351
+ .thinking-header {
352
+ display: flex;
353
+ align-items: center;
354
+ gap: 10px;
355
+ cursor: pointer;
356
+ user-select: none;
357
+ font-size: 0.88rem;
358
+ font-weight: 700;
359
+ color: var(--text-muted);
360
+ list-style: none; /* Hide default triangle */
361
+ }
362
+ .thinking-header::-webkit-details-marker {
363
+ display: none;
364
+ }
365
+ .thinking-icon {
366
+ font-size: 1.1rem;
367
+ }
368
+ .thinking-title {
369
+ color: var(--text-main);
370
+ flex-grow: 1;
371
+ }
372
+ .thinking-badge {
373
+ font-size: 0.72rem;
374
+ background: #ede9fe;
375
+ color: #6d28d9;
376
+ padding: 2px 8px;
377
+ border-radius: 9999px;
378
+ font-weight: 700;
379
+ }
380
+ body.dark-mode .thinking-badge {
381
+ background: #4c1d9555;
382
+ color: #c4b5fd;
383
+ }
384
+ .thinking-body {
385
+ margin-top: 14px;
386
+ padding-top: 12px;
387
+ border-top: 1px solid var(--border-subtle);
388
+ font-size: 0.88rem;
389
+ line-height: 1.6;
390
+ color: var(--text-muted);
391
+ font-family: var(--font-sans);
392
+ }
393
+ .thinking-step {
394
+ margin-bottom: 8px;
395
+ }
396
+ .thinking-step strong {
397
+ color: var(--text-main);
398
+ }
399
+ .thinking-math {
400
+ font-family: var(--font-mono);
401
+ background: var(--bg-subtle);
402
+ padding: 2px 6px;
403
+ border-radius: 4px;
404
+ color: var(--text-main);
405
+ }
406
+
407
+ /* Aliases for thinking and summary classes */
408
+ .thinking {
409
+ background: var(--bg-subtle);
410
+ border: 1px dashed var(--border-card);
411
+ border-radius: var(--radius-lg);
412
+ padding: 12px 18px;
413
+ margin-bottom: 12px;
414
+ transition: all 0.2s ease;
415
+ }
416
+ .thinking[open] {
417
+ background: var(--bg-card);
418
+ border-style: solid;
419
+ border-color: #cbd5e1;
420
+ box-shadow: var(--shadow-sm);
421
+ }
422
+ body.dark-mode .thinking {
423
+ border-color: #374151;
424
+ background: #11182788;
425
+ }
426
+ body.dark-mode .thinking[open] {
427
+ background: #1f2937;
428
+ border-color: #4b5563;
429
+ }
430
+
431
+ .summary {
432
+ background: var(--bg-card);
433
+ border: 1px solid var(--border-card);
434
+ border-left: 5px solid var(--accent-color);
435
+ border-radius: var(--radius-lg);
436
+ padding: 20px 24px;
437
+ box-shadow: var(--shadow-md);
438
+ margin-top: 10px;
439
+ }
440
+ .summary .lead {
441
+ font-size: 1.05rem;
442
+ font-weight: 500;
443
+ color: var(--text-main);
444
+ margin: 10px 0 0 0;
445
+ line-height: 1.6;
446
+ }
447
+
448
+ /* Modern List System (Kills the Markdown / 90s bullet look) */
449
+ .ai-view ul {
450
+ list-style: none !important;
451
+ padding-left: 0 !important;
452
+ margin: 12px 0 !important;
453
+ display: flex !important;
454
+ flex-direction: column !important;
455
+ gap: 8px !important;
456
+ }
457
+
458
+ .ai-view ul li {
459
+ position: relative !important;
460
+ padding: 8px 14px 8px 30px !important;
461
+ background: rgba(0, 0, 0, 0.02) !important;
462
+ border-radius: var(--radius-sm, 8px) !important;
463
+ border: 1px solid rgba(0, 0, 0, 0.05) !important;
464
+ color: var(--text-body) !important;
465
+ line-height: 1.5 !important;
466
+ font-size: 0.92rem !important;
467
+ transition: all 0.15s ease !important;
468
+ }
469
+
470
+ .ai-view ul li:hover {
471
+ background: rgba(37, 99, 235, 0.04) !important;
472
+ border-color: rgba(37, 99, 235, 0.15) !important;
473
+ }
474
+
475
+ .ai-view ul li::before {
476
+ content: "" !important;
477
+ position: absolute !important;
478
+ left: 12px !important;
479
+ top: 14px !important;
480
+ width: 7px !important;
481
+ height: 7px !important;
482
+ border-radius: 50% !important;
483
+ background: var(--accent-color, #2563eb) !important;
484
+ box-shadow: 0 0 0 3px var(--accent-bg, rgba(37, 99, 235, 0.15)) !important;
485
+ }
486
+
487
+ /* Grid Child Auto-Cardify: if model outputs bare <div> inside .grid, give it card polish */
488
+ .grid > div:not(.card) {
489
+ background: #f8fafc !important;
490
+ border: 1px solid var(--border-card, #e2e8f0) !important;
491
+ border-radius: var(--radius-md, 12px) !important;
492
+ padding: 18px 20px !important;
493
+ box-shadow: 0 1px 3px rgba(0, 0, 0, 0.02) !important;
494
+ }
495
+
496
+ .grid > div:not(.card) h3 {
497
+ margin-bottom: 12px !important;
498
+ padding-bottom: 6px !important;
499
+ border-bottom: 1px solid rgba(0, 0, 0, 0.06) !important;
500
+ }
501
+
502
+ /* Auto-span single child in grid (Prevents blank ghost columns when model outputs only 1 child) */
503
+ .grid > :only-child,
504
+ .grid-2 > :only-child,
505
+ .grid-3 > :only-child {
506
+ grid-column: 1 / -1 !important;
507
+ }
508
+
509
+ /* Grid Child Container Safety: Never allow child to burst out of grid track */
510
+ .grid > div,
511
+ .grid-2 > div,
512
+ .grid-3 > div {
513
+ min-width: 0 !important;
514
+ box-sizing: border-box !important;
515
+ }
516
+
517
+ /* Feature List & Item (Upgraded from old-school bullet points; clean vertical card flow) */
518
+ .feature-list {
519
+ display: flex !important;
520
+ flex-direction: column !important;
521
+ gap: 8px !important;
522
+ margin: 10px 0 !important;
523
+ width: 100% !important;
524
+ box-sizing: border-box !important;
525
+ }
526
+
527
+ .feature-item {
528
+ display: flex !important;
529
+ flex-direction: column !important;
530
+ align-items: flex-start !important;
531
+ gap: 6px !important;
532
+ padding: 10px 14px !important;
533
+ background: #ffffff !important;
534
+ border-radius: var(--radius-sm, 8px) !important;
535
+ border: 1px solid var(--border-card, #e2e8f0) !important;
536
+ box-shadow: 0 1px 2px rgba(0, 0, 0, 0.02) !important;
537
+ transition: all 0.15s ease !important;
538
+ width: 100% !important;
539
+ box-sizing: border-box !important;
540
+ }
541
+
542
+ .feature-item:hover {
543
+ border-color: #cbd5e1 !important;
544
+ box-shadow: 0 2px 5px rgba(0, 0, 0, 0.04) !important;
545
+ }
546
+
547
+ .feature-item .badge {
548
+ font-size: 0.72rem !important;
549
+ padding: 2px 8px !important;
550
+ line-height: 1.4 !important;
551
+ display: inline-flex !important;
552
+ }
553
+
554
+ .feature-item p {
555
+ margin: 0 !important;
556
+ font-size: 0.88rem !important;
557
+ color: var(--text-body, #334155) !important;
558
+ line-height: 1.5 !important;
559
+ width: 100% !important;
560
+ }
561
+
562
+ .feature-item:hover {
563
+ border-color: #cbd5e1 !important;
564
+ box-shadow: 0 2px 5px rgba(0, 0, 0, 0.04) !important;
565
+ transform: translateX(2px) !important;
566
+ }
567
+
568
+ .feature-item .badge {
569
+ flex-shrink: 0 !important;
570
+ margin-top: 1px !important;
571
+ font-size: 0.75rem !important;
572
+ }
573
+
574
+ .feature-item p {
575
+ margin: 0 !important;
576
+ font-size: 0.92rem !important;
577
+ color: var(--text-body, #334155) !important;
578
+ line-height: 1.5 !important;
579
+ flex-grow: 1 !important;
580
+ }
581
+
582
+ answer {
583
+ display: inline-block;
584
+ font-family: var(--font-mono);
585
+ font-weight: 800;
586
+ font-size: 1.15rem;
587
+ color: var(--accent-color);
588
+ background: var(--accent-bg);
589
+ border: 1px solid rgba(37, 99, 235, 0.25);
590
+ padding: 2px 10px;
591
+ border-radius: 6px;
592
+ letter-spacing: -0.01em;
593
+ }