import gradio as gr import yfinance as yf # --- Define your logic --- def analyze_stock(ticker): """ Fetch current and previous data for the given stock symbol, check price change, and give BUY or SELL recommendation. """ try: stock = yf.Ticker(ticker) data = stock.history(period="5d") if data.empty: return f"⚠️ No data found for '{ticker}'" # Get current and previous close current_price = data['Close'][-1] previous_price = data['Close'][-2] # Calculate % change change_percent = ((current_price - previous_price) / previous_price) * 100 # --- Decision logic --- recommendation = "📊 HOLD" if 10 <= change_percent <= 11: recommendation = "🟢 BUY SIGNAL" elif change_percent <= -14: recommendation = "🔴 SELL SIGNAL" return f""" 📈 **{ticker.upper()} Stock Analysis** - Current Price: {current_price:.2f} USD - Previous Close: {previous_price:.2f} USD - Change: {change_percent:.2f}% **AI Suggestion:** {recommendation} """ except Exception as e: return f"❌ Error: {str(e)}" # --- Gradio UI --- iface = gr.Interface( fn=analyze_stock, inputs=gr.Textbox(label="Enter Stock Symbol (e.g., AAPL, TSLA, INFY.NS)"), outputs=gr.Markdown(label="Result"), title="🤖 Smart AI Bull", description="An AI-powered stock signal tracker that shows when to BUY or SELL based on % gain/loss.", ) iface.launch()