|
|
|
import os
|
|
import streamlit as st
|
|
from openai import OpenAI
|
|
|
|
|
|
api_key_nvidia = os.environ.get("api_key_nvidia")
|
|
|
|
|
|
if not api_key_nvidia:
|
|
st.error("NVIDIA API key not found. Please set the `api_key_nvidia` environment variable.")
|
|
st.stop()
|
|
|
|
|
|
st.markdown("""
|
|
<style>
|
|
/* Set background color for the main section */
|
|
.main {
|
|
background-color: #f4f9f9; /* Light teal for a professional look */
|
|
color: #000000; /* Black text for readability */
|
|
}
|
|
/* Set background color for the sidebar */
|
|
.sidebar .sidebar-content {
|
|
background-color: #d1e7dd; /* Slightly darker teal */
|
|
}
|
|
/* Set text color for input fields */
|
|
.stTextInput textarea {
|
|
color: #000000 !important;
|
|
}
|
|
/* Change styles for dropdown menu */
|
|
.stSelectbox div[data-baseweb="select"] {
|
|
color: black !important;
|
|
background-color: #d1e7dd !important;
|
|
}
|
|
/* Change color of dropdown icons */
|
|
.stSelectbox svg {
|
|
fill: black !important;
|
|
}
|
|
/* Change background and text color for dropdown options */
|
|
.stSelectbox option {
|
|
background-color: #d1e7dd !important;
|
|
color: black !important;
|
|
}
|
|
/* Change background and text color for dropdown items */
|
|
div[role="listbox"] div {
|
|
background-color: #d1e7dd !important;
|
|
color: black !important;
|
|
}
|
|
</style>
|
|
""", unsafe_allow_html=True)
|
|
|
|
|
|
st.title("π° Finance Assistant")
|
|
|
|
st.caption("π Your AI-Powered Financial Advisor")
|
|
|
|
|
|
with st.sidebar:
|
|
|
|
st.divider()
|
|
|
|
st.markdown("### Assistant Capabilities")
|
|
st.markdown("""
|
|
- π Investment Analysis
|
|
- π³ Budgeting Advice
|
|
- π¦ Loan Guidance
|
|
- π‘ Retirement Planning
|
|
""")
|
|
|
|
st.divider()
|
|
|
|
st.markdown("Built with NVIDIA API | LangChain ")
|
|
|
|
|
|
client = OpenAI(
|
|
base_url="https://integrate.api.nvidia.com/v1",
|
|
api_key=api_key_nvidia
|
|
)
|
|
|
|
|
|
system_prompt_template = (
|
|
"You are an expert AI financial assistant. Provide accurate, concise, and empathetic responses "
|
|
"to user queries related to investments, budgeting, loans, retirement planning, and other financial matters. "
|
|
"Always respond in English."
|
|
)
|
|
|
|
|
|
if "message_log" not in st.session_state:
|
|
st.session_state.message_log = [
|
|
{"role": "assistant", "content": "Hi! I'm your Finance Assistant. How can I assist you today? π°"}
|
|
]
|
|
|
|
|
|
chat_container = st.container()
|
|
|
|
|
|
with chat_container:
|
|
for message in st.session_state.message_log:
|
|
with st.chat_message(message["role"]):
|
|
st.markdown(message["content"])
|
|
|
|
|
|
user_query = st.chat_input("Type your finance-related question here...")
|
|
|
|
|
|
def generate_ai_response(messages):
|
|
"""
|
|
Sends the conversation to the AI model and processes the response.
|
|
"""
|
|
completion = client.chat.completions.create(
|
|
model="deepseek-ai/deepseek-r1",
|
|
messages=messages,
|
|
temperature=0.5,
|
|
top_p=0.5,
|
|
max_tokens=1000,
|
|
stream=True
|
|
)
|
|
|
|
|
|
response = ""
|
|
for chunk in completion:
|
|
content = chunk.choices[0].delta.content or ""
|
|
response += content
|
|
return response
|
|
|
|
|
|
if user_query:
|
|
|
|
st.session_state.message_log.append({"role": "user", "content": user_query})
|
|
|
|
|
|
messages = [
|
|
{"role": "system", "content": system_prompt_template},
|
|
]
|
|
|
|
|
|
for msg in st.session_state.message_log:
|
|
role = msg["role"]
|
|
if role == "ai":
|
|
role = "assistant"
|
|
messages.append({"role": role, "content": msg["content"]})
|
|
|
|
|
|
with st.spinner("π§ Processing..."):
|
|
|
|
ai_response = generate_ai_response(messages)
|
|
|
|
|
|
st.session_state.message_log.append({"role": "assistant", "content": ai_response})
|
|
|
|
|
|
st.rerun() |