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) # Global variable to maintain chat session 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" }, ] # Function to handle text summary requests def text_summary(text, isNew=False): global chat if isNew or chat is None: # Start a new chat session model = genai.GenerativeModel( model_name="gemini-pro", generation_config=generation_config, safety_settings=safety_settings ) chat = model.start_chat() chat.send_message(""" Act as a financial advisor and generate financial summaries in a structured and tabular format. Follow these guidelines strictly: - Start each section with a clear title in tags. - For key metrics, use a table with two columns: one for the metric name and one for its value. - Use bullet points only for listing risks and growth prospects. - Ensure each section is clearly separated with line breaks. - Do not use bold or italic formatting (, *), except for the specified HTML tags. Example format: Company Overview

Company Name: {Company Name}

Description: {Company Description}



Stock Performance

Apple Inc. (AAPL) is a highly valued stock...



Key Metrics
Metric Value
Market Capitalization $2.7 trillion
Stock Price $170 per share
EPS (TTM) $6.15
P/E Ratio 24.34


Growth Prospects


Risks


Overall

Apple Inc. is a financially strong company with a history of innovation...



""") # Send message and return response response = chat.send_message(text) return response.text # Streamlit UI st.title("Financial Summary Chatbot") st.write("Welcome to the Financial Summary Chatbot! Type a message to get a response from the chatbot.") # Input area for user text chat_input = st.text_area("Type a message:", "") # Button to submit the text if st.button("Send"): if chat_input.strip(): response = text_summary(chat_input) st.write("### Response:") st.markdown(response, unsafe_allow_html=True) else: st.warning("Please enter a message to send.")