Akshayram1 commited on
Commit
341de20
1 Parent(s): 73edfbf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +143 -140
app.py CHANGED
@@ -1,140 +1,143 @@
1
- import streamlit as st
2
- import google.generativeai as genai
3
- from dotenv import load_dotenv
4
- import os
5
-
6
- # Load environment variables
7
- load_dotenv()
8
-
9
- # Configure Google Generative AI with API key
10
- api_key = os.getenv("GENERATIVEAI_API_KEY")
11
- genai.configure(api_key=api_key)
12
-
13
- # Global variable to maintain chat session
14
- chat = None
15
-
16
- # Generation configuration and safety settings
17
- generation_config = {
18
- "temperature": 0.9,
19
- "top_p": 0.5,
20
- "top_k": 5,
21
- "max_output_tokens": 1000,
22
- }
23
-
24
- safety_settings = [
25
- {
26
- "category": "HARM_CATEGORY_HARASSMENT",
27
- "threshold": "BLOCK_MEDIUM_AND_ABOVE"
28
- },
29
- {
30
- "category": "HARM_CATEGORY_HATE_SPEECH",
31
- "threshold": "BLOCK_MEDIUM_AND_ABOVE"
32
- },
33
- {
34
- "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
35
- "threshold": "BLOCK_MEDIUM_AND_ABOVE"
36
- },
37
- {
38
- "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
39
- "threshold": "BLOCK_MEDIUM_AND_ABOVE"
40
- },
41
- ]
42
-
43
- # Function to handle text summary requests
44
- def text_summary(text, isNew=False):
45
- global chat
46
-
47
- if isNew or chat is None: # Start a new chat session
48
- model = genai.GenerativeModel(
49
- model_name="gemini-pro",
50
- generation_config=generation_config,
51
- safety_settings=safety_settings
52
- )
53
- chat = model.start_chat()
54
- chat.send_message("""
55
- Act as a financial advisor and generate financial summaries in a structured and tabular format. Follow these guidelines strictly:
56
-
57
- - Start each section with a clear title in <strong> tags.
58
- - For key metrics, use a table with two columns: one for the metric name and one for its value.
59
- - Use bullet points only for listing risks and growth prospects.
60
- - Ensure each section is clearly separated with line breaks.
61
- - Do not use bold or italic formatting (, *), except for the specified HTML tags.
62
-
63
- Example format:
64
-
65
- <strong>Company Overview</strong><br/>
66
- <p>Company Name: {Company Name}</p>
67
- <p>Description: {Company Description}</p>
68
- <br/><br/>
69
-
70
- <strong>Stock Performance</strong><br/>
71
- <p>Apple Inc. (AAPL) is a highly valued stock...</p>
72
- <br/><br/>
73
-
74
- <strong>Key Metrics</strong><br/>
75
- <table>
76
- <tr>
77
- <th>Metric</th>
78
- <th>Value</th>
79
- </tr>
80
- <tr>
81
- <td>Market Capitalization</td>
82
- <td>$2.7 trillion</td>
83
- </tr>
84
- <tr>
85
- <td>Stock Price</td>
86
- <td>$170 per share</td>
87
- </tr>
88
- <tr>
89
- <td>EPS (TTM)</td>
90
- <td>$6.15</td>
91
- </tr>
92
- <tr>
93
- <td>P/E Ratio</td>
94
- <td>24.34</td>
95
- </tr>
96
- </table>
97
- <br/><br/>
98
-
99
- <strong>Growth Prospects</strong><br/>
100
- <ul>
101
- <li>iPhone sales growth in emerging markets.</li>
102
- <li>Expansion of services revenue.</li>
103
- <li>Increased demand for wearable devices.</li>
104
- <li>Development of AR/VR technologies.</li>
105
- </ul>
106
- <br/><br/>
107
-
108
- <strong>Risks</strong><br/>
109
- <ul>
110
- <li>Competition from other technology companies.</li>
111
- <li>Dependence on iPhone sales.</li>
112
- <li>Economic downturns.</li>
113
- <li>Supply chain disruptions and geopolitical risks.</li>
114
- </ul>
115
- <br/><br/>
116
-
117
- <strong>Overall</strong><br/>
118
- <p>Apple Inc. is a financially strong company with a history of innovation...</p>
119
- <br/><br/>
120
- """)
121
-
122
- # Send message and return response
123
- response = chat.send_message(text)
124
- return response.text
125
-
126
- # Streamlit UI
127
- st.title("Financial Summary Chatbot")
128
- st.write("Welcome to the Financial Summary Chatbot! Type a message to get a response from the chatbot.")
129
-
130
- # Input area for user text
131
- chat_input = st.text_area("Type a message:", "")
132
-
133
- # Button to submit the text
134
- if st.button("Send"):
135
- if chat_input.strip():
136
- response = text_summary(chat_input)
137
- st.write("### Response:")
138
- st.markdown(response, unsafe_allow_html=True)
139
- else:
140
- st.warning("Please enter a message to send.")
 
 
 
 
1
+ import streamlit as st
2
+ import google.generativeai as genai
3
+ from dotenv import load_dotenv
4
+ import os
5
+
6
+ # Load environment variables
7
+ load_dotenv()
8
+
9
+ # Configure Google Generative AI with API key
10
+ api_key = os.getenv("GENERATIVEAI_API_KEY")
11
+ genai.configure(api_key=api_key)
12
+
13
+ # Initialize session state for message history if it doesn't exist
14
+ if 'messages' not in st.session_state:
15
+ st.session_state.messages = []
16
+
17
+ # Global variable to maintain chat session
18
+ chat = None
19
+
20
+ # Generation configuration and safety settings
21
+ generation_config = {
22
+ "temperature": 0.9,
23
+ "top_p": 0.5,
24
+ "top_k": 5,
25
+ "max_output_tokens": 1000,
26
+ }
27
+
28
+ safety_settings = [
29
+ {
30
+ "category": "HARM_CATEGORY_HARASSMENT",
31
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
32
+ },
33
+ {
34
+ "category": "HARM_CATEGORY_HATE_SPEECH",
35
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
36
+ },
37
+ {
38
+ "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
39
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
40
+ },
41
+ {
42
+ "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
43
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
44
+ },
45
+ ]
46
+
47
+ def text_summary(text, isNew=False):
48
+ global chat
49
+
50
+ if isNew or chat is None:
51
+ model = genai.GenerativeModel(
52
+ model_name="gemini-pro",
53
+ generation_config=generation_config,
54
+ safety_settings=safety_settings
55
+ )
56
+ chat = model.start_chat()
57
+ chat.send_message("""
58
+ Act as a financial advisor and generate financial summaries in a structured and tabular format...
59
+ """) # Your existing prompt here
60
+
61
+ response = chat.send_message(text)
62
+ return response.text
63
+
64
+ # Custom CSS for chat interface
65
+ st.markdown("""
66
+ <style>
67
+ .chat-container {
68
+ height: 600px;
69
+ overflow-y: auto;
70
+ padding: 20px;
71
+ margin-bottom: 100px;
72
+ }
73
+ .user-message {
74
+ background-color: #e6f3ff;
75
+ padding: 10px;
76
+ border-radius: 10px;
77
+ margin: 10px 0;
78
+ max-width: 80%;
79
+ margin-left: auto;
80
+ }
81
+ .bot-message {
82
+ background-color: #f0f0f0;
83
+ padding: 10px;
84
+ border-radius: 10px;
85
+ margin: 10px 0;
86
+ max-width: 80%;
87
+ }
88
+ .input-container {
89
+ position: fixed;
90
+ bottom: 0;
91
+ left: 0;
92
+ right: 0;
93
+ padding: 20px;
94
+ background-color: white;
95
+ border-top: 1px solid #ddd;
96
+ }
97
+ </style>
98
+ """, unsafe_allow_html=True)
99
+
100
+ # Main title
101
+ st.title("Financial Summary Chatbot")
102
+
103
+ # Chat message container
104
+ chat_container = st.container()
105
+
106
+ # Create a container for the input area at the bottom
107
+ with st.container():
108
+ st.markdown('<div class="input-container">', unsafe_allow_html=True)
109
+
110
+ # Create two columns for input and button
111
+ col1, col2 = st.columns([5,1])
112
+
113
+ with col1:
114
+ user_input = st.text_input("Message", key="user_input", label_visibility="collapsed")
115
+
116
+ with col2:
117
+ send_button = st.button("Send")
118
+
119
+ st.markdown('</div>', unsafe_allow_html=True)
120
+
121
+ # Handle sending messages
122
+ if send_button and user_input.strip():
123
+ # Add user message to history
124
+ st.session_state.messages.append({"role": "user", "content": user_input})
125
+
126
+ # Get bot response
127
+ response = text_summary(user_input)
128
+
129
+ # Add bot response to history
130
+ st.session_state.messages.append({"role": "assistant", "content": response})
131
+
132
+ # Clear input
133
+ st.session_state.user_input = ""
134
+
135
+ # Display chat messages in the container
136
+ with chat_container:
137
+ st.markdown('<div class="chat-container">', unsafe_allow_html=True)
138
+ for message in st.session_state.messages:
139
+ if message["role"] == "user":
140
+ st.markdown(f'<div class="user-message">{message["content"]}</div>', unsafe_allow_html=True)
141
+ else:
142
+ st.markdown(f'<div class="bot-message">{message["content"]}</div>', unsafe_allow_html=True)
143
+ st.markdown('</div>', unsafe_allow_html=True)