Spaces:
Sleeping
Sleeping
Akshayram1
commited on
Commit
•
489301b
1
Parent(s):
2e2d75b
Update app2.py
Browse files
app2.py
CHANGED
@@ -7,16 +7,15 @@ import os
|
|
7 |
load_dotenv()
|
8 |
|
9 |
# Configure Google Generative AI with API key
|
10 |
-
api_key = os.getenv("
|
11 |
genai.configure(api_key=api_key)
|
12 |
|
13 |
-
# Initialize session state
|
14 |
if 'messages' not in st.session_state:
|
15 |
-
st.session_state
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
st.session_state.chat = None
|
20 |
|
21 |
# Generation configuration and safety settings
|
22 |
generation_config = {
|
@@ -45,109 +44,68 @@ safety_settings = [
|
|
45 |
},
|
46 |
]
|
47 |
|
|
|
48 |
def text_summary(text, isNew=False):
|
49 |
-
|
|
|
|
|
50 |
model = genai.GenerativeModel(
|
51 |
model_name="gemini-pro",
|
52 |
generation_config=generation_config,
|
53 |
safety_settings=safety_settings
|
54 |
)
|
55 |
-
|
56 |
-
|
57 |
-
Act as a financial advisor and generate financial summaries in a structured and tabular format
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
|
|
|
61 |
return response.text
|
62 |
|
63 |
-
#
|
64 |
-
st.markdown("""
|
65 |
-
<style>
|
66 |
-
.chat-container {
|
67 |
-
height: 600px;
|
68 |
-
overflow-y: auto;
|
69 |
-
padding: 20px;
|
70 |
-
margin-bottom: 100px;
|
71 |
-
}
|
72 |
-
.user-message {
|
73 |
-
background-color: #e6f3ff;
|
74 |
-
padding: 10px;
|
75 |
-
border-radius: 10px;
|
76 |
-
margin: 10px 0;
|
77 |
-
max-width: 80%;
|
78 |
-
margin-left: auto;
|
79 |
-
}
|
80 |
-
.bot-message {
|
81 |
-
background-color: #f0f0f0;
|
82 |
-
padding: 10px;
|
83 |
-
border-radius: 10px;
|
84 |
-
margin: 10px 0;
|
85 |
-
max-width: 80%;
|
86 |
-
}
|
87 |
-
.input-container {
|
88 |
-
position: fixed;
|
89 |
-
bottom: 0;
|
90 |
-
left: 0;
|
91 |
-
right: 0;
|
92 |
-
padding: 20px;
|
93 |
-
background-color: white;
|
94 |
-
border-top: 1px solid #ddd;
|
95 |
-
}
|
96 |
-
.stTextInput input {
|
97 |
-
border-radius: 20px;
|
98 |
-
}
|
99 |
-
</style>
|
100 |
-
""", unsafe_allow_html=True)
|
101 |
-
|
102 |
-
# Main title
|
103 |
st.title("Financial Summary Chatbot")
|
104 |
|
105 |
-
# Chat
|
106 |
chat_container = st.container()
|
107 |
|
108 |
-
#
|
109 |
-
|
110 |
-
if message.strip():
|
111 |
-
# Add user message to history
|
112 |
-
st.session_state.messages.append({"role": "user", "content": message})
|
113 |
-
|
114 |
-
# Get bot response
|
115 |
-
response = text_summary(message)
|
116 |
-
|
117 |
-
# Add bot response to history
|
118 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
119 |
|
120 |
-
#
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
with col2:
|
131 |
-
if st.button("Send"):
|
132 |
-
process_message(user_input)
|
133 |
-
# Instead of trying to clear the input directly, we'll use a rerun
|
134 |
-
st.rerun()
|
135 |
-
|
136 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
137 |
|
138 |
-
#
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
-
# Display chat
|
146 |
-
|
147 |
-
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
148 |
-
for message in st.session_state.messages:
|
149 |
-
if message["role"] == "user":
|
150 |
-
st.markdown(f'<div class="user-message">{message["content"]}</div>', unsafe_allow_html=True)
|
151 |
-
else:
|
152 |
-
st.markdown(f'<div class="bot-message">{message["content"]}</div>', unsafe_allow_html=True)
|
153 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
|
|
7 |
load_dotenv()
|
8 |
|
9 |
# Configure Google Generative AI with API key
|
10 |
+
api_key = os.getenv("GOOGLE_API_KEY")
|
11 |
genai.configure(api_key=api_key)
|
12 |
|
13 |
+
# Initialize the session state to store chat history
|
14 |
if 'messages' not in st.session_state:
|
15 |
+
st.session_state['messages'] = []
|
16 |
|
17 |
+
# Global variable to maintain chat session
|
18 |
+
chat = None
|
|
|
19 |
|
20 |
# Generation configuration and safety settings
|
21 |
generation_config = {
|
|
|
44 |
},
|
45 |
]
|
46 |
|
47 |
+
# Function to handle text summary requests
|
48 |
def text_summary(text, isNew=False):
|
49 |
+
global chat
|
50 |
+
|
51 |
+
if isNew or chat is None: # Start a new chat session
|
52 |
model = genai.GenerativeModel(
|
53 |
model_name="gemini-pro",
|
54 |
generation_config=generation_config,
|
55 |
safety_settings=safety_settings
|
56 |
)
|
57 |
+
chat = model.start_chat()
|
58 |
+
chat.send_message("""
|
59 |
+
Act as a financial advisor and generate financial summaries in a structured and tabular format. Follow these guidelines strictly:
|
60 |
+
- Start each section with a clear title in <strong> tags.
|
61 |
+
- For key metrics, use a table with two columns: one for the metric name and one for its value.
|
62 |
+
- Use bullet points only for listing risks and growth prospects.
|
63 |
+
- Ensure each section is clearly separated with line breaks.
|
64 |
+
- Do not use bold or italic formatting (, *), except for the specified HTML tags.
|
65 |
+
""")
|
66 |
|
67 |
+
# Send message and return response
|
68 |
+
response = chat.send_message(text)
|
69 |
return response.text
|
70 |
|
71 |
+
# Layout for chatbot UI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
st.title("Financial Summary Chatbot")
|
73 |
|
74 |
+
# Chat history container (This is where the conversation will appear)
|
75 |
chat_container = st.container()
|
76 |
|
77 |
+
# Input container (This will stay at the bottom)
|
78 |
+
input_container = st.container()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
+
# Function to display the chat history
|
81 |
+
def display_chat():
|
82 |
+
with chat_container:
|
83 |
+
# Loop through session messages and display them
|
84 |
+
for message in st.session_state['messages']:
|
85 |
+
if message['role'] == 'user':
|
86 |
+
st.write(f"**You:** {message['content']}")
|
87 |
+
else:
|
88 |
+
st.write(f"**Bot:** {message['content']}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
+
# Fixed input area at the bottom using the input container
|
91 |
+
with input_container:
|
92 |
+
is_new_session = st.checkbox("Start new session", value=False)
|
93 |
+
user_input = st.text_area("Type your message here:", height=100)
|
94 |
+
send_button = st.button("Send")
|
95 |
+
|
96 |
+
# If user presses 'Send'
|
97 |
+
if send_button and user_input:
|
98 |
+
# Store the user's input
|
99 |
+
st.session_state['messages'].append({"role": "user", "content": user_input})
|
100 |
+
|
101 |
+
# Call the text_summary function to get the bot's response
|
102 |
+
bot_response = text_summary(user_input, is_new_session)
|
103 |
+
|
104 |
+
# Store the bot's response
|
105 |
+
st.session_state['messages'].append({"role": "bot", "content": bot_response})
|
106 |
+
|
107 |
+
# Clear the input text area
|
108 |
+
user_input = ""
|
109 |
|
110 |
+
# Display the chat history
|
111 |
+
display_chat()
|
|
|
|
|
|
|
|
|
|
|
|
|
|