langquantof commited on
Commit
6033cd4
·
verified ·
1 Parent(s): 6bab5aa

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* 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
 
 
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
36
+ tokenizer.json filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,3 +1,120 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ base_model: Qwen/Qwen3-VL-4B-Instruct
3
+ library_name: peft
4
+ pipeline_tag: image-text-to-text
5
+ license: apache-2.0
6
+ language:
7
+ - en
8
+ tags:
9
+ - lora
10
+ - sft
11
+ - peft
12
+ - bitcoin
13
+ - trading
14
+ - chart-analysis
15
+ - qwen3-vl
16
+ ---
17
+
18
+ # LQ-Qwen3-VL-4B-ChartSignal
19
+
20
+ BTC 15분봉 차트 이미지를 분석하여 매매 신호와 상세한 분석 근거를 JSON으로 출력하는 Qwen3-VL-4B LoRA adapter.
21
+
22
+ - **Base Model**: [Qwen/Qwen3-VL-4B-Instruct](https://huggingface.co/Qwen/Qwen3-VL-4B-Instruct)
23
+ - **Tutorial**: [Qwen3-VL-SFT-RL-Tutorial](https://github.com/taegyeong-lee/Qwen3-VL-SFT-RL-Tutorial)
24
+
25
+ ## Quick Start
26
+
27
+ ```python
28
+ import torch
29
+ from transformers import Qwen3VLForConditionalGeneration, AutoProcessor
30
+ from peft import PeftModel
31
+
32
+ # Load model
33
+ base_model = "Qwen/Qwen3-VL-4B-Instruct"
34
+ processor = AutoProcessor.from_pretrained(base_model)
35
+ model = Qwen3VLForConditionalGeneration.from_pretrained(
36
+ base_model, torch_dtype=torch.bfloat16, device_map="auto",
37
+ )
38
+ model = PeftModel.from_pretrained(model, "LangQuant/LQ-Qwen3-VL-4B-ChartSignal")
39
+ model.eval()
40
+
41
+ # Predict
42
+ messages = [
43
+ {"role": "system", "content": "You are a professional Bitcoin futures trader. Analyze 15-minute candlestick charts to predict the direction over the next 4 hours."},
44
+ {"role": "user", "content": [
45
+ {"type": "image", "image": "chart.png"},
46
+ {"type": "text", "text": "BTCUSDT 15m chart. Predict the direction for the next 4 hours (16 candles).\nRespond in JSON."},
47
+ ]},
48
+ ]
49
+ inputs = processor.apply_chat_template(
50
+ messages, tokenize=True, add_generation_prompt=True,
51
+ return_dict=True, return_tensors="pt",
52
+ ).to(model.device)
53
+
54
+ with torch.no_grad():
55
+ generated_ids = model.generate(**inputs, max_new_tokens=512)
56
+
57
+ trimmed = [out[len(inp):] for inp, out in zip(inputs.input_ids, generated_ids)]
58
+ print(processor.batch_decode(trimmed, skip_special_tokens=True)[0])
59
+ ```
60
+
61
+ ## Example
62
+
63
+ **Input:**
64
+
65
+ ![example](example.png)
66
+
67
+ **Output:**
68
+
69
+ ```json
70
+ {
71
+ "signal": "NEUTRAL",
72
+ "confidence": 75,
73
+ "stop_loss_pct": -0.5,
74
+ "take_profit_pct": 0.5,
75
+ "reasoning": {
76
+ "market_context": "The price is consolidating around a narrow range near $89,000 to $90,000, with no clear trend established over the 16 candles.",
77
+ "price_action": "Candlesticks show alternating small-bodied bullish and bearish candles without strong directional wicks, indicating indecision and sideways movement.",
78
+ "volume_oi": "Volume is low and stable, not supporting strong buying or selling pressure. Open Interest shows a slight upward trend but remains relatively flat, suggesting no significant new positions being opened to drive direction.",
79
+ "risk_assessment": "The main risk is a breakout in either direction, which could lead to a move beyond the consolidation range. However, the lack of strong momentum and volume reduces the probability of a sustained move."
80
+ },
81
+ "risk_level": "MEDIUM"
82
+ }
83
+ ```
84
+
85
+ ## Evaluation (Best: checkpoint-200)
86
+
87
+ 테스트셋 600개 (LONG 214 / SHORT 259 / NEUTRAL 127)
88
+
89
+ **Accuracy: 42.7% | Macro F1: 42.4%**
90
+
91
+ ### Confusion Matrix
92
+
93
+ ![Confusion Matrix](confusion_matrix.png)
94
+
95
+ | | Pred LONG | Pred SHORT | Pred NEUTRAL | Recall |
96
+ |---|---|---|---|---|
97
+ | **LONG** | 71 | 60 | 83 | 33.2% |
98
+ | **SHORT** | 55 | 102 | 102 | 39.4% |
99
+ | **NEUTRAL** | 16 | 28 | 83 | 65.4% |
100
+
101
+ ### Classification Report
102
+
103
+ | Class | Precision | Recall | F1 | Support |
104
+ |---|---|---|---|---|
105
+ | LONG | 50.0% | 33.2% | 39.9% | 214 |
106
+ | SHORT | 53.7% | 39.4% | 45.4% | 259 |
107
+ | NEUTRAL | 31.0% | 65.4% | 42.0% | 127 |
108
+ | **Macro Avg** | 44.9% | 46.0% | 42.4% | |
109
+
110
+ ### F1 by Class & Checkpoint
111
+
112
+ ![F1 Comparison](f1_comparison.png)
113
+
114
+ ## Input
115
+
116
+ BTC/USDT 15분봉 차트 이미지 (24시간 분량). 차트에는 캔들스틱, 거래량, 펀딩비, 미결제약정이 포함됩니다.
117
+
118
+ ## Disclaimer
119
+
120
+ 이 모델은 연구 및 교육 목적으로 만들어졌습니다. 실제 매매에 사용하여 발생하는 손실에 대해 책임지지 않습니다.
adapter_config.json ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "alora_invocation_tokens": null,
3
+ "alpha_pattern": {},
4
+ "arrow_config": null,
5
+ "auto_mapping": null,
6
+ "base_model_name_or_path": "Qwen/Qwen3-VL-4B-Instruct",
7
+ "bias": "none",
8
+ "corda_config": null,
9
+ "ensure_weight_tying": false,
10
+ "eva_config": null,
11
+ "exclude_modules": null,
12
+ "fan_in_fan_out": false,
13
+ "inference_mode": true,
14
+ "init_lora_weights": true,
15
+ "layer_replication": null,
16
+ "layers_pattern": null,
17
+ "layers_to_transform": null,
18
+ "loftq_config": {},
19
+ "lora_alpha": 32,
20
+ "lora_bias": false,
21
+ "lora_dropout": 0.05,
22
+ "megatron_config": null,
23
+ "megatron_core": "megatron.core",
24
+ "modules_to_save": null,
25
+ "peft_type": "LORA",
26
+ "peft_version": "0.18.1",
27
+ "qalora_group_size": 16,
28
+ "r": 16,
29
+ "rank_pattern": {},
30
+ "revision": null,
31
+ "target_modules": [
32
+ "q_proj",
33
+ "gate_proj",
34
+ "up_proj",
35
+ "o_proj",
36
+ "k_proj",
37
+ "v_proj",
38
+ "down_proj"
39
+ ],
40
+ "target_parameters": null,
41
+ "task_type": "CAUSAL_LM",
42
+ "trainable_token_indices": null,
43
+ "use_dora": false,
44
+ "use_qalora": false,
45
+ "use_rslora": false
46
+ }
adapter_model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f59b18403253214bbfb94dafe3aa12a44eb34e29a69c531c39d9f018bad29c34
3
+ size 132195448
chat_template.jinja ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- if tools %}
2
+ {{- '<|im_start|>system\n' }}
3
+ {%- if messages[0].role == 'system' %}
4
+ {%- if messages[0].content is string %}
5
+ {{- messages[0].content }}
6
+ {%- else %}
7
+ {%- for content in messages[0].content %}
8
+ {%- if 'text' in content %}
9
+ {{- content.text }}
10
+ {%- endif %}
11
+ {%- endfor %}
12
+ {%- endif %}
13
+ {{- '\n\n' }}
14
+ {%- endif %}
15
+ {{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
16
+ {%- for tool in tools %}
17
+ {{- "\n" }}
18
+ {{- tool | tojson }}
19
+ {%- endfor %}
20
+ {{- "\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call><|im_end|>\n" }}
21
+ {%- else %}
22
+ {%- if messages[0].role == 'system' %}
23
+ {{- '<|im_start|>system\n' }}
24
+ {%- if messages[0].content is string %}
25
+ {{- messages[0].content }}
26
+ {%- else %}
27
+ {%- for content in messages[0].content %}
28
+ {%- if 'text' in content %}
29
+ {{- content.text }}
30
+ {%- endif %}
31
+ {%- endfor %}
32
+ {%- endif %}
33
+ {{- '<|im_end|>\n' }}
34
+ {%- endif %}
35
+ {%- endif %}
36
+ {%- set image_count = namespace(value=0) %}
37
+ {%- set video_count = namespace(value=0) %}
38
+ {%- for message in messages %}
39
+ {%- if message.role == "user" %}
40
+ {{- '<|im_start|>' + message.role + '\n' }}
41
+ {%- if message.content is string %}
42
+ {{- message.content }}
43
+ {%- else %}
44
+ {%- for content in message.content %}
45
+ {%- if content.type == 'image' or 'image' in content or 'image_url' in content %}
46
+ {%- set image_count.value = image_count.value + 1 %}
47
+ {%- if add_vision_id %}Picture {{ image_count.value }}: {% endif -%}
48
+ <|vision_start|><|image_pad|><|vision_end|>
49
+ {%- elif content.type == 'video' or 'video' in content %}
50
+ {%- set video_count.value = video_count.value + 1 %}
51
+ {%- if add_vision_id %}Video {{ video_count.value }}: {% endif -%}
52
+ <|vision_start|><|video_pad|><|vision_end|>
53
+ {%- elif 'text' in content %}
54
+ {{- content.text }}
55
+ {%- endif %}
56
+ {%- endfor %}
57
+ {%- endif %}
58
+ {{- '<|im_end|>\n' }}
59
+ {%- elif message.role == "assistant" %}
60
+ {{- '<|im_start|>' + message.role + '\n' }}
61
+ {%- if message.content is string %}
62
+ {{- message.content }}
63
+ {%- else %}
64
+ {%- for content_item in message.content %}
65
+ {%- if 'text' in content_item %}
66
+ {{- content_item.text }}
67
+ {%- endif %}
68
+ {%- endfor %}
69
+ {%- endif %}
70
+ {%- if message.tool_calls %}
71
+ {%- for tool_call in message.tool_calls %}
72
+ {%- if (loop.first and message.content) or (not loop.first) %}
73
+ {{- '\n' }}
74
+ {%- endif %}
75
+ {%- if tool_call.function %}
76
+ {%- set tool_call = tool_call.function %}
77
+ {%- endif %}
78
+ {{- '<tool_call>\n{"name": "' }}
79
+ {{- tool_call.name }}
80
+ {{- '", "arguments": ' }}
81
+ {%- if tool_call.arguments is string %}
82
+ {{- tool_call.arguments }}
83
+ {%- else %}
84
+ {{- tool_call.arguments | tojson }}
85
+ {%- endif %}
86
+ {{- '}\n</tool_call>' }}
87
+ {%- endfor %}
88
+ {%- endif %}
89
+ {{- '<|im_end|>\n' }}
90
+ {%- elif message.role == "tool" %}
91
+ {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
92
+ {{- '<|im_start|>user' }}
93
+ {%- endif %}
94
+ {{- '\n<tool_response>\n' }}
95
+ {%- if message.content is string %}
96
+ {{- message.content }}
97
+ {%- else %}
98
+ {%- for content in message.content %}
99
+ {%- if content.type == 'image' or 'image' in content or 'image_url' in content %}
100
+ {%- set image_count.value = image_count.value + 1 %}
101
+ {%- if add_vision_id %}Picture {{ image_count.value }}: {% endif -%}
102
+ <|vision_start|><|image_pad|><|vision_end|>
103
+ {%- elif content.type == 'video' or 'video' in content %}
104
+ {%- set video_count.value = video_count.value + 1 %}
105
+ {%- if add_vision_id %}Video {{ video_count.value }}: {% endif -%}
106
+ <|vision_start|><|video_pad|><|vision_end|>
107
+ {%- elif 'text' in content %}
108
+ {{- content.text }}
109
+ {%- endif %}
110
+ {%- endfor %}
111
+ {%- endif %}
112
+ {{- '\n</tool_response>' }}
113
+ {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
114
+ {{- '<|im_end|>\n' }}
115
+ {%- endif %}
116
+ {%- endif %}
117
+ {%- endfor %}
118
+ {%- if add_generation_prompt %}
119
+ {{- '<|im_start|>assistant\n' }}
120
+ {%- endif %}
confusion_matrix.png ADDED
example.png ADDED
f1_comparison.png ADDED
predict.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ BTC Chart Trading Signal Prediction with Qwen3-VL-4B LoRA.
3
+
4
+ Usage:
5
+ python predict.py --image chart.png
6
+ python predict.py --adapter ./checkpoint-200 --image chart.png
7
+ python predict.py --adapter LangQuant/LQ-Qwen3-VL-4B-ChartSignal --image chart.png
8
+ """
9
+
10
+ import json
11
+ import argparse
12
+
13
+ import torch
14
+ from transformers import Qwen3VLForConditionalGeneration, AutoProcessor
15
+ from peft import PeftModel
16
+
17
+ BASE_MODEL = "Qwen/Qwen3-VL-4B-Instruct"
18
+
19
+ SYSTEM_PROMPT = (
20
+ "You are a professional Bitcoin futures trader. "
21
+ "Analyze 15-minute candlestick charts to predict the direction over the next 4 hours."
22
+ )
23
+ USER_PROMPT = (
24
+ "BTCUSDT 15m chart. Predict the direction for the next 4 hours (16 candles).\n"
25
+ "Respond in JSON."
26
+ )
27
+
28
+
29
+ def load_model(adapter_path: str, base_model: str = BASE_MODEL):
30
+ """Load base model + LoRA adapter."""
31
+ print(f"Loading model: {base_model}")
32
+ processor = AutoProcessor.from_pretrained(base_model)
33
+ model = Qwen3VLForConditionalGeneration.from_pretrained(
34
+ base_model,
35
+ torch_dtype=torch.bfloat16,
36
+ device_map="auto",
37
+ )
38
+
39
+ print(f"Loading LoRA adapter: {adapter_path}")
40
+ model = PeftModel.from_pretrained(model, adapter_path)
41
+ model.eval()
42
+ return model, processor
43
+
44
+
45
+ def predict(model, processor, image_path: str) -> dict:
46
+ """Run inference on a single chart image."""
47
+ messages = [
48
+ {"role": "system", "content": SYSTEM_PROMPT},
49
+ {"role": "user", "content": [
50
+ {"type": "image", "image": image_path},
51
+ {"type": "text", "text": USER_PROMPT},
52
+ ]},
53
+ ]
54
+
55
+ inputs = processor.apply_chat_template(
56
+ messages,
57
+ tokenize=True,
58
+ add_generation_prompt=True,
59
+ return_dict=True,
60
+ return_tensors="pt",
61
+ ).to(model.device)
62
+
63
+ with torch.no_grad():
64
+ generated_ids = model.generate(**inputs, max_new_tokens=512)
65
+
66
+ trimmed = [
67
+ out_ids[len(in_ids):]
68
+ for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
69
+ ]
70
+ raw_output = processor.batch_decode(
71
+ trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
72
+ )[0]
73
+
74
+ # Parse JSON
75
+ text = raw_output.strip()
76
+ if text.startswith("```"):
77
+ text = text.split("\n", 1)[1].rsplit("```", 1)[0].strip()
78
+ try:
79
+ return json.loads(text)
80
+ except json.JSONDecodeError:
81
+ return {"raw": raw_output}
82
+
83
+
84
+ def main():
85
+ parser = argparse.ArgumentParser(description="BTC Chart Trading Signal Prediction")
86
+ parser.add_argument("--image", type=str, required=True, help="Path to chart image")
87
+ parser.add_argument("--adapter", type=str, default="LangQuant/LQ-Qwen3-VL-4B-ChartSignal",
88
+ help="LoRA adapter path or HuggingFace repo ID")
89
+ parser.add_argument("--base-model", type=str, default=BASE_MODEL)
90
+ args = parser.parse_args()
91
+
92
+ model, processor = load_model(args.adapter, args.base_model)
93
+ result = predict(model, processor, args.image)
94
+
95
+ print("\n" + "=" * 60)
96
+ print("Prediction Result")
97
+ print("=" * 60)
98
+ print(json.dumps(result, indent=2, ensure_ascii=False))
99
+
100
+ signal = result.get("signal", "?")
101
+ conf = result.get("confidence", "?")
102
+ risk = result.get("risk_level", "?")
103
+ print(f"\nSignal: {signal} | Confidence: {conf} | Risk: {risk}")
104
+
105
+
106
+ if __name__ == "__main__":
107
+ main()
processor_config.json ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "image_processor": {
3
+ "data_format": "channels_first",
4
+ "do_convert_rgb": true,
5
+ "do_normalize": true,
6
+ "do_rescale": true,
7
+ "do_resize": true,
8
+ "image_mean": [
9
+ 0.5,
10
+ 0.5,
11
+ 0.5
12
+ ],
13
+ "image_processor_type": "Qwen2VLImageProcessorFast",
14
+ "image_std": [
15
+ 0.5,
16
+ 0.5,
17
+ 0.5
18
+ ],
19
+ "merge_size": 2,
20
+ "patch_size": 16,
21
+ "resample": 3,
22
+ "rescale_factor": 0.00392156862745098,
23
+ "size": {
24
+ "longest_edge": 16777216,
25
+ "shortest_edge": 65536
26
+ },
27
+ "temporal_patch_size": 2
28
+ },
29
+ "processor_class": "Qwen3VLProcessor",
30
+ "video_processor": {
31
+ "data_format": "channels_first",
32
+ "default_to_square": true,
33
+ "do_convert_rgb": true,
34
+ "do_normalize": true,
35
+ "do_rescale": true,
36
+ "do_resize": true,
37
+ "do_sample_frames": true,
38
+ "fps": 2,
39
+ "image_mean": [
40
+ 0.5,
41
+ 0.5,
42
+ 0.5
43
+ ],
44
+ "image_std": [
45
+ 0.5,
46
+ 0.5,
47
+ 0.5
48
+ ],
49
+ "max_frames": 768,
50
+ "merge_size": 2,
51
+ "min_frames": 4,
52
+ "patch_size": 16,
53
+ "resample": 3,
54
+ "rescale_factor": 0.00392156862745098,
55
+ "return_metadata": false,
56
+ "size": {
57
+ "longest_edge": 25165824,
58
+ "shortest_edge": 4096
59
+ },
60
+ "temporal_patch_size": 2,
61
+ "video_processor_type": "Qwen3VLVideoProcessor"
62
+ }
63
+ }
tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:be75606093db2094d7cd20f3c2f385c212750648bd6ea4fb2bf507a6a4c55506
3
+ size 11422650
tokenizer_config.json ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_prefix_space": false,
3
+ "backend": "tokenizers",
4
+ "bos_token": null,
5
+ "clean_up_tokenization_spaces": false,
6
+ "eos_token": "<|im_end|>",
7
+ "errors": "replace",
8
+ "extra_special_tokens": [
9
+ "<|im_start|>",
10
+ "<|im_end|>",
11
+ "<|object_ref_start|>",
12
+ "<|object_ref_end|>",
13
+ "<|box_start|>",
14
+ "<|box_end|>",
15
+ "<|quad_start|>",
16
+ "<|quad_end|>",
17
+ "<|vision_start|>",
18
+ "<|vision_end|>",
19
+ "<|vision_pad|>",
20
+ "<|image_pad|>",
21
+ "<|video_pad|>"
22
+ ],
23
+ "is_local": false,
24
+ "model_max_length": 262144,
25
+ "pad_token": "<|endoftext|>",
26
+ "processor_class": "Qwen3VLProcessor",
27
+ "split_special_tokens": false,
28
+ "tokenizer_class": "Qwen2Tokenizer",
29
+ "unk_token": null
30
+ }