import numpy as np import pandas as pd from statsmodels.tsa.arima.model import ARIMA import matplotlib.pyplot as plt from io import BytesIO import base64 import re import gradio as gr def generate_plot(data): model = ARIMA(data, order=(5, 1, 3)) fit_model = model.fit() forecast = fit_model.get_forecast(steps=7) min_price_index = forecast.predicted_mean.idxmin() min_price = forecast.predicted_mean[min_price_index] plt.figure(figsize=(12, 6)) plt.plot(data.index, data, label='Actual Prices') plt.plot(forecast.predicted_mean.index, forecast.predicted_mean, label='ARIMA Forecast') plt.scatter(min_price_index, min_price, color='red', label=f'Minimum Forecasted Price\nDate: {min_price_index.strftime("%Y-%m-%d")}\nPrice: {min_price:.2f}') plt.title('ARIMA Time Series Forecasting') plt.xlabel('Date') plt.ylabel('Price') plt.legend() # Save the plot to a BytesIO object img_bytes = BytesIO() plt.savefig(img_bytes, format='png') img_bytes.seek(0) plt.close() # Convert the plot to base64 for embedding in HTML plot_base64 = base64.b64encode(img_bytes.read()).decode('utf-8') return plot_base64 def get_product_name(url): # Map webpage URLs to CSV file names url_mapping = { 'https://www.flipkart.com/lg-ur7500-108-cm-43-inch-ultra-hd-4k-led-smart-webos-tv-2023-a5-ai-processor-4k-gen6-60hz-refresh-rate-magic-remote-capability/p/itm926f6b65c136b?pid=TVSGQQDYSCFGRX2F&lid=LSTTVSGQQDYSCFGRX2FVATNAW&fm=neo%2Fmerchandising&iid=M_f14f9138-3f49-4442-9825-2e80b67ca013_27.GO45SKVBQD2H&ppt=hp&ppn=homepage&ssid=ucylcxqj1c0000001706348119148&otracker=clp_omu_Televisions_3_27.dealCard.OMU_tvs-and-appliances-new-clp-store_tvs-and-appliances-new-clp-store_GO45SKVBQD2H_25&otracker1=clp_omu_PINNED_neo%2Fmerchandising_Televisions_NA_dealCard_cc_3_NA_view-all_25&cid=GO45SKVBQD2H': 'lgMonitor.csv', 'https://www.flipkart.com/apple-ipad-10th-gen-64-gb-rom-10-9-inch-wi-fi-only-pink/p/itmaf20456d05879?pid=TABGJ6XUSFTPNTKH&lid=LSTTABGJ6XUSFTPNTKHITLWPF&marketplace=FLIPKART&q=%2Fapple-ipad-10th-gen-64-gb-rom&store=tyy%2Fhry&srno=s_1_3&otracker=search&otracker1=search&fm=Search&iid=d0c72954-0a17-40fc-b627-0a02420a730a.TABGJ6XUSFTPNTKH.SEARCH&ppt=pp&ppn=pp&ssid=95nrm0ypv40000001706348742165&qH=3e0ceaa4a1aebf32': 'IPad.csv', 'https://www.flipkart.com/hf-mrf-grand-vk-18-full-size-ideal-15-21-years-complete-cricket-kit/p/itmae4fff077ce32?pid=KITG86QHGZZKTKRG&lid=LSTKITG86QHGZZKTKRGVELUCO&marketplace=FLIPKART&q=cricket+kit&store=abc%2F5lf%2Fra8&spotlightTagId=BestsellerId_abc%2F5lf%2Fra8&srno=s_1_1&otracker=search&otracker1=search&fm=Search&iid=66f4bc54-2518-4fe6-87d8-77285a37918b.KITG86QHGZZKTKRG.SEARCH&ppt=sp&ppn=sp&ssid=jrcatake680000001706349413005&qH=602f2733d0cdfdfb': 'cricketball.csv', 'https://www.flipkart.com/oppo-enco-buds-2-28-hours-battery-life-deep-noise-cancellation-bluetooth-headset/p/itm3344fa26518ed?pid=ACCGH7YZY6AHGCHJ&lid=LSTACCGH7YZY6AHGCHJHLWF41&marketplace=FLIPKART&q=earbuds&store=0pm%2Ffcn%2F821%2Fa7x%2F2si&srno=s_1_4&otracker=AS_Query_PredictiveAutoSuggest_10_0_na_na_na&otracker1=AS_Query_PredictiveAutoSuggest_10_0_na_na_na&fm=search-autosuggest&iid=542a4755-f4d6-4aec-b0bd-879c71423622.ACCGH7YZY6AHGCHJ.SEARCH&ppt=sp&ppn=sp&ssid=1kux6gdhv40000001706350125817&qH=2951f68ff1fee1e0': 'earphones.csv', # Add more mappings as needed } # Extract the product name from the URL match = re.search(r'/([^/]+)\.csv', url) if match: product_name = match.group(1) # Check if the product name is in the mapping if product_name in url_mapping: return url_mapping[product_name] return None def predict(url): # Get the product name from the URL product_name = get_product_name(url) if product_name is not None: # Adjust the path based on the provided product csv_path = f"datasets/{product_name}" try: dataset = pd.read_csv(csv_path) dataset['Date'] = pd.to_datetime(dataset['Date']) dataset = dataset.sort_values(by='Date') data = dataset.set_index('Date')['Price'] plot_base64 = generate_plot(data) # HTML template for rendering the plot html_template = f""" ARIMA Time Series Forecasting

ARIMA Time Series Forecasting

ARIMA Forecast Plot
""" return html_template except FileNotFoundError: return "CSV file not found for the given product." else: return "No data found for the given URL." # Gradio Interface iface = gr.Interface(fn=predict, inputs="text", outputs="html", live=True) iface.launch()