Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import os | |
| import pandas as pd | |
| import yfinance as yf | |
| import matplotlib.pyplot as plt | |
| from agno.agent import Agent | |
| from agno.models.groq import Groq | |
| from agno.tools.yfinance import YFinanceTools | |
| from agno.tools.duckduckgo import DuckDuckGoTools | |
| import base64 | |
| import groq | |
| from functools import wraps | |
| from dotenv import load_dotenv | |
| import time | |
| from finance_agent import FinanceAgent | |
| from pathlib import Path | |
| # UI enhancement | |
| st.set_page_config(layout="wide") | |
| def add_bg_from_local(image_path): | |
| # with open(image_path, "rb") as image_file: | |
| # encoded_string = base64.b64encode(image_file.read()).decode() | |
| #background-image: url("data:image/png;base64,{encoded_string}"); | |
| return f""" | |
| <style> | |
| .stApp {{ | |
| background-image: url("static/23115991-fffc-4f67-a116-581a1a9046c8.png"); | |
| background-size: cover; | |
| background-position: center; | |
| background-repeat: no-repeat; | |
| background-attachment: fixed; | |
| }} | |
| </style> | |
| """ | |
| def apply_custom_style(): | |
| # Add the background image | |
| image_path = "/Users/aditya/Desktop/finance_agent/23115991-fffc-4f67-a116-581a1a9046c8.png" | |
| st.markdown(add_bg_from_local(image_path), unsafe_allow_html=True) | |
| # Add the rest of the custom styling | |
| st.markdown(""" | |
| <style> | |
| .main .block-container { | |
| padding-top: 2rem; | |
| max-width: 1000px; | |
| background-color: rgba(255, 255, 255, 0.85); | |
| border-radius: 10px; | |
| padding: 20px; | |
| } | |
| h1 { | |
| color: #2E4057; | |
| text-align: center; | |
| margin-bottom: 2rem; | |
| padding-bottom: 1rem; | |
| border-bottom: 2px solid #E3B448; | |
| } | |
| .stButton > button { | |
| border-radius: 10px; | |
| background-color: #2E4057; | |
| color: white; | |
| font-weight: 600; | |
| width: 100%; | |
| } | |
| .stTextInput > div > div > input { | |
| border-radius: 10px; | |
| } | |
| .stExpander { | |
| background-color: #f8f9fa; | |
| border-radius: 10px; | |
| padding: 0.75rem; | |
| margin-bottom: 1rem; | |
| } | |
| .stChart { | |
| background-color: #f8f9fa; | |
| border-radius: 10px; | |
| padding: 1rem; | |
| box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); | |
| } | |
| div[data-testid="stVerticalBlock"] div[style*="flex-direction: column;"] { | |
| gap: 1rem; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| apply_custom_style() | |
| # Apply the custom styling | |
| apply_custom_style() | |
| # Load environment variables first | |
| load_dotenv() | |
| # Set API key explicitly | |
| groq.api_key = os.getenv("GROQ_API_KEY") | |
| # Import agent modules after setting API key | |
| # Initialize only the finance agent first | |
| finance_agent = FinanceAgent() | |
| # Streamlit app title | |
| st.title("Finance Agent App") | |
| # User input for stock ticker | |
| ticker_symbol = st.text_input("Enter Stock Ticker Symbol (e.g., TSLA):", "TSLA") | |
| if st.button("Get Financial Insights"): | |
| if ticker_symbol: | |
| try: | |
| # First display a comprehensive report | |
| with st.spinner(f"Generating comprehensive financial report for {ticker_symbol}..."): | |
| st.subheader(f"๐ Comprehensive Financial Report for {ticker_symbol}") | |
| comprehensive_report = finance_agent.generate_comprehensive_report(ticker_symbol) | |
| st.markdown(comprehensive_report.content) | |
| # Then show historical price chart | |
| with st.spinner(f"Retrieving historical price data for {ticker_symbol}..."): | |
| st.subheader(f"๐ Historical Stock Price Data for {ticker_symbol}") | |
| historical_data = finance_agent.get_historical_price_data(ticker_symbol) | |
| st.line_chart(historical_data['Close']) | |
| # Add a divider | |
| st.markdown("---") | |
| # Include expand sections for detailed information | |
| with st.expander(f"Detailed Stock Fundamentals for {ticker_symbol}"): | |
| fundamentals_response = finance_agent.get_stock_fundamentals(ticker_symbol) | |
| st.markdown(fundamentals_response.content) | |
| with st.expander(f"Analyst Recommendations for {ticker_symbol}"): | |
| analyst_response = finance_agent.get_analyst_recommendations_and_news(ticker_symbol) | |
| st.markdown(analyst_response.content) | |
| except Exception as e: | |
| st.error(f"An error occurred: {str(e)}") | |
| st.info("Try again or use a different ticker symbol") | |
| else: | |
| st.error("Please enter a valid stock ticker symbol.") |