Spaces:
Runtime error
Runtime error
File size: 1,779 Bytes
5b5f612 |
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 |
import gradio as gr
import torch
from chronos import ChronosPipeline
import numpy as np
import pandas as pd
# 从 Hugging Face 加载模型
# model_name = "amazon/chronos-t5-small" # 替换为你在 Hugging Face 上的模型名称
# model = AutoModelForConditionalGeneration.from_pretrained(model_name)
# model.eval()
model = ChronosPipeline.from_pretrained(
"amazon/chronos-t5-small",
device_map="cuda",
torch_dtype=torch.bfloat16,
)
def predict_with_chronos(input_data):
prediction = model.predict(
context=input_data,
prediction_length=24,
num_samples=1
)
return np.round(prediction.mean(axis=0).squeeze().cpu().numpy()).astype(int)
def predict_from_csv(csv_file):
df = pd.read_csv(csv_file.name)
raw_values = pd.to_numeric(df['value'], errors='coerce').dropna().values
print(raw_values)
print('输入数据长度为:',len(raw_values))
input_data = torch.tensor(
raw_values.astype(np.float32)
)
predictions = predict_with_chronos(input_data)
predictions = np.asarray(predictions).ravel()
forecast_index = range(1, len(predictions)+1)
assert len(forecast_index) == len(predictions), "数组长度不一致"
output_df = pd.DataFrame({
'period': forecast_index,
'value': predictions
})
output_path = "/tmp/predictions.csv"
output_df.to_csv(output_path, index=False)
return output_path
iface = gr.Interface(
fn=predict_from_csv,
inputs=gr.File(label="上传包含时序数据的 CSV 文件"),
outputs=gr.File(label="预测结果下载", file_count="single"),
title="Chronos时序预测",
description="上传包含时序数据的 CSV 文件,获取未来24步预测结果。"
)
iface.launch() |