File size: 4,522 Bytes
6daad43
 
 
 
 
 
 
 
 
 
 
 
75b3cda
6daad43
75b3cda
6daad43
 
73edfbf
 
 
 
6daad43
 
 
73edfbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6daad43
 
 
 
73edfbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6daad43
 
 
dde818b
6daad43
75b3cda
dde818b
6daad43
 
75b3cda
73edfbf
 
 
 
 
 
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
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! 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.")