Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	
		Sigrid De los Santos
		
	commited on
		
		
					Commit 
							
							Β·
						
						c319a1e
	
1
								Parent(s):
							
							4b24cc9
								
App is ready
Browse files- app.py +53 -25
- src/main.py +2 -2
    	
        app.py
    CHANGED
    
    | @@ -2,10 +2,12 @@ import os | |
| 2 | 
             
            import sys
         | 
| 3 | 
             
            import tempfile
         | 
| 4 | 
             
            import time
         | 
|  | |
| 5 | 
             
            import streamlit as st
         | 
| 6 | 
             
            import pandas as pd
         | 
| 7 | 
             
            import requests
         | 
| 8 | 
             
            import openai
         | 
|  | |
| 9 |  | 
| 10 | 
             
            # Add 'src' to Python path
         | 
| 11 | 
             
            sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
         | 
| @@ -56,24 +58,40 @@ if submitted: | |
| 56 | 
             
                    spinner_box = st.empty()
         | 
| 57 | 
             
                    log_box = st.empty()
         | 
| 58 | 
             
                    logs = []
         | 
|  | |
| 59 |  | 
| 60 | 
             
                    def log(msg):
         | 
| 61 | 
             
                        logs.append(msg)
         | 
| 62 | 
             
                        log_box.code("\n".join(logs))
         | 
| 63 |  | 
| 64 | 
            -
                     | 
| 65 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 66 |  | 
| 67 | 
            -
             | 
|  | |
| 68 | 
             
                        try:
         | 
| 69 | 
             
                            client = openai.OpenAI(api_key=openai_api_key)
         | 
| 70 | 
             
                            client.models.list()
         | 
