import yfinance as yf import pandas as pd import gradio as gr import datetime import os def fetch_and_predict(ticker, start_date, end_date): try: # Fetch historical data data = yf.download(ticker, start=start_date, end=end_date, progress=False) if data.empty: return "⚠️ No data found for given range.", None, None data.reset_index(inplace=True) data = data[["Date", "Open", "High", "Low", "Close", "Volume"]] # Tomorrow's prediction = last close last_close = data["Close"].iloc[-1] tomorrow = pd.to_datetime(data["Date"].iloc[-1]) + pd.Timedelta(days=1) predicted_price = round(last_close, 2) prediction_df = pd.DataFrame({ "Date": [tomorrow], "Predicted_Open": [predicted_price] }) # Save results result_df = pd.concat([data, prediction_df], ignore_index=True) csv_path = os.path.join(os.getcwd(), "sbin_prediction.csv") result_df.to_csv(csv_path, index=False) msg = f"✅ Last Close: {last_close:.2f}\n📈 Predicted buying price for {tomorrow.date()}: {predicted_price}" return msg, csv_path, result_df except Exception as e: return f"❌ Error: {str(e)}", None, None # Gradio App with gr.Blocks() as demo: gr.Markdown("## 📊 SBIN.NS Stock Prediction (Tomorrow’s Price)") with gr.Row(): ticker = gr.Textbox(value="SBIN.NS", label="Stock Ticker (Yahoo format)") with gr.Row(): start_date = gr.Textbox(value="2025-01-01", label="Start Date (YYYY-MM-DD)") end_date = gr.Textbox(value=str(datetime.date.today()), label="End Date (YYYY-MM-DD)") run_btn = gr.Button("Fetch & Predict") output_msg = gr.Textbox(label="Prediction Result") download_file = gr.File(label="Download CSV") output_table = gr.Dataframe(label="Data + Prediction") run_btn.click(fetch_and_predict, inputs=[ticker, start_date, end_date], outputs=[output_msg, download_file, output_table]) if __name__ == "__main__": demo.launch(server_name="0.0.0.0", server_port=7860)