nikhil-kumar commited on
Commit
7bda6cd
Β·
verified Β·
1 Parent(s): e3262a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +153 -0
app.py CHANGED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("πŸ’° Financial 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
+ st.markdown("Created with ❀ by Nikhil Kumar")
74
+
75
+ # Start the AI client using the API key
76
+ client = OpenAI(
77
+ base_url="https://integrate.api.nvidia.com/v1",
78
+ api_key=api_key_nvidia
79
+ )
80
+
81
+ # Define a message that tells the AI how to respond
82
+ system_prompt_template = (
83
+ "I am an expert AI financial assistant. Provide accurate, concise, and empathetic responses "
84
+ "to user queries related to investments, budgeting, loans, retirement planning, and other financial matters. "
85
+ "Always respond in English."
86
+ )
87
+
88
+ # Initialize chat history if it doesn't exist
89
+ if "message_log" not in st.session_state:
90
+ st.session_state.message_log = [
91
+ {"role": "assistant", "content": "Hi! I'm your Finance Assistant. How can I assist you today? πŸ’°"}
92
+ ]
93
+
94
+ # Create a container to display chat messages
95
+ chat_container = st.container()
96
+
97
+ # Show chat messages inside the container
98
+ with chat_container:
99
+ for message in st.session_state.message_log:
100
+ with st.chat_message(message["role"]):
101
+ st.markdown(message["content"])
102
+
103
+ # Input field for user to type questions
104
+ user_query = st.chat_input("Type your finance-related question here...")
105
+
106
+ # Function to get a response from the AI
107
+ def generate_ai_response(messages):
108
+ """
109
+ Sends the conversation to the AI model and processes the response.
110
+ """
111
+ completion = client.chat.completions.create(
112
+ model="deepseek-ai/deepseek-r1",
113
+ messages=messages,
114
+ temperature=0.5, # Controls randomness of responses
115
+ top_p=0.5, # Helps control diversity of responses
116
+ max_tokens=1000, # Maximum length of response
117
+ stream=True # Enables streaming of responses
118
+ )
119
+
120
+ # Process the AI response piece by piece
121
+ response = ""
122
+ for chunk in completion:
123
+ content = chunk.choices[0].delta.content or ""
124
+ response += content
125
+ return response
126
+
127
+ # Handle user input and generate AI responses
128
+ if user_query:
129
+ # Save the user's message in chat history
130
+ st.session_state.message_log.append({"role": "user", "content": user_query})
131
+
132
+ # Create a list of messages to send to AI
133
+ messages = [
134
+ {"role": "system", "content": system_prompt_template}, # First message that tells AI how to behave
135
+ ]
136
+
137
+ # Add all previous messages to the conversation
138
+ for msg in st.session_state.message_log:
139
+ role = msg["role"]
140
+ if role == "ai":
141
+ role = "assistant"
142
+ messages.append({"role": role, "content": msg["content"]})
143
+
144
+ # Show a loading spinner while AI is thinking
145
+ with st.spinner("🧠 Processing..."):
146
+ # Get the AI response
147
+ ai_response = generate_ai_response(messages)
148
+
149
+ # Save the AI's response in chat history
150
+ st.session_state.message_log.append({"role": "assistant", "content": ai_response})
151
+
152
+ # Refresh the page to show the new chat messages
153
+ st.rerun()