File size: 4,382 Bytes
8f8cc82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# app.py

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 <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.

        Example format:

        <strong>Company Overview</strong><br/>
        <p>Company Name: {Company Name}</p>
        <p>Description: {Company Description}</p>
        <br/><br/>

        <strong>Stock Performance</strong><br/>
        <p>Apple Inc. (AAPL) is a highly valued stock...</p>
        <br/><br/>

        <strong>Key Metrics</strong><br/>
        <table>
            <tr>
                <th>Metric</th>
                <th>Value</th>
            </tr>
            <tr>
                <td>Market Capitalization</td>
                <td>$2.7 trillion</td>
            </tr>
            <tr>
                <td>Stock Price</td>
                <td>$170 per share</td>
            </tr>
            <tr>
                <td>EPS (TTM)</td>
                <td>$6.15</td>
            </tr>
            <tr>
                <td>P/E Ratio</td>
                <td>24.34</td>
            </tr>
        </table>
        <br/><br/>

        <strong>Growth Prospects</strong><br/>
        <ul>
            <li>iPhone sales growth in emerging markets.</li>
            <li>Expansion of services revenue.</li>
            <li>Increased demand for wearable devices.</li>
            <li>Development of AR/VR technologies.</li>
        </ul>
        <br/><br/>

        <strong>Risks</strong><br/>
        <ul>
            <li>Competition from other technology companies.</li>
            <li>Dependence on iPhone sales.</li>
            <li>Economic downturns.</li>
            <li>Supply chain disruptions and geopolitical risks.</li>
        </ul>
        <br/><br/>

        <strong>Overall</strong><br/>
        <p>Apple Inc. is a financially strong company with a history of innovation...</p>
        <br/><br/>
        """)

    # 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! Enter your text below to get a financial summary.")

# Input area for user text
user_input = st.text_area("Enter your text here:", "")

# Button to submit the text
if st.button("Get Summary"):
    if user_input.strip():
        summary = text_summary(user_input, isNew=True)  # Start a new session each time
        st.write("### Summary:")
        st.markdown(summary, unsafe_allow_html=True)
    else:
        st.warning("Please enter some text to summarize.")

if __name__ == '__main__':
    # Start the Streamlit app
    st.run()