Akshayram1 commited on
Commit
a1e3c3f
·
verified ·
1 Parent(s): 331c42a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -122
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import streamlit as st
2
  import google.generativeai as genai
3
  from dotenv import load_dotenv
@@ -10,143 +12,144 @@ load_dotenv()
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! Enter your text below to get a financial summary.")
129
 
130
- # Input area for user text
131
- user_input = st.text_area("Enter your text here:", "")
 
 
 
 
 
132
 
133
  # Button to submit the text
134
  if st.button("Get Summary"):
135
-   if user_input.strip():
136
-     summary = text_summary(user_input, isNew=True) # Start a new session each time
137
-     st.write("### Summary:")
138
-     st.markdown(summary, unsafe_allow_html=True)
139
-   else:
140
-     st.warning("Please enter some text to summarize.")
141
-
142
- # Chatbot UI
143
- st.write("### Chatbot")
144
- st.write("Type a message to get a response from the chatbot.")
145
- chat_input = st.text_area("Type a message:", "")
146
- if st.button("Send"):
147
-   if chat_input.strip():
148
-     response = text_summary(chat_input)
149
-     st.write("### Response:")
150
-     st.markdown(response, unsafe_allow_html=True)
151
-   else:
152
-     st.warning("Please enter a message to send.")
 
1
+ # app.py
2
+
3
  import streamlit as st
4
  import google.generativeai as genai
5
  from dotenv import load_dotenv
 
12
  api_key = os.getenv("GENERATIVEAI_API_KEY")
13
  genai.configure(api_key=api_key)
14
 
15
+ # Global variable to maintain chat session and history
16
  chat = None
17
+ if 'chat_history' not in st.session_state:
18
+ st.session_state.chat_history = [] # Initialize chat history
19
+ if 'input' not in st.session_state:
20
+ st.session_state.input = "" # Initialize input state
21
+
22
  # Generation configuration and safety settings
23
  generation_config = {
24
+ "temperature": 0.9,
25
+ "top_p": 0.5,
26
+ "top_k": 5,
27
+ "max_output_tokens": 1000,
28
  }
29
 
30
  safety_settings = [
31
+ {
32
+ "category": "HARM_CATEGORY_HARASSMENT",
33
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
34
+ },
35
+ {
36
+ "category": "HARM_CATEGORY_HATE_SPEECH",
37
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
38
+ },
39
+ {
40
+ "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
41
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
42
+ },
43
+ {
44
+ "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
45
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
46
+ },
47
  ]
48
 
49
  # Function to handle text summary requests
50
  def text_summary(text, isNew=False):
51
+ global chat
52
+
53
+ if isNew or chat is None: # Start a new chat session
54
+ model = genai.GenerativeModel(
55
+ model_name="gemini-pro",
56
+ generation_config=generation_config,
57
+ safety_settings=safety_settings
58
+ )
59
+ chat = model.start_chat()
60
+ chat.send_message("""
61
+ Act as a financial advisor and generate financial summaries in a structured and tabular format. Follow these guidelines strictly:
62
+
63
+ - Start each section with a clear title in <strong> tags.
64
+ - For key metrics, use a table with two columns: one for the metric name and one for its value.
65
+ - Use bullet points only for listing risks and growth prospects.
66
+ - Ensure each section is clearly separated with line breaks.
67
+ - Do not use bold or italic formatting (, *), except for the specified HTML tags.
68
+
69
+ Example format:
70
+
71
+ <strong>Company Overview</strong><br/>
72
+ <p>Company Name: {Company Name}</p>
73
+ <p>Description: {Company Description}</p>
74
+ <br/><br/>
75
+
76
+ <strong>Stock Performance</strong><br/>
77
+ <p>Apple Inc. (AAPL) is a highly valued stock...</p>
78
+ <br/><br/>
79
+
80
+ <strong>Key Metrics</strong><br/>
81
+ <table>
82
+ <tr>
83
+ <th>Metric</th>
84
+ <th>Value</th>
85
+ </tr>
86
+ <tr>
87
+ <td>Market Capitalization</td>
88
+ <td>$2.7 trillion</td>
89
+ </tr>
90
+ <tr>
91
+ <td>Stock Price</td>
92
+ <td>$170 per share</td>
93
+ </tr>
94
+ <tr>
95
+ <td>EPS (TTM)</td>
96
+ <td>$6.15</td>
97
+ </tr>
98
+ <tr>
99
+ <td>P/E Ratio</td>
100
+ <td>24.34</td>
101
+ </tr>
102
+ </table>
103
+ <br/><br/>
104
+
105
+ <strong>Growth Prospects</strong><br/>
106
+ <ul>
107
+ <li>iPhone sales growth in emerging markets.</li>
108
+ <li>Expansion of services revenue.</li>
109
+ <li>Increased demand for wearable devices.</li>
110
+ <li>Development of AR/VR technologies.</li>
111
+ </ul>
112
+ <br/><br/>
113
+
114
+ <strong>Risks</strong><br/>
115
+ <ul>
116
+ <li>Competition from other technology companies.</li>
117
+ <li>Dependence on iPhone sales.</li>
118
+ <li>Economic downturns.</li>
119
+ <li>Supply chain disruptions and geopolitical risks.</li>
120
+ </ul>
121
+ <br/><br/>
122
+
123
+ <strong>Overall</strong><br/>
124
+ <p>Apple Inc. is a financially strong company with a history of innovation...</p>
125
+ <br/><br/>
126
+ """)
127
+
128
+ # Send message and return response
129
+ response = chat.send_message(text)
130
+ return response.text
131
 
132
  # Streamlit UI
133
  st.title("Financial Summary Chatbot")
134
  st.write("Welcome to the Financial Summary Chatbot! Enter your text below to get a financial summary.")
135
 
136
+ # Display chat history at the top
137
+ if st.session_state.chat_history:
138
+ for entry in st.session_state.chat_history:
139
+ st.markdown(entry, unsafe_allow_html=True)
140
+
141
+ # Input area for user text at the bottom
142
+ user_input = st.text_area("Enter your text here:", value=st.session_state.input)
143
 
144
  # Button to submit the text
145
  if st.button("Get Summary"):
146
+ if user_input.strip():
147
+ summary = text_summary(user_input, isNew=True) # Start a new session each time
148
+ # Add user input and summary to chat history
149
+ st.session_state.chat_history.append(f"<strong>You:</strong><br/>{user_input}<br/><br/>")
150
+ st.session_state.chat_history.append(f"<strong>Bot:</strong><br/>{summary}<br/><br/>")
151
+ # Clear the text area by resetting the input session state
152
+ st.session_state.input = "" # Reset input state to empty
153
+ st.experimental_rerun() # Rerun the script to refresh the input field
154
+ else:
155
+ st.warning("Please enter some text to summarize.")