Spaces:
Build error
Build error
| 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() | |