import streamlit as st import google.generativeai as genai from dotenv import load_dotenv import os # Load environment variables load_dotenv() # Configure Google Generative AI with API key api_key = os.getenv("GENERATIVEAI_API_KEY") genai.configure(api_key=api_key) # Initialize session state for message history if it doesn't exist if 'messages' not in st.session_state: st.session_state.messages = [] # Initialize session state for chat if 'chat' not in st.session_state: st.session_state.chat = None # Generation configuration and safety settings generation_config = { "temperature": 0.9, "top_p": 0.5, "top_k": 5, "max_output_tokens": 1000, } safety_settings = [ { "category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE" }, { "category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE" }, { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE" }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE" }, ] def text_summary(text, isNew=False): if isNew or st.session_state.chat is None: model = genai.GenerativeModel( model_name="gemini-pro", generation_config=generation_config, safety_settings=safety_settings ) st.session_state.chat = model.start_chat() st.session_state.chat.send_message(""" Act as a financial advisor and generate financial summaries in a structured and tabular format... """) # Your existing prompt here response = st.session_state.chat.send_message(text) return response.text # Custom CSS for chat interface st.markdown(""" """, unsafe_allow_html=True) # Main title st.title("Financial Summary Chatbot") # Chat message container chat_container = st.container() # Function to process the message def process_message(message: str): if message.strip(): # Add user message to history st.session_state.messages.append({"role": "user", "content": message}) # Get bot response response = text_summary(message) # Add bot response to history st.session_state.messages.append({"role": "assistant", "content": response}) # Create a container for the input area at the bottom with st.container(): st.markdown('