|
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] |
|
|
|
|
|
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() |
|
|
|
|