-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
354 lines (304 loc) · 12.2 KB
/
Copy pathmain.py
File metadata and controls
354 lines (304 loc) · 12.2 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python3
"""
WillTiboReset - Main entry point
Predict whether Tibo/OpenAI will reset ChatGPT/Codex usage quota
within the next 5h / 24h / 48h.
Phase 3: full prediction pipeline (collect → analyze → LLM signals → survival model prediction)
Usage:
python main.py # Run the full prediction pipeline
python main.py --status # Show project status only
python main.py --analyze # Run signal analysis only (no prediction)
python main.py --predict # Run prediction only (using already collected data)
"""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
# Ensure the project root is in sys.path
PROJECT_ROOT = Path(__file__).resolve().parent
if str(PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(PROJECT_ROOT))
from analyzer import SignalAnalyzer
from analyzer.llm_signal import DeepSeekAnalyzer, MockLLMAnalyzer
from collectors import ResetHistoryCollector, TweetCollector
from config import config
from model.data_models import PredictionResult
from model.model_state import ModelStateManager
from model.survival_model import ResetPredictor, build_features
from output import OutputFormatter
def _validate_prediction_config() -> None:
"""Validate required configuration before running prediction."""
if not config.has_deepseek_credentials:
raise RuntimeError(
"DEEPSEEK_API_KEY is not configured. Set it in .env or GitHub Actions Secrets."
)
if not config.rss_feeds.get("tibo"):
raise RuntimeError(
"TIBO_RSS_URLS is not configured. Tibo is the core data source; at least one RSS URL is required."
)
def print_separator(title: str = "") -> None:
"""Print a separator line"""
width = 60
if title:
pad = (width - len(title) - 2) // 2
print(f"{'─' * pad} {title} {'─' * (width - len(title) - 2 - pad)}")
else:
print("─" * width)
def show_status() -> None:
"""Display project status"""
print()
print("=" * 60)
print(" WillTiboReset - Project Status")
print("=" * 60)
print()
# Configuration
print_separator("Configuration")
print(f" Prediction horizons: {config.prediction_horizons} hours")
print(f" Confidence threshold: {config.confidence_threshold}")
print(f" Data directory: {config.data_dir}")
print(f" Output directory: {config.output_dir}")
print()
# API credentials
print_separator("API Credentials")
print(f" RSS feeds: {'✓ configured' if config.has_rss_feeds else '✗ not configured'}")
print(f" OpenAI: {'✓ configured' if config.has_openai_credentials else '✗ not configured'}")
print()
# Data files
print_separator("Data Files")
tweet_collector = TweetCollector(config.tweets_path)
reset_collector = ResetHistoryCollector(config.reset_history_path)
tweets = tweet_collector.collect()
events = reset_collector.collect()
print(f" Tweets: {len(tweets)} tweets")
print(f" Reset events: {len(events)} events")
print()
print("=" * 60)
print()
def run_analysis() -> None:
"""Run signal analysis and print results"""
print()
print("=" * 60)
print(" WillTiboReset - Signal Analysis")
print("=" * 60)
print()
# 1. Load data
print_separator("Data Collection")
tweet_collector = TweetCollector(config.tweets_path)
reset_collector = ResetHistoryCollector(config.reset_history_path)
tweets = tweet_collector.collect()
events = reset_collector.collect()
print(f" Loaded tweets: {len(tweets)} tweets")
print(f" Loaded reset events: {len(events)} events")
print()
# 2. Analyze signals
print_separator("Feature Extraction")
analyzer = SignalAnalyzer()
features = analyzer.analyze(tweets, events)
print(f" Total tweets: {features.tweet_count}")
print(f" Tweets in last 24h: {features.recent_tweet_count}")
print(f" Unique authors: {features.unique_authors}")
print(f" Historical reset events: {features.total_reset_events}")
if features.hours_since_last_reset is not None:
print(f" Hours since last reset: {features.hours_since_last_reset:.1f} hours")
if features.avg_reset_interval_hours is not None:
print(f" Average reset interval: {features.avg_reset_interval_hours:.1f} hours")
print()
# 3. Signal descriptions
print_separator("Signal Summary")
for desc in features.to_signal_descriptions():
print(f" • {desc}")
print()
print("=" * 60)
print()
def run_prediction(tweets=None, events=None) -> None:
"""
Run the prediction step: LLM signal analysis → feature building → survival model prediction.
If tweets/events are not provided, load them from data files.
"""
print()
print("=" * 60)
print(" WillTiboReset - Prediction Engine")
print("=" * 60)
print()
config.ensure_dirs()
_validate_prediction_config()
# Load data if not provided
if tweets is None or events is None:
print_separator("Data Loading")
tweet_collector = TweetCollector(config.tweets_path)
reset_collector = ResetHistoryCollector(config.reset_history_path)
tweets = tweet_collector.collect()
events = reset_collector.collect()
print(f" Tweets: {len(tweets)} tweets")
print(f" Reset events: {len(events)} events")
print()
# Step 1: Statistical feature extraction
print_separator("Step 1: Statistical Feature Extraction")
analyzer = SignalAnalyzer()
analysis_features = analyzer.analyze(tweets, events)
print(f" Total tweets: {analysis_features.tweet_count}")
print(f" Tweets in last 24h: {analysis_features.recent_tweet_count}")
if analysis_features.hours_since_last_reset is not None:
print(f" Hours since last reset: {analysis_features.hours_since_last_reset:.1f} hours")
else:
print(f" Hours since last reset: no historical record")
if analysis_features.avg_reset_interval_hours is not None:
print(f" Average reset interval: {analysis_features.avg_reset_interval_hours:.1f} hours")
print()
# Step 2: LLM signal analysis
print_separator("Step 2: LLM Signal Analysis")
llm_analyzer = DeepSeekAnalyzer(
api_key=config.deepseek_api_key,
model=config.deepseek_model,
)
print(f" Analyzer: {llm_analyzer.__class__.__name__}")
if tweets:
signal_scores = llm_analyzer.analyze_tweets(tweets)
batch_scores = llm_analyzer.analyze_batch([t.text for t in tweets])
print(f" Analyzed tweets: {len(signal_scores)}")
print(f" Aggregated reset_intent: {batch_scores.reset_intent:.2f}")
print(f" Aggregated reset_confirmation: {batch_scores.reset_confirmation:.2f}")
print(f" Aggregated limit_complaint: {batch_scores.limit_complaint:.2f}")
print(f" Aggregated official_change: {batch_scores.official_change:.2f}")
else:
signal_scores = []
batch_scores = None
print(" No tweets to analyze")
print()
# Step 3: Load model state and build prediction features
print_separator("Step 3: Feature Building")
state_manager = ModelStateManager(config.model_state_path)
model_state = state_manager.load()
if model_state is not None:
print(f" Loaded model_state: {model_state.sample_count} intervals")
else:
print(" model_state.json not found, using default prior parameters")
pred_features = build_features(
hours_since_last_reset=analysis_features.hours_since_last_reset,
average_reset_interval=analysis_features.avg_reset_interval_hours,
median_reset_interval=analysis_features.median_reset_interval_hours,
interval_uncertainty=analysis_features.std_reset_interval_hours,
signal_scores=signal_scores if signal_scores else None,
tweets=tweets if tweets else None,
interval_count=analysis_features.reset_interval_count,
model_state=model_state,
recent_reset_time=(
max((e.reset_time for e in events), default=None) if events else None
),
)
print(f" hours_since_last_reset: {pred_features.hours_since_last_reset}")
print(f" average_reset_interval: {pred_features.average_reset_interval}")
print(f" median_reset_interval: {pred_features.median_reset_interval}")
print(f" time_pressure: {pred_features.time_pressure:.3f}")
print(f" tibo_signal: {pred_features.tibo_signal:.3f}")
print(f" community_signal: {pred_features.community_signal:.3f}")
print(f" release_signal: {pred_features.release_signal:.3f}")
print()
# Step 4: Survival model prediction
print_separator("Step 4: Survival Model Prediction")
predictor = ResetPredictor(
horizons=config.prediction_horizons,
default_interval=config.default_reset_interval_hours,
model_state=model_state,
)
print(f" Model: {predictor.model_version}")
explanation = predictor.predict(pred_features)
print(f" Hazard rate: {explanation.hazard_rate:.4f}/h")
print(f" Time pressure: {explanation.time_pressure:.2f}")
if explanation.time_ratio is not None:
print(f" Time ratio: {explanation.time_ratio:.2f}x")
print()
print(" Predicted probabilities:")
for horizon, prob in explanation.probability.items():
bar_len = int(prob * 30)
bar = "█" * bar_len + "░" * (30 - bar_len)
print(f" {horizon:>4s}: {prob:.2%} {bar}")
print()
print_separator("Explanation")
for reason in explanation.reasons:
print(f" • {reason}")
print()
# Step 5: Save results
print_separator("Step 5: Save Results")
import json
from datetime import datetime, timezone
prior_applied = analysis_features.avg_reset_interval_hours is None
output_data = {
"timestamp": datetime.now(timezone.utc).isoformat(),
"model_version": predictor.model_version,
"features": pred_features.model_dump(mode="json"),
"prediction": explanation.model_dump(mode="json"),
"meta": {
"prior_applied": prior_applied,
"interval_count": analysis_features.reset_interval_count,
},
}
json_path = config.output_dir / "prediction_latest.json"
json_path.write_text(json.dumps(output_data, indent=2, ensure_ascii=False), encoding="utf-8")
print(f" Saved: {json_path}")
print()
print_separator("Done")
print(" Prediction pipeline complete.")
print()
print("=" * 60)
print()
def run_pipeline() -> None:
"""
Run the full pipeline: collect → analyze → predict → output
"""
print()
print("=" * 60)
print(" WillTiboReset - Full Prediction Pipeline")
print("=" * 60)
print()
config.ensure_dirs()
# 1. Collect
print_separator("Step 1: Data Collection")
tweet_collector = TweetCollector(config.tweets_path)
reset_collector = ResetHistoryCollector(config.reset_history_path)
tweets = tweet_collector.collect()
events = reset_collector.collect()
print(f" Tweets: {len(tweets)} tweets")
print(f" Reset events: {len(events)} events")
print()
# 2. Statistical signal analysis
print_separator("Step 2: Signal Analysis")
analyzer = SignalAnalyzer()
features = analyzer.analyze(tweets, events)
for desc in features.to_signal_descriptions():
print(f" • {desc}")
print()
# 3. Predict
run_prediction(tweets=tweets, events=events)
def main() -> int:
"""CLI entry point"""
parser = argparse.ArgumentParser(
description="WillTiboReset - AI prediction for Tibo/OpenAI quota resets",
)
parser.add_argument(
"--status",
action="store_true",
help="Show project status only",
)
parser.add_argument(
"--analyze",
action="store_true",
help="Run signal analysis only (no prediction)",
)
parser.add_argument(
"--predict",
action="store_true",
help="Run prediction only (using already collected data)",
)
args = parser.parse_args()
if args.status:
show_status()
elif args.analyze:
run_analysis()
elif args.predict:
run_prediction()
else:
run_pipeline()
return 0
if __name__ == "__main__":
sys.exit(main())