gradio2 / app.py
maty0505's picture
upload 2 files
28745e6 verified
raw
history blame
980 Bytes
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.inputs.Textbox(label="Enter future date (YYYY-MM-DD)"),
outputs=gr.outputs.Textbox(label="Predicted sales")
)
if __name__ == "__main__":
iface.launch()