| 71 | 
             
                            log("β
 OpenAI API key is valid.")
         | 
| 72 | 
             
                        except Exception as e:
         | 
| 73 | 
             
                            log(f"β OpenAI API Key Error: {e}")
         | 
|  | |
|  | |
| 74 | 
             
                            st.stop()
         | 
| 75 |  | 
| 76 | 
            -
                        # === Check Tavily Key ===
         | 
| 77 | 
             
                        try:
         | 
| 78 | 
             
                            response = requests.post(
         | 
| 79 | 
             
                                "https://api.tavily.com/search",
         | 
| @@ -84,50 +102,58 @@ if submitted: | |
| 84 | 
             
                                log("β
 Tavily API key is valid.")
         | 
| 85 | 
             
                            else:
         | 
| 86 | 
             
                                log(f"β Tavily Key Error: {response.status_code} {response.text}")
         | 
|  | |
|  | |
| 87 | 
             
                                st.stop()
         | 
| 88 | 
             
                        except Exception as e:
         | 
| 89 | 
             
                            log(f"β Tavily API Key Error: {e}")
         | 
|  | |
|  | |
| 90 | 
             
                            st.stop()
         | 
| 91 |  | 
| 92 | 
            -
                         | 
| 93 | 
            -
             | 
|  | |
|  | |
|  | |
| 94 | 
             
                        spinner_box.success("β
 Analysis complete!")
         | 
| 95 |  | 
| 96 | 
             
                        # === Report Tab ===
         | 
| 97 | 
            -
             | 
| 98 | 
            -
             | 
| 99 | 
            -
                        # === Articles Tab ===
         | 
| 100 | 
             
                        with tab_report:
         | 
|  | |
| 101 | 
             
                            if html_paths:
         | 
| 102 | 
            -
                                # Add one button to download the latest report
         | 
| 103 | 
             
                                latest_report = html_paths[-1]
         | 
| 104 | 
             
                                with open(latest_report, 'r', encoding='utf-8') as f:
         | 
| 105 | 
             
                                    html_content = f.read()
         | 
| 106 |  | 
|  | |
| 107 | 
             
                                st.download_button(
         | 
| 108 | 
            -
                                    label= | 
| 109 | 
             
                                    data=html_content,
         | 
| 110 | 
             
                                    file_name=os.path.basename(latest_report),
         | 
| 111 | 
             
                                    mime="text/html"
         | 
| 112 | 
             
                                )
         | 
| 113 |  | 
| 114 | 
            -
                                # Display the report
         | 
| 115 | 
             
                                st.components.v1.html(html_content, height=600, scrolling=True)
         | 
| 116 | 
             
                            else:
         | 
| 117 | 
             
                                st.error("β No reports were generated.")
         | 
| 118 | 
            -
             | 
| 119 | 
            -
                        # | 
| 120 | 
            -
                         | 
| 121 | 
            -
             | 
| 122 | 
            -
             | 
| 123 | 
            -
             | 
| 124 | 
            -
             | 
| 125 | 
            -
             | 
| 126 | 
            -
             | 
| 127 | 
            -
             | 
| 128 | 
            -
             | 
| 129 | 
            -
             | 
| 130 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
| 131 |  | 
| 132 | 
             
                        # === Insights Tab ===
         | 
| 133 | 
             
                        with tab_insights:
         | 
| @@ -144,6 +170,8 @@ if submitted: | |
| 144 | 
             
                                st.info("No insights available.")
         | 
| 145 |  | 
| 146 | 
             
                    except Exception as e:
         | 
|  | |
|  | |
| 147 | 
             
                        spinner_box.error("β Failed.")
         | 
| 148 | 
             
                        log_box.error(f"β Error: {e}")
         | 
| 149 |  | 
|  | |
| 2 | 
             
            import sys
         | 
| 3 | 
             
            import tempfile
         | 
| 4 | 
             
            import time
         | 
| 5 | 
            +
            import itertools
         | 
| 6 | 
             
            import streamlit as st
         | 
| 7 | 
             
            import pandas as pd
         | 
| 8 | 
             
            import requests
         | 
| 9 | 
             
            import openai
         | 
| 10 | 
            +
            from threading import Thread
         | 
| 11 |  | 
| 12 | 
             
            # Add 'src' to Python path
         | 
| 13 | 
             
            sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
         | 
|  | |
| 58 | 
             
                    spinner_box = st.empty()
         | 
| 59 | 
             
                    log_box = st.empty()
         | 
| 60 | 
             
                    logs = []
         | 
| 61 | 
            +
                    rotating = True
         | 
| 62 |  | 
| 63 | 
             
                    def log(msg):
         | 
| 64 | 
             
                        logs.append(msg)
         | 
| 65 | 
             
                        log_box.code("\n".join(logs))
         | 
| 66 |  | 
| 67 | 
            +
                    # === Rotating UI Messages ===
         | 
| 68 | 
            +
                    def rotating_messages():
         | 
| 69 | 
            +
                        messages = itertools.cycle([
         | 
| 70 | 
            +
                            "π Searching financial news...",
         | 
| 71 | 
            +
                            "π§  Running AI analysis...",
         | 
| 72 | 
            +
                            "π Evaluating sentiment...",
         | 
| 73 | 
            +
                            "π Generating report...",
         | 
| 74 | 
            +
                            "πΉ Finalizing insights..."
         | 
| 75 | 
            +
                        ])
         | 
| 76 | 
            +
                        while rotating:
         | 
| 77 | 
            +
                            spinner_box.markdown(f"β³ {next(messages)}")
         | 
| 78 | 
            +
                            time.sleep(1.5)
         | 
| 79 | 
            +
             | 
| 80 | 
            +
                    rotator_thread = Thread(target=rotating_messages)
         | 
| 81 | 
            +
                    rotator_thread.start()
         | 
| 82 |  | 
| 83 | 
            +
                    try:
         | 
| 84 | 
            +
                        # Check API Keys
         | 
| 85 | 
             
                        try:
         | 
| 86 | 
             
                            client = openai.OpenAI(api_key=openai_api_key)
         | 
| 87 | 
             
                            client.models.list()
         | 
| 88 | 
             
                            log("β
 OpenAI API key is valid.")
         | 
| 89 | 
             
                        except Exception as e:
         | 
| 90 | 
             
                            log(f"β OpenAI API Key Error: {e}")
         | 
| 91 | 
            +
                            rotating = False
         | 
| 92 | 
            +
                            rotator_thread.join()
         | 
| 93 | 
             
                            st.stop()
         | 
| 94 |  | 
|  | |
| 95 | 
             
                        try:
         | 
| 96 | 
             
                            response = requests.post(
         | 
| 97 | 
             
                                "https://api.tavily.com/search",
         | 
|  | |
| 102 | 
             
                                log("β
 Tavily API key is valid.")
         | 
| 103 | 
             
                            else:
         | 
| 104 | 
             
                                log(f"β Tavily Key Error: {response.status_code} {response.text}")
         | 
| 105 | 
            +
                                rotating = False
         | 
| 106 | 
            +
                                rotator_thread.join()
         | 
| 107 | 
             
                                st.stop()
         | 
| 108 | 
             
                        except Exception as e:
         | 
| 109 | 
             
                            log(f"β Tavily API Key Error: {e}")
         | 
| 110 | 
            +
                            rotating = False
         | 
| 111 | 
            +
                            rotator_thread.join()
         | 
| 112 | 
             
                            st.stop()
         | 
| 113 |  | 
| 114 | 
            +
                        with st.spinner("β³ Running analysis..."):
         | 
| 115 | 
            +
                            html_paths, articles_df, insights_df = run_pipeline(csv_path, tavily_api_key, progress_callback=log)
         | 
| 116 | 
            +
             | 
| 117 | 
            +
                        rotating = False
         | 
| 118 | 
            +
                        rotator_thread.join()
         | 
| 119 | 
             
                        spinner_box.success("β
 Analysis complete!")
         | 
| 120 |  | 
| 121 | 
             
                        # === Report Tab ===
         | 
|  | |
|  | |
|  | |
| 122 | 
             
                        with tab_report:
         | 
| 123 | 
            +
                            st.subheader("π Latest Report")
         | 
| 124 | 
             
                            if html_paths:
         | 
|  | |
| 125 | 
             
                                latest_report = html_paths[-1]
         | 
| 126 | 
             
                                with open(latest_report, 'r', encoding='utf-8') as f:
         | 
| 127 | 
             
                                    html_content = f.read()
         | 
| 128 |  | 
| 129 | 
            +
                                # Download button for HTML report
         | 
| 130 | 
             
                                st.download_button(
         | 
| 131 | 
            +
                                    label="β¬οΈ Download Report (HTML)",
         | 
| 132 | 
             
                                    data=html_content,
         | 
| 133 | 
             
                                    file_name=os.path.basename(latest_report),
         | 
| 134 | 
             
                                    mime="text/html"
         | 
| 135 | 
             
                                )
         | 
| 136 |  | 
|  | |
| 137 | 
             
                                st.components.v1.html(html_content, height=600, scrolling=True)
         | 
| 138 | 
             
                            else:
         | 
| 139 | 
             
                                st.error("β No reports were generated.")
         | 
| 140 | 
            +
             | 
| 141 | 
            +
                        # === Articles Tab ===
         | 
| 142 | 
            +
                        with tab_articles:
         | 
| 143 | 
            +
                            st.subheader("π Articles Table")
         | 
| 144 | 
            +
                            if not articles_df.empty:
         | 
| 145 | 
            +
                                st.dataframe(
         | 
| 146 | 
            +
                                    articles_df[["Title", "URL", "Priority", "Sentiment", "Confidence", "Signal", "Date"]],
         | 
| 147 | 
            +
                                    use_container_width=True
         | 
| 148 | 
            +
                                )
         | 
| 149 | 
            +
                                st.download_button(
         | 
| 150 | 
            +
                                    label="β¬οΈ Download Articles CSV",
         | 
| 151 | 
            +
                                    data=articles_df.to_csv(index=False).encode("utf-8"),
         | 
| 152 | 
            +
                                    file_name="articles.csv",
         | 
| 153 | 
            +
                                    mime="text/csv"
         | 
| 154 | 
            +
                                )
         | 
| 155 | 
            +
                            else:
         | 
| 156 | 
            +
                                st.info("No articles available.")
         | 
| 157 |  | 
| 158 | 
             
                        # === Insights Tab ===
         | 
| 159 | 
             
                        with tab_insights:
         | 
|  | |
| 170 | 
             
                                st.info("No insights available.")
         | 
| 171 |  | 
| 172 | 
             
                    except Exception as e:
         | 
| 173 | 
            +
                        rotating = False
         | 
| 174 | 
            +
                        rotator_thread.join()
         | 
| 175 | 
             
                        spinner_box.error("β Failed.")
         | 
| 176 | 
             
                        log_box.error(f"β Error: {e}")
         | 
| 177 |  | 
    	
        src/main.py
    CHANGED
    
    | @@ -45,8 +45,8 @@ def run_value_investing_analysis(csv_path, progress_callback=None): | |
| 45 | 
             
                for _, row in current_df.iterrows():
         | 
| 46 | 
             
                    topic = row.get("topic")
         | 
| 47 | 
             
                    timespan = row.get("timespan_days", 7)
         | 
| 48 | 
            -
                    if progress_callback:
         | 
| 49 | 
            -
             | 
| 50 | 
             
                    # try:
         | 
| 51 | 
             
                    #     news = fetch_deep_news(topic, timespan)
         | 
| 52 | 
             
                    #     if progress_callback:
         | 
|  | |
| 45 | 
             
                for _, row in current_df.iterrows():
         | 
| 46 | 
             
                    topic = row.get("topic")
         | 
| 47 | 
             
                    timespan = row.get("timespan_days", 7)
         | 
| 48 | 
            +
                    # if progress_callback:
         | 
| 49 | 
            +
                    #     progress_callback(f"π Processing topic: {topic} ({timespan} days)")
         | 
| 50 | 
             
                    # try:
         | 
| 51 | 
             
                    #     news = fetch_deep_news(topic, timespan)
         | 
| 52 | 
             
                    #     if progress_callback:
         |