MCPcopy Index your code
hub / github.com/shiyu-coder/Kronos / predict

Function predict

webui/app.py:405–624  ·  view source on GitHub ↗

Perform prediction

()

Source from the content-addressed store, hash-verified

403
404@app.route('/api/predict', methods=['POST'])
405def predict():
406 """Perform prediction"""
407 try:
408 data = request.get_json()
409 file_path = data.get('file_path')
410 lookback = int(data.get('lookback', 400))
411 pred_len = int(data.get('pred_len', 120))
412
413 # Get prediction quality parameters
414 temperature = float(data.get('temperature', 1.0))
415 top_p = float(data.get('top_p', 0.9))
416 sample_count = int(data.get('sample_count', 1))
417
418 if not file_path:
419 return jsonify({'error': 'File path cannot be empty'}), 400
420
421 # Load data
422 df, error = load_data_file(file_path)
423 if error:
424 return jsonify({'error': error}), 400
425
426 if len(df) < lookback:
427 return jsonify({'error': f'Insufficient data length, need at least {lookback} rows'}), 400
428
429 # Perform prediction
430 if MODEL_AVAILABLE and predictor is not None:
431 try:
432 # Use real Kronos model
433 # Only use necessary columns: OHLCV, excluding amount
434 required_cols = ['open', 'high', 'low', 'close']
435 if 'volume' in df.columns:
436 required_cols.append('volume')
437
438 # Process time period selection
439 start_date = data.get('start_date')
440
441 if start_date:
442 # Custom time period - fix logic: use data within selected window
443 start_dt = pd.to_datetime(start_date)
444
445 # Find data after start time
446 mask = df['timestamps'] >= start_dt
447 time_range_df = df[mask]
448
449 # Ensure sufficient data: lookback + pred_len
450 if len(time_range_df) < lookback + pred_len:
451 return jsonify({'error': f'Insufficient data from start time {start_dt.strftime("%Y-%m-%d %H:%M")}, need at least {lookback + pred_len} data points, currently only {len(time_range_df)} available'}), 400
452
453 # Use first lookback data points within selected window for prediction
454 x_df = time_range_df.iloc[:lookback][required_cols]
455 x_timestamp = time_range_df.iloc[:lookback]['timestamps']
456
457 # Use last pred_len data points within selected window as actual values
458 y_timestamp = time_range_df.iloc[lookback:lookback+pred_len]['timestamps']
459
460 # Calculate actual time period length
461 start_timestamp = time_range_df['timestamps'].iloc[0]
462 end_timestamp = time_range_df['timestamps'].iloc[lookback+pred_len-1]

Callers

nothing calls this directly

Calls 5

load_data_fileFunction · 0.85
create_prediction_chartFunction · 0.85
save_prediction_resultsFunction · 0.85
getMethod · 0.80
predictMethod · 0.80

Tested by

no test coverage detected