Views
No views yet
1{
2 "instruction": "You are a precise financial news analyst. Read the news text and output a compact JSON with fields: symbol, site, source_name, sentiment_score, sentiment_confidence, wow_score, wow_confidence.",
3 "input": "Tesla Reports Record Q3 Deliveries, Beats Wall Street Estimates Symbol: TSLA Site: reuters.com",
4 "output": "SENTIMENT: 0.8\nSENTIMENT CONFIDENCE: 0.9\nWOW SCORE: Big News\nWOW CONFIDENCE: 0.85"
5}1from transformers import AutoTokenizer, AutoModelForCausalLM
2import torch
3
4# Load model and tokenizer
5model_name = "path/to/finnews001"
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7model = AutoModelForCausalLM.from_pretrained(
8 model_name,
9 torch_dtype=torch.bfloat16,
10 device_map="auto"
11)
12
13# Prepare input
14news_text = "Apple beats Q4 earnings expectations, stock surges 5% in after-hours trading"
15input_text = f"{news_text} Symbol: AAPL Site: marketwatch.com"
16
17prompt = f"""<|im_start|>system
18You are a precise financial news analyst. Read the news text and output a compact JSON with fields: symbol, site, source_name, sentiment_score, sentiment_confidence, wow_score, wow_confidence.
19<|im_end|>
20<|im_start|>user
21{input_text}
22<|im_end|>
23<|im_start|>assistant
24"""
25
26# Generate response
27inputs = tokenizer(prompt, return_tensors="pt")
28with torch.no_grad():
29 outputs = model.generate(
30 **inputs,
31 max_new_tokens=100,
32 temperature=0.1,
33 do_sample=True
34 )
35
36response = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
37print(response)1def score_news_batch(news_items, model, tokenizer, batch_size=16):
2 """Process multiple news items efficiently"""
3 results = []
4
5 for i in range(0, len(news_items), batch_size):
6 batch = news_items[i:i+batch_size]
7
8 # Prepare batch prompts
9 prompts = []
10 for item in batch:
11 input_text = f"{item['text']} Symbol: {item['symbol']} Site: {item['site']}"
12 prompt = f"""<|im_start|>system
13You are a precise financial news analyst. Read the news text and output a compact JSON with fields: symbol, site, source_name, sentiment_score, sentiment_confidence, wow_score, wow_confidence.
14<|im_end|>
15<|im_start|>user
16{input_text}
17<|im_end|>
18<|im_start|>assistant
19"""
20 prompts.append(prompt)
21
22 # Tokenize batch
23 inputs = tokenizer(prompts, return_tensors="pt", padding=True, truncation=True)
24
25 # Generate responses
26 with torch.no_grad():
27 outputs = model.generate(
28 **inputs,
29 max_new_tokens=100,
30 temperature=0.1,
31 do_sample=True,
32 pad_token_id=tokenizer.eos_token_id
33 )
34
35 # Process outputs
36 for j, output in enumerate(outputs):
37 response = tokenizer.decode(
38 output[inputs['input_ids'][j].shape[0]:],
39 skip_special_tokens=True
40 )
41 results.append({
42 'original': batch[j],
43 'score': response.strip()
44 })
45
46 return results1from kafka import KafkaConsumer, KafkaProducer
2import json
3
4def kafka_news_scorer():
5 """Real-time news scoring with Kafka"""
6 consumer = KafkaConsumer(
7 'raw_news_feed',
8 bootstrap_servers=['localhost:9092'],
9 value_deserializer=lambda x: json.loads(x.decode('utf-8'))
10 )
11
12 producer = KafkaProducer(
13 bootstrap_servers=['localhost:9092'],
14 value_serializer=lambda x: json.dumps(x).encode('utf-8')
15 )
16
17 for message in consumer:
18 news_item = message.value
19
20 # Score the news
21 score = score_single_news(news_item, model, tokenizer)
22
23 # Route based on score
24 if "Big News" in score or "Huge News" in score:
25 producer.send('high_priority_news', {
26 'original': news_item,
27 'score': score,
28 'priority': 'high'
29 })
30 elif "Regular News" in score:
31 producer.send('medium_priority_news', {
32 'original': news_item,
33 'score': score,
34 'priority': 'medium'
35 })
36 # Low priority news is filtered out1class NewsScorer:
2 def __init__(self, model_path):
3 self.tokenizer = AutoTokenizer.from_pretrained(model_path)
4 self.model = AutoModelForCausalLM.from_pretrained(
5 model_path,
6 torch_dtype=torch.bfloat16,
7 device_map="auto"
8 )
9
10 def score_for_trading_signals(self, news_text, symbol, source):
11 """Score news for trading signal generation"""
12 input_text = f"{news_text} Symbol: {symbol} Site: {source}"
13
14 # Generate score
15 score_output = self.score_news(input_text)
16
17 # Parse sentiment and significance
18 sentiment = self.extract_sentiment(score_output)
19 significance = self.extract_significance(score_output)
20
21 # Determine trading signal strength
22 if significance in ["Big News", "Huge News"] and abs(sentiment) > 0.6:
23 return {
24 'signal_strength': 'HIGH',
25 'sentiment': sentiment,
26 'significance': significance,
27 'action': 'trigger_full_analysis'
28 }
29 elif significance == "Regular News" and abs(sentiment) > 0.4:
30 return {
31 'signal_strength': 'MEDIUM',
32 'sentiment': sentiment,
33 'significance': significance,
34 'action': 'monitor'
35 }
36 else:
37 return {
38 'signal_strength': 'LOW',
39 'sentiment': sentiment,
40 'significance': significance,
41 'action': 'ignore'
42 }SENTIMENT: 0.8
SENTIMENT CONFIDENCE: 0.9
WOW SCORE: Big News
WOW CONFIDENCE: 0.85Extremely Bad News: Catastrophic events (bankruptcies, major scandals)Bad News: Negative but manageable (missed earnings, downgrades)Meh News: Neutral or insignificant updatesRegular News: Standard business updatesBig News: Significant positive developments (beat earnings, partnerships)Huge News: Major positive catalysts (breakthroughs, acquisitions)1# Install Ollama
2curl -fsSL https://ollama.ai/install.sh | sh
3
4# Create model from Modelfile
5ollama create finnews001 -f trained_models/finnews001/Modelfile
6
7# Run the model
8ollama run finnews0011from transformers import pipeline
2
3scorer = pipeline(
4 "text-generation",
5 model="path/to/finnews001",
6 torch_dtype=torch.bfloat16,
7 device_map="auto"
8)1from vllm import LLM, SamplingParams
2
3llm = LLM(model="path/to/finnews001")
4sampling_params = SamplingParams(temperature=0.1, max_tokens=100)
5
6outputs = llm.generate(prompts, sampling_params)