import gradio as gr import requests import pandas as pd import matplotlib.pyplot as plt API_KEY = "PJRAUD6KHJ2O097X" def get_stock_data(symbol, start_date, end_date): url = f"https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol={symbol}&apikey={API_KEY}" response = requests.get(url) data = response.json() if "Time Series (Daily)" in data: df = pd.DataFrame(data["Time Series (Daily)"]).T df.index = pd.to_datetime(df.index) df = df.loc[start_date:end_date] df.columns = ["Open", "High", "Low", "Close", "Volume"] # Plotting plt.figure(figsize=(10, 5)) plt.plot(df['Close'], label='Close Price') plt.title(f'Closing Price of {symbol}') plt.xlabel('Date') plt.ylabel('Price') plt.legend() plt.grid(True) plt.xticks(rotation=45) plt.tight_layout() return df.to_html(), gr.Image.from_matplotlib(plt) else: return "Data not found or invalid stock symbol.", None iface = gr.Interface( fn=get_stock_data, inputs=[ gr.Textbox(label="Stock Symbol", placeholder="Enter a stock symbol (like AAPL, MSFT)"), gr.Date(label="Start Date"), gr.Date(label="End Date") ], outputs=["html", "plot"], title="Personalized Stock Market Data App", description="Enter a stock symbol and date range to fetch its daily time series data." ) if __name__ == "__main__": iface.launch()