Upload 2 files
Browse files- app.py +152 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import necessary libraries to use their functions
|
2 |
+
import os
|
3 |
+
import streamlit as st
|
4 |
+
from openai import OpenAI
|
5 |
+
|
6 |
+
# Get the API key from the environment settings
|
7 |
+
api_key_nvidia = os.environ.get("api_key_nvidia")
|
8 |
+
|
9 |
+
# If there is no API key, show an error message and stop the program
|
10 |
+
if not api_key_nvidia:
|
11 |
+
st.error("NVIDIA API key not found. Please set the `api_key_nvidia` environment variable.")
|
12 |
+
st.stop()
|
13 |
+
|
14 |
+
# Add custom design for a finance theme
|
15 |
+
st.markdown("""
|
16 |
+
<style>
|
17 |
+
/* Set background color for the main section */
|
18 |
+
.main {
|
19 |
+
background-color: #f4f9f9; /* Light teal for a professional look */
|
20 |
+
color: #000000; /* Black text for readability */
|
21 |
+
}
|
22 |
+
/* Set background color for the sidebar */
|
23 |
+
.sidebar .sidebar-content {
|
24 |
+
background-color: #d1e7dd; /* Slightly darker teal */
|
25 |
+
}
|
26 |
+
/* Set text color for input fields */
|
27 |
+
.stTextInput textarea {
|
28 |
+
color: #000000 !important;
|
29 |
+
}
|
30 |
+
/* Change styles for dropdown menu */
|
31 |
+
.stSelectbox div[data-baseweb="select"] {
|
32 |
+
color: black !important;
|
33 |
+
background-color: #d1e7dd !important;
|
34 |
+
}
|
35 |
+
/* Change color of dropdown icons */
|
36 |
+
.stSelectbox svg {
|
37 |
+
fill: black !important;
|
38 |
+
}
|
39 |
+
/* Change background and text color for dropdown options */
|
40 |
+
.stSelectbox option {
|
41 |
+
background-color: #d1e7dd !important;
|
42 |
+
color: black !important;
|
43 |
+
}
|
44 |
+
/* Change background and text color for dropdown items */
|
45 |
+
div[role="listbox"] div {
|
46 |
+
background-color: #d1e7dd !important;
|
47 |
+
color: black !important;
|
48 |
+
}
|
49 |
+
</style>
|
50 |
+
""", unsafe_allow_html=True)
|
51 |
+
|
52 |
+
# Set the title of the app
|
53 |
+
st.title("π° Finance Assistant")
|
54 |
+
# Add a small description under the title
|
55 |
+
st.caption("π Your AI-Powered Financial Advisor")
|
56 |
+
|
57 |
+
# Create the sidebar with options
|
58 |
+
with st.sidebar:
|
59 |
+
# Add a dividing line
|
60 |
+
st.divider()
|
61 |
+
# Display a section for assistant features
|
62 |
+
st.markdown("### Assistant Capabilities")
|
63 |
+
st.markdown("""
|
64 |
+
- π Investment Analysis
|
65 |
+
- π³ Budgeting Advice
|
66 |
+
- π¦ Loan Guidance
|
67 |
+
- π‘ Retirement Planning
|
68 |
+
""")
|
69 |
+
# Add another dividing line
|
70 |
+
st.divider()
|
71 |
+
# Show a small footer message
|
72 |
+
st.markdown("Built with NVIDIA API | LangChain ")
|
73 |
+
|
74 |
+
# Start the AI client using the API key
|
75 |
+
client = OpenAI(
|
76 |
+
base_url="https://integrate.api.nvidia.com/v1",
|
77 |
+
api_key=api_key_nvidia
|
78 |
+
)
|
79 |
+
|
80 |
+
# Define a message that tells the AI how to respond
|
81 |
+
system_prompt_template = (
|
82 |
+
"You are an expert AI financial assistant. Provide accurate, concise, and empathetic responses "
|
83 |
+
"to user queries related to investments, budgeting, loans, retirement planning, and other financial matters. "
|
84 |
+
"Always respond in English."
|
85 |
+
)
|
86 |
+
|
87 |
+
# Initialize chat history if it doesn't exist
|
88 |
+
if "message_log" not in st.session_state:
|
89 |
+
st.session_state.message_log = [
|
90 |
+
{"role": "assistant", "content": "Hi! I'm your Finance Assistant. How can I assist you today? π°"}
|
91 |
+
]
|
92 |
+
|
93 |
+
# Create a container to display chat messages
|
94 |
+
chat_container = st.container()
|
95 |
+
|
96 |
+
# Show chat messages inside the container
|
97 |
+
with chat_container:
|
98 |
+
for message in st.session_state.message_log:
|
99 |
+
with st.chat_message(message["role"]):
|
100 |
+
st.markdown(message["content"])
|
101 |
+
|
102 |
+
# Input field for user to type questions
|
103 |
+
user_query = st.chat_input("Type your finance-related question here...")
|
104 |
+
|
105 |
+
# Function to get a response from the AI
|
106 |
+
def generate_ai_response(messages):
|
107 |
+
"""
|
108 |
+
Sends the conversation to the AI model and processes the response.
|
109 |
+
"""
|
110 |
+
completion = client.chat.completions.create(
|
111 |
+
model="deepseek-ai/deepseek-r1",
|
112 |
+
messages=messages,
|
113 |
+
temperature=0.5, # Controls randomness of responses
|
114 |
+
top_p=0.5, # Helps control diversity of responses
|
115 |
+
max_tokens=1000, # Maximum length of response
|
116 |
+
stream=True # Enables streaming of responses
|
117 |
+
)
|
118 |
+
|
119 |
+
# Process the AI response piece by piece
|
120 |
+
response = ""
|
121 |
+
for chunk in completion:
|
122 |
+
content = chunk.choices[0].delta.content or ""
|
123 |
+
response += content
|
124 |
+
return response
|
125 |
+
|
126 |
+
# Handle user input and generate AI responses
|
127 |
+
if user_query:
|
128 |
+
# Save the user's message in chat history
|
129 |
+
st.session_state.message_log.append({"role": "user", "content": user_query})
|
130 |
+
|
131 |
+
# Create a list of messages to send to AI
|
132 |
+
messages = [
|
133 |
+
{"role": "system", "content": system_prompt_template}, # First message that tells AI how to behave
|
134 |
+
]
|
135 |
+
|
136 |
+
# Add all previous messages to the conversation
|
137 |
+
for msg in st.session_state.message_log:
|
138 |
+
role = msg["role"]
|
139 |
+
if role == "ai":
|
140 |
+
role = "assistant"
|
141 |
+
messages.append({"role": role, "content": msg["content"]})
|
142 |
+
|
143 |
+
# Show a loading spinner while AI is thinking
|
144 |
+
with st.spinner("π§ Processing..."):
|
145 |
+
# Get the AI response
|
146 |
+
ai_response = generate_ai_response(messages)
|
147 |
+
|
148 |
+
# Save the AI's response in chat history
|
149 |
+
st.session_state.message_log.append({"role": "assistant", "content": ai_response})
|
150 |
+
|
151 |
+
# Refresh the page to show the new chat messages
|
152 |
+
st.rerun()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
langchain_core
|
3 |
+
langchain_community
|
4 |
+
langchain-openai
|