Spaces:
Sleeping
Sleeping
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, | |
) | |
# 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. | |
""") |