Akshayram1 commited on
Commit
2e2d75b
·
verified ·
1 Parent(s): e33bd8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -74
app.py CHANGED
@@ -10,6 +10,10 @@ load_dotenv()
10
  api_key = os.getenv("GOOGLE_API_KEY")
11
  genai.configure(api_key=api_key)
12
 
 
 
 
 
13
  # Global variable to maintain chat session
14
  chat = None
15
 
@@ -53,88 +57,55 @@ def text_summary(text, isNew=False):
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 app layout
127
- st.title("Financial Summary Generator")
128
- st.write("Enter the text you want summarized and select if you want to start a new session.")
129
-
130
- # Input text and options
131
- input_text = st.text_area("Enter text to summarize:")
132
- start_new = st.checkbox("Start new session", value=False)
133
-
134
- if st.button("Generate Summary"):
135
- if input_text:
136
- # Generate the summary using the text_summary function
137
- result = text_summary(input_text, start_new)
138
- st.markdown(result, unsafe_allow_html=True)
139
- else:
140
- st.write("Please enter some text to summarize.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  api_key = os.getenv("GOOGLE_API_KEY")
11
  genai.configure(api_key=api_key)
12
 
13
+ # Initialize the session state to store chat history
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
 
 
57
  chat = model.start_chat()
58
  chat.send_message("""
59
  Act as a financial advisor and generate financial summaries in a structured and tabular format. Follow these guidelines strictly:
 
60
  - Start each section with a clear title in <strong> tags.
61
  - For key metrics, use a table with two columns: one for the metric name and one for its value.
62
  - Use bullet points only for listing risks and growth prospects.
63
  - Ensure each section is clearly separated with line breaks.
64
  - Do not use bold or italic formatting (, *), except for the specified HTML tags.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  """)
66
 
67
  # Send message and return response
68
  response = chat.send_message(text)
69
  return response.text
70
 
71
+ # Layout for chatbot UI
72
+ st.title("Financial Summary Chatbot")
73
+
74
+ # Chat history container (This is where the conversation will appear)
75
+ chat_container = st.container()
76
+
77
+ # Input container (This will stay at the bottom)
78
+ input_container = st.container()
79
+
80
+ # Function to display the chat history
81
+ def display_chat():
82
+ with chat_container:
83
+ # Loop through session messages and display them
84
+ for message in st.session_state['messages']:
85
+ if message['role'] == 'user':
86
+ st.write(f"**You:** {message['content']}")
87
+ else:
88
+ st.write(f"**Bot:** {message['content']}")
89
+
90
+ # Fixed input area at the bottom using the input container
91
+ with input_container:
92
+ is_new_session = st.checkbox("Start new session", value=False)
93
+ user_input = st.text_area("Type your message here:", height=100)
94
+ send_button = st.button("Send")
95
+
96
+ # If user presses 'Send'
97
+ if send_button and user_input:
98
+ # Store the user's input
99
+ st.session_state['messages'].append({"role": "user", "content": user_input})
100
+
101
+ # Call the text_summary function to get the bot's response
102
+ bot_response = text_summary(user_input, is_new_session)
103
+
104
+ # Store the bot's response
105
+ st.session_state['messages'].append({"role": "bot", "content": bot_response})
106
+
107
+ # Clear the input text area
108
+ user_input = ""
109
+
110
+ # Display the chat history
111
+ display_chat()