File size: 5,684 Bytes
44f095f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import streamlit as st
import asyncio
from datetime import datetime
import sys
import traceback
import re
from email_validator import validate_email, EmailNotValidError

# Add project root to path for imports
sys.path.append(os.path.dirname(os.path.dirname(__file__)))

# Import required modules
from modules.news_pipeline import run_news_summary_pipeline
from modules.email_sender import send_report_via_email

# Page setup
st.set_page_config(
    page_title="Daily Market News Report",
    page_icon="📰",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Initialize session state
if "news_report_initialized" not in st.session_state:
    st.session_state.news_report_initialized = True
    if "newsletter_content" not in st.session_state:
        st.session_state.newsletter_content = None
    if "last_generated" not in st.session_state:
        st.session_state.last_generated = None

# Create main container
main_container = st.container()

with main_container:
    # Page title
    st.title("📰 Daily Market News Report")
    
    # Description
    st.markdown("""
    This feature uses AI to automatically scan, analyze, and summarize the latest financial news from multiple reliable sources.
    The newsletter is updated daily, helping you quickly grasp market developments.
    """)
    
    # Create columns for generate button and timestamp
    col1, col2 = st.columns([4, 8])
    
    with col1:
        # Button to create a new newsletter
        if st.button("🔄 Generate Today's Report", use_container_width=True):
            # Show spinner while processing
            with st.spinner("AI is analyzing news from multiple sources..."):
                try:
                    # Call newsletter pipeline
                    newsletter = asyncio.run(run_news_summary_pipeline())
                    
                    # Save results to session state
                    st.session_state.newsletter_content = newsletter
                    st.session_state.last_generated = datetime.now()
                    
                    # Display success message
                    st.success("Report successfully generated!")
                except Exception as e:
                    st.error(f"An error occurred: {str(e)}")
                    traceback.print_exc()
    
    with col2:
        # Display last generation time
        if st.session_state.last_generated:
            st.info(f"Last updated: {st.session_state.last_generated.strftime('%d/%m/%Y %H:%M')}")
    
    # Display newsletter if available
    if st.session_state.newsletter_content:
        # Create tabs to display newsletter and email form
        tab1, tab2 = st.tabs(["Report", "Email Report"])
        
        with tab1:
            # Display newsletter content
            st.markdown(st.session_state.newsletter_content)
        
        with tab2:
            # Form to send email
            st.markdown("### Send Report via Email")
            
            with st.form(key="email_form"):
                # Email input
                email = st.text_input("Enter email address to receive the report")
                
                # Submit button
                submit_button = st.form_submit_button(label="📩 Send Report via Email")
                
                if submit_button:
                    # Validate email
                    if not email:
                        st.error("Please enter an email address.")
                    else:
                        try:
                            # Validate email format
                            validate_email(email)
                            
                            # Show spinner while sending
                            with st.spinner("Sending email..."):
                                # Call send email function
                                success, message = send_report_via_email(
                                    st.session_state.newsletter_content,
                                    email
                                )
                                
                                # Display result
                                if success:
                                    st.success(message)
                                else:
                                    st.error(message)
                        except EmailNotValidError:
                            st.error("Invalid email address.")
                        except Exception as e:
                            st.error(f"An error occurred: {str(e)}")
            
            # Add privacy note
            st.info("📝 **Note:** Your email is only used to send this report and is not stored.")
    else:
        # Display note if no newsletter available
        st.info("👆 Click 'Generate Today's Report' to start analyzing the news.")
        
        # Add explanation about process
        with st.expander("How it works"):
            st.markdown("""
            ### Report Generation Process:
            
            1. **Data Collection**: The system automatically scans the latest financial news from multiple reliable sources.
            
            2. **Analysis & Filtering**: AI removes duplicate and irrelevant news, keeping only the most important information.
            
            3. **Summarization**: Each article is summarized into key points, helping you quickly grasp the information.
            
            4. **Synthesis**: AI organizes information by topics and writes it into a well-structured report.
            
            5. **Display & Share**: The final report is displayed on the web and can be sent via email upon request.
            """)