File size: 3,092 Bytes
f43d06f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import streamlit as st
import requests
import os
from dotenv import load_dotenv
import groq  # Import Groq API SDK

# Load environment variables
load_dotenv()

# Initialize Groq Client
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
client = groq.Client(api_key=GROQ_API_KEY)

# Set API key for Financial Modeling Prep (FMP)
FMP_API_KEY = os.environ.get("FMP_API_KEY")
# FMP_API_KEY="9oIC7Pv2OyjTcw2QarsR71Ct8D053ced"

# Function to fetch stock data from Financial Modeling Prep API
def get_stock_data(symbol):
    url = f"https://financialmodelingprep.com/api/v3/quote/{symbol}?apikey={FMP_API_KEY}"
    response = requests.get(url)
    if response.status_code == 200 and response.json():
        return response.json()[0]
    return None

# Function to generate AI response using Groq
def get_ai_response(prompt):
    model_name = "llama-3.3-70b-versatile"  # More stable model
    messages = [{"role": "user", "content": prompt}]

    try:
        response = client.chat.completions.create(
            model=model_name,
            messages=messages,
            stream=False  # Using non-streaming for reliability
        )
        return response.choices[0].message.content if response.choices else "No response received."
    except Exception as exc:
        return f"❌ Error: {exc}"

# Streamlit UI
st.set_page_config(page_title="AI Financial Assistant", page_icon="πŸ’°", layout="centered")

# Sidebar Navigation
st.sidebar.title("Navigation")
# page = st.sidebar.radio("Go to", ["Home", "AI Assistant"])
page = st.sidebar.selectbox('Select a page:', 
                               ['Home', 'AI Assistant'])

if page == "Home":
    
    st.title("Welcome to AI Financial Assistant πŸ’°")
    st.write("This tool helps you with stock market insights and financial advice.")
    st.write("Navigate to 'AI Assistant' to get started.")
    st.markdown("---")
    st.write("Developed by **Subramanyam Rekhandar**")
    st.image("banner.jpeg", use_container_width=True)  # Add banner image

elif page == "AI Assistant":
    st.title("πŸ’° AI Financial Assistant")
    st.write("Ask me about stock prices, market trends, or investment advice!")

    query = st.text_input("Enter a stock symbol (e.g., AAPL) or a financial question:")

    if st.button("Get Insights"):
        if query.upper().isalpha():  # Check if input is a stock symbol
            data = get_stock_data(query.upper())
            if data:
                st.write(f"### {data['name']} ({data['symbol']})")
                st.write(f"πŸ“ˆ Price: ${data['price']}")
                st.write(f"πŸ“Š Change: {data['change']} ({data['changesPercentage']}%)")
            else:
                st.write("⚠️ Stock data not found. Try a different symbol.")
        else:  # Treat input as a general finance query
            with st.spinner("πŸ’¬ Thinking..."):
                response = get_ai_response(query)
            st.write(response)

    st.markdown("---")
    st.write("πŸ” Data sourced from Financial Modeling Prep & Groq AI.")