File size: 987 Bytes
28745e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4b2b21
 
28745e6
 
 
 
 
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
import gradio as gr
import pandas as pd
from sklearn.linear_model import LinearRegression
import numpy as np

# サンプルデータの作成
np.random.seed(0)
dates = pd.date_range('20230101', periods=100)
sales = np.random.randint(100, 200, size=(100,))
data = pd.DataFrame({'date': dates, 'sales': sales})

# モデルの訓練
model = LinearRegression()
data['date_ordinal'] = pd.to_datetime(data['date']).map(pd.Timestamp.toordinal)
X = data['date_ordinal'].values.reshape(-1, 1)
y = data['sales'].values
model.fit(X, y)

def predict_sales(future_date):
    future_date_ordinal = pd.to_datetime(future_date).toordinal()
    prediction = model.predict(np.array([[future_date_ordinal]]))
    return prediction[0]

# Gradioインターフェースの定義
iface = gr.Interface(
    fn=predict_sales,
    inputs=gr.components.Textbox(label="Enter future date (YYYY-MM-DD)"),
    outputs=gr.components.Textbox(label="Predicted sales")
)

if __name__ == "__main__":
    iface.launch()