Spaces:
Runtime error
Runtime error
File size: 3,611 Bytes
32e3c06 5c43f7a 21ac398 01e3cc0 805f7bd 32e3c06 f4ebb84 32e3c06 f4ebb84 805f7bd ab4ce46 805f7bd 21ac398 f4ebb84 805f7bd 21ac398 805f7bd 21ac398 f4ebb84 805f7bd 21ac398 f4ebb84 805f7bd 21ac398 32e3c06 21ac398 32e3c06 805f7bd 32e3c06 805f7bd 32e3c06 21ac398 32e3c06 ab4ce46 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
import os
os.environ['HF_HOME'] = '/tmp/huggingface'
import torch
import json
import pandas as pd
from pattern_logic import PatternLogic
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
class PatternAnalyzer:
def __init__(self):
quantization_config = BitsAndBytesConfig(
load_in_8bit=True,
llm_int8_threshold=6.0
)
model_kwargs = {
"device_map": "cpu",
"torch_dtype": torch.float32,
"low_cpu_mem_usage": True,
"max_memory": {
"cpu": "4GB",
"disk": "8GB"
},
"offload_folder": "/tmp/offload",
"quantization_config": quantization_config
}
self.model = AutoModelForCausalLM.from_pretrained(
"tmmdev/codellama-pattern-analysis",
**model_kwargs,
trust_remote_code=True
)
self.tokenizer = AutoTokenizer.from_pretrained(
"tmmdev/codellama-pattern-analysis",
use_fast=True
)
self.basic_patterns = {
'channel': {'min_points': 4, 'confidence_threshold': 0.7},
'triangle': {'min_points': 3, 'confidence_threshold': 0.75},
'support': {'min_touches': 2, 'confidence_threshold': 0.8},
'resistance': {'min_touches': 2, 'confidence_threshold': 0.8},
'double_top': {'max_deviation': 0.02, 'confidence_threshold': 0.85},
'double_bottom': {'max_deviation': 0.02, 'confidence_threshold': 0.85}
}
self.pattern_logic = PatternLogic()
def analyze_data(self, ohlcv_data):
data_prompt = f"""TASK: Identify high-confidence technical patterns only.
Minimum confidence threshold: 0.8
Required pattern criteria:
1. Channel: Must have at least 3 touching points
2. Triangle: Must have clear convergence point
3. Support: Minimum 3 price bounces
4. Resistance: Minimum 3 price rejections
INPUT DATA:
{ohlcv_data.to_json(orient='records')}
Return ONLY high-confidence patterns (>0.8) in JSON format with exact price coordinates."""
inputs = self.tokenizer(data_prompt, return_tensors="pt")
outputs = self.model.generate(**inputs, max_length=1000)
analysis = self.tokenizer.decode(outputs[0])
return self.parse_analysis(analysis)
def parse_analysis(self, analysis_text):
try:
json_start = analysis_text.find('{')
json_end = analysis_text.rfind('}') + 1
json_str = analysis_text[json_start:json_end]
analysis_data = json.loads(json_str)
patterns = []
for pattern in analysis_data.get('patterns', []):
pattern_type = pattern.get('type')
if pattern_type in self.basic_patterns:
threshold = self.basic_patterns[pattern_type]['confidence_threshold']
if pattern.get('confidence', 0) >= threshold:
patterns.append({
'type': pattern_type,
'coordinates': pattern.get('coordinates', []),
'confidence': pattern.get('confidence'),
'metadata': {
'rules': self.basic_patterns[pattern_type],
'timestamp': pd.Timestamp.now().isoformat()
}
})
return patterns
except json.JSONDecodeError:
return [] |