Akshayram1's picture
Update app.py
2e2d75b verified
raw
history blame
3.58 kB
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("GOOGLE_API_KEY")
genai.configure(api_key=api_key)
# Initialize the session state to store chat history
if 'messages' not in st.session_state:
st.session_state['messages'] = []
# 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 <strong> 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.
""")
# Send message and return response
response = chat.send_message(text)
return response.text
# Layout for chatbot UI
st.title("Financial Summary Chatbot")
# Chat history container (This is where the conversation will appear)
chat_container = st.container()
# Input container (This will stay at the bottom)
input_container = st.container()
# Function to display the chat history
def display_chat():
with chat_container:
# Loop through session messages and display them
for message in st.session_state['messages']:
if message['role'] == 'user':
st.write(f"**You:** {message['content']}")
else:
st.write(f"**Bot:** {message['content']}")
# Fixed input area at the bottom using the input container
with input_container:
is_new_session = st.checkbox("Start new session", value=False)
user_input = st.text_area("Type your message here:", height=100)
send_button = st.button("Send")
# If user presses 'Send'
if send_button and user_input:
# Store the user's input
st.session_state['messages'].append({"role": "user", "content": user_input})
# Call the text_summary function to get the bot's response
bot_response = text_summary(user_input, is_new_session)
# Store the bot's response
st.session_state['messages'].append({"role": "bot", "content": bot_response})
# Clear the input text area
user_input = ""
# Display the chat history
display_chat()