-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
768 lines (645 loc) · 23.2 KB
/
Copy pathapp.py
File metadata and controls
768 lines (645 loc) · 23.2 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
import streamlit as st
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
from modules.query_analyzer import QueryAnalyzer
from modules.research_module import ResearchModule
from modules.validation_module import ValidationModule
from modules.synthesis_module import SynthesisModule
from utils.logger import logger
from config import OPENAI_API_KEY
# Page configuration
st.set_page_config(
page_title="Modular Equity Research System",
page_icon="📊",
layout="wide",
initial_sidebar_state="expanded"
)
# Custom CSS
st.markdown("""
<style>
/* ===== MAIN CONTAINER ===== */
.stApp {
background-color: #0d1117;
color: #e6edf3;
}
/* ===== HEADERS - High Contrast ===== */
.main-header {
font-size: 2.5rem;
font-weight: bold;
color: #ffffff; /* Pure white for main header */
text-align: center;
margin-bottom: 0.5rem;
text-shadow: 0 0 10px rgba(88, 166, 255, 0.3);
}
.sub-header {
text-align: center;
color: #c9d1d9; /* Lighter gray for better readability */
margin-bottom: 2rem;
font-size: 1.1rem;
}
/* ===== MODULE STATUS BOXES ===== */
.module-status {
padding: 1rem;
border-radius: 0.5rem;
margin: 0.5rem 0;
background-color: #21262d;
border: 1px solid #30363d;
}
.confidence-high {
background-color: #1a472a; /* Darker green background */
border-left: 4px solid #2ea043;
color: #a5f3a5; /* Brighter green text */
font-weight: 500;
}
.confidence-medium {
background-color: #4d3800; /* Darker yellow background */
border-left: 4px solid #d29922;
color: #ffdf6b; /* Brighter yellow text */
font-weight: 500;
}
.confidence-low {
background-color: #4c1f1f; /* Darker red background */
border-left: 4px solid #f85149;
color: #ffa198; /* Brighter red text */
font-weight: 500;
}
/* ===== METRIC CARDS - Better Contrast ===== */
.metric-card {
background-color: #21262d;
padding: 1rem;
border-radius: 0.5rem;
text-align: center;
border: 1px solid #30363d;
}
/* ===== EXPANDERS - More Visible ===== */
.streamlit-expanderHeader {
background-color: #21262d !important;
border: 1px solid #444c56 !important;
color: #e6edf3 !important;
font-weight: 500 !important;
}
.streamlit-expanderHeader:hover {
background-color: #30363d !important;
border-color: #58a6ff !important;
}
/* ===== TEXT INPUTS - Better Visibility ===== */
.stTextInput > div > div > input,
.stTextArea > div > div > textarea {
background-color: #161b22;
color: #e6edf3;
border: 1px solid #444c56;
font-size: 1rem;
}
.stTextInput > div > div > input:focus,
.stTextArea > div > div > textarea:focus {
border-color: #58a6ff;
box-shadow: 0 0 0 1px #58a6ff;
}
/* ===== BUTTONS - High Contrast ===== */
.stButton > button {
background-color: #238636;
color: #ffffff;
border: 1px solid #2ea043;
font-weight: 500;
font-size: 1rem;
}
.stButton > button:hover {
background-color: #2ea043;
border: 1px solid #3fb950;
box-shadow: 0 0 10px rgba(46, 160, 67, 0.4);
}
.stButton > button[kind="primary"] {
background-color: #1f6feb;
border-color: #58a6ff;
}
.stButton > button[kind="primary"]:hover {
background-color: #58a6ff;
box-shadow: 0 0 10px rgba(88, 166, 255, 0.4);
}
/* ===== SIDEBAR - Better Contrast ===== */
section[data-testid="stSidebar"] {
background-color: #0d1117;
border-right: 1px solid #30363d;
}
section[data-testid="stSidebar"] .stMarkdown {
color: #e6edf3;
}
section[data-testid="stSidebar"] h2,
section[data-testid="stSidebar"] h3 {
color: #ffffff;
}
/* ===== ALERT BOXES - High Visibility ===== */
.stAlert {
background-color: #21262d;
border: 1px solid #444c56;
color: #e6edf3;
}
.stSuccess {
background-color: #1a472a;
border-left: 4px solid #2ea043;
color: #a5f3a5;
}
.stWarning {
background-color: #4d3800;
border-left: 4px solid #d29922;
color: #ffdf6b;
}
.stError {
background-color: #4c1f1f;
border-left: 4px solid #f85149;
color: #ffa198;
}
.stInfo {
background-color: #1c2d41;
border-left: 4px solid #58a6ff;
color: #79c0ff;
}
/* ===== MARKDOWN CONTENT - Better Readability ===== */
.stMarkdown {
color: #e6edf3;
}
.stMarkdown h1,
.stMarkdown h2,
.stMarkdown h3 {
color: #ffffff;
font-weight: 600;
}
.stMarkdown p {
color: #e6edf3;
line-height: 1.6;
}
.stMarkdown strong {
color: #ffffff;
font-weight: 600;
}
/* ===== LINKS - Bright and Visible ===== */
a {
color: #58a6ff !important;
text-decoration: none;
font-weight: 500;
}
a:hover {
color: #79c0ff !important;
text-decoration: underline;
}
/* ===== PROGRESS BAR ===== */
.stProgress > div > div > div {
background-color: #2ea043;
}
.stProgress > div > div {
background-color: #21262d;
}
/* ===== METRICS - High Contrast ===== */
[data-testid="stMetricValue"] {
color: #58a6ff;
font-size: 2rem;
font-weight: 600;
}
[data-testid="stMetricLabel"] {
color: #c9d1d9;
font-size: 1rem;
font-weight: 500;
}
[data-testid="stMetricDelta"] {
color: #2ea043;
}
/* ===== RADIO BUTTONS ===== */
.stRadio > label {
color: #e6edf3;
font-weight: 500;
}
.stRadio div[role="radiogroup"] label {
color: #e6edf3;
}
/* ===== CODE BLOCKS ===== */
code {
background-color: #161b22;
color: #ff7b72;
padding: 2px 6px;
border-radius: 3px;
border: 1px solid #30363d;
font-size: 0.9em;
}
pre {
background-color: #161b22;
border: 1px solid #30363d;
border-radius: 6px;
padding: 1rem;
}
/* ===== TABLES ===== */
table {
color: #e6edf3;
border-color: #30363d;
}
thead tr {
background-color: #161b22;
}
tbody tr:hover {
background-color: #21262d;
}
/* ===== DIVIDERS ===== */
hr {
border-color: #30363d;
}
/* ===== SCROLLBAR ===== */
::-webkit-scrollbar {
width: 12px;
height: 12px;
}
::-webkit-scrollbar-track {
background: #0d1117;
}
::-webkit-scrollbar-thumb {
background: #30363d;
border-radius: 6px;
border: 2px solid #0d1117;
}
::-webkit-scrollbar-thumb:hover {
background: #484f58;
}
/* ===== CAPTIONS ===== */
.caption {
color: #8b949e;
font-size: 0.9rem;
}
/* ===== SELECTBOX & MULTISELECT ===== */
.stSelectbox > div > div,
.stMultiSelect > div > div {
background-color: #161b22;
color: #e6edf3;
border: 1px solid #444c56;
}
/* ===== NUMBER INPUT ===== */
.stNumberInput > div > div > input {
background-color: #161b22;
color: #e6edf3;
border: 1px solid #444c56;
}
/* ===== DATE INPUT ===== */
.stDateInput > div > div > input {
background-color: #161b22;
color: #e6edf3;
border: 1px solid #444c56;
}
/* ===== FILE UPLOADER ===== */
.stFileUploader {
background-color: #161b22;
border: 1px dashed #444c56;
}
/* ===== TABS ===== */
.stTabs [data-baseweb="tab-list"] {
background-color: #161b22;
border-bottom: 1px solid #30363d;
}
.stTabs [data-baseweb="tab"] {
color: #8b949e;
}
.stTabs [aria-selected="true"] {
color: #58a6ff;
border-bottom-color: #58a6ff;
}
/* ===== SPINNER ===== */
.stSpinner > div {
border-top-color: #58a6ff;
}
/* ===== CUSTOM CLASSES ===== */
/* Source badges */
.source-badge-high {
background-color: #1a472a;
color: #a5f3a5;
padding: 4px 8px;
border-radius: 4px;
font-size: 0.85rem;
font-weight: 500;
}
.source-badge-medium {
background-color: #4d3800;
color: #ffdf6b;
padding: 4px 8px;
border-radius: 4px;
font-size: 0.85rem;
font-weight: 500;
}
.source-badge-low {
background-color: #4c1f1f;
color: #ffa198;
padding: 4px 8px;
border-radius: 4px;
font-size: 0.85rem;
font-weight: 500;
}
/* Make sure all text is readable */
* {
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
/* Improve text contrast */
p, span, div, label {
color: #e6edf3;
}
/* Headers in content */
h1 { color: #ffffff; font-weight: 600; }
h2 { color: #ffffff; font-weight: 600; }
h3 { color: #ffffff; font-weight: 600; }
h4 { color: #e6edf3; font-weight: 600; }
h5 { color: #e6edf3; font-weight: 500; }
h6 { color: #c9d1d9; font-weight: 500; }
</style>
""", unsafe_allow_html=True)
def initialize_session_state():
"""Initialize session state variables"""
if 'research_complete' not in st.session_state:
st.session_state.research_complete = False
if 'report' not in st.session_state:
st.session_state.report = None
if 'query_analysis' not in st.session_state:
st.session_state.query_analysis = None
def check_api_key():
"""Check if OpenAI API key is configured"""
if not OPENAI_API_KEY:
st.error("⚠️ OpenAI API key not found! Please set OPENAI_API_KEY in your .env file")
st.info("Create a .env file with: OPENAI_API_KEY=your_key_here")
st.stop()
def display_header():
"""Display application header"""
st.markdown('<p class="main-header">📊 Modular Equity Research System</p>', unsafe_allow_html=True)
st.markdown('<p class="sub-header">AI modules working in coordination to analyze financial markets</p>', unsafe_allow_html=True)
st.markdown("---")
def display_module_info():
"""Display information about the modules"""
with st.expander("🤖 About the Modular System", expanded=False):
col1, col2, col3, col4 = st.columns(4)
with col1:
st.markdown("### 🔍 Query Analyzer")
st.write("Analyzes research queries and extracts key information")
with col2:
st.markdown("### 📰 Research Module")
st.write("Discovers and loads relevant sources autonomously")
with col3:
st.markdown("### ✅ Validation Module")
st.write("Validates sources and calculates confidence scores")
with col4:
st.markdown("### 📝 Synthesis Module")
st.write("Generates comprehensive research reports")
def run_research(query: str, mode: str, user_urls: list = None):
"""Execute the multi modular research pipeline"""
logger.clear_logs()
try:
# Initialize modules
query_analyzer = QueryAnalyzer()
research_module = ResearchModule()
validation_module = ValidationModule()
synthesis_module = SynthesisModule()
# Progress tracking
progress_bar = st.progress(0)
status_text = st.empty()
# Step 1: Query Analysis (20%)
status_text.text("🔍 Analyzing your query...")
progress_bar.progress(20)
query_analysis = query_analyzer.analyze(query)
st.session_state.query_analysis = query_analysis
# Display analysis
with st.expander("📋 Query Analysis Results", expanded=True):
col1, col2 = st.columns(2)
with col1:
st.write("**Company:**", query_analysis.get("company_name", "N/A"))
st.write("**Ticker:**", query_analysis.get("ticker", "N/A"))
st.write("**Research Type:**", query_analysis.get("research_intent", "N/A"))
with col2:
st.write("**Key Topics:**")
for topic in query_analysis.get("key_topics", []):
st.write(f"- {topic}")
# Step 2: Research (40%)
status_text.text("📰 Discovering and loading sources...")
progress_bar.progress(40)
if mode == "autonomous" or not user_urls:
documents = research_module.search_and_load(query_analysis)
else:
documents = research_module.load_from_user_urls(user_urls)
if not documents:
st.error("❌ No sources could be loaded. Please try different URLs or query.")
return
st.success(f"✅ Loaded {len(documents)} sources successfully")
# Step 3: Validation (60%)
status_text.text("✅ Validating sources and checking credibility...")
progress_bar.progress(60)
validation_report = validation_module.validate_documents(documents)
# Display validation
with st.expander("🔒 Validation Report", expanded=True):
col1, col2, col3 = st.columns(3)
with col1:
confidence = validation_report.get("overall_confidence", 0)
st.metric("Overall Confidence", f"{confidence:.1f}%")
with col2:
st.metric("Total Sources", validation_report.get("trusted_sources", 0))
with col3:
st.metric("Trusted Sources", validation_report.get("trusted_sources", 0))
for note in validation_report.get("validation_notes", []):
st.info(note)
# Step 4: Synthesis (80%)
status_text.text("📝 Generating comprehensive report...")
progress_bar.progress(80)
report = synthesis_module.generate_report(
query_analysis,
documents,
validation_report,
query
)
st.session_state.report = report
# Complete (100%)
progress_bar.progress(100)
status_text.text("✅ Research complete!")
st.session_state.research_complete = True
# Display report
# display_report(report)
except Exception as e:
st.error(f"❌ An error occurred: {str(e)}")
st.exception(e)
def display_report(report: dict):
"""Display the generated research report"""
st.markdown("---")
st.markdown("## 📊 Research Report")
# Header with metadata
col1, col2, col3, col4 = st.columns(4)
with col1:
st.markdown('<div class="metric-card">', unsafe_allow_html=True)
st.metric("Company", report.get("company", "N/A"))
st.markdown('</div>', unsafe_allow_html=True)
with col2:
st.markdown('<div class="metric-card">', unsafe_allow_html=True)
st.metric("Ticker", report.get("ticker", "N/A"))
st.markdown('</div>', unsafe_allow_html=True)
with col3:
st.markdown('<div class="metric-card">', unsafe_allow_html=True)
confidence = report.get("confidence_score", 0)
st.metric("Confidence", f"{confidence:.1f}%")
st.markdown('</div>', unsafe_allow_html=True)
with col4:
st.markdown('<div class="metric-card">', unsafe_allow_html=True)
st.metric("Sources", report["metadata"].get("total_sources", 0))
st.markdown('</div>', unsafe_allow_html=True)
# Confidence indicator
confidence = report.get("confidence_score", 0)
if confidence >= 75:
css_class = "confidence-high"
indicator = "🟢 High Confidence"
elif confidence >= 50:
css_class = "confidence-medium"
indicator = "🟡 Medium Confidence"
else:
css_class = "confidence-low"
indicator = "🔴 Low Confidence"
st.markdown(f'<div class="module-status {css_class}">{indicator}</div>', unsafe_allow_html=True)
# Report content
st.markdown("### 📄 Report Content")
st.markdown(report.get("content", "No content available"))
# # Sources
# st.markdown("### 🔗 Sources")
# sources = report.get("sources", [])
# if sources:
# for idx, source in enumerate(sources, 1):
# st.markdown(f"{idx}. [{source.get('title', 'Source')}]({source.get('url', '#')})")
# else:
# st.info("No sources available")
# Sources
st.markdown("### 🔗 Sources")
sources = report.get("sources", [])
if sources and len(sources) > 0:
st.markdown(f"**Analyzed {len(sources)} source(s):**")
st.markdown("") # Add spacing
for source in sources:
if isinstance(source, dict):
# Get source information
url = source.get('url', '#')
title = source.get('title', url)
index = source.get('index', '')
credibility = source.get('credibility_score')
is_trusted = source.get('is_trusted', False)
# Create source display with credibility badge
if credibility and credibility != "N/A":
# Color code based on credibility
if credibility >= 80:
badge_color = "🟢"
badge_text = f"High Quality ({credibility}/100)"
elif credibility >= 60:
badge_color = "🟡"
badge_text = f"Medium Quality ({credibility}/100)"
else:
badge_color = "🔴"
badge_text = f"Low Quality ({credibility}/100)"
# Display with badge
col1, col2 = st.columns([3, 1])
with col1:
st.markdown(f"**{index}.** [{title}]({url})")
with col2:
st.caption(f"{badge_color} {badge_text}")
if is_trusted:
st.caption("✅ Trusted Source")
else:
# Display without credibility score
st.markdown(f"**{index}.** [{title}]({url})")
st.markdown("") # Add spacing between sources
elif isinstance(source, str):
# Source is just a URL string
st.markdown(f"- [{source}]({source})")
else:
# Try to get sources from metadata as fallback
metadata_sources = report.get("metadata", {}).get("sources_analyzed", [])
if metadata_sources:
st.markdown(f"**Sources analyzed ({len(metadata_sources)}):**")
for idx, source_url in enumerate(metadata_sources, 1):
# Extract domain for display
try:
from urllib.parse import urlparse
parsed = urlparse(source_url)
domain = parsed.netloc or "Unknown"
st.markdown(f"{idx}. [{domain}]({source_url})")
except:
st.markdown(f"{idx}. [{source_url}]({source_url})")
else:
st.info("ℹ️ No source information available in this report")
# Validation notes
if report.get("validation_notes"):
st.markdown("### ℹ️ Validation Notes")
for note in report["validation_notes"]:
st.info(note)
# Metadata
with st.expander("📈 Analysis Metadata", expanded=True):
metadata = report.get("metadata", {})
# Display in readable format
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Total Sources", metadata.get("total_sources", 0))
st.metric("Trusted Sources", metadata.get("trusted_sources", 0))
with col2:
st.metric("Analysis Depth", metadata.get("analysis_depth", "N/A"))
st.caption("Based on content volume")
with col3:
if "sources_analyzed" in metadata:
st.caption("**Sources Analyzed:**")
for idx, url in enumerate(metadata["sources_analyzed"][:5], 1):
# Shorten URL for display
display_url = url[:50] + "..." if len(url) > 50 else url
st.caption(f"{idx}. {display_url}")
def main():
"""Main application function"""
initialize_session_state()
check_api_key()
display_header()
display_module_info()
# Sidebar
with st.sidebar:
st.markdown("## ⚙️ Research Configuration")
# Mode selection
mode = st.radio(
"Research Mode",
["autonomous", "manual"],
format_func=lambda x: "🤖 Autonomous (AI finds sources)" if x == "autonomous" else "📝 Manual (Provide URLs)",
help="Autonomous mode: AI modules discover sources automatically\nManual mode: Provide your own URLs"
)
user_urls = []
if mode == "manual":
st.markdown("### 📎 Enter URLs")
st.caption("Provide up to 5 URLs for analysis")
for i in range(5):
url = st.text_input(f"URL {i+1}", key=f"url_{i}", placeholder="https://example.com/article")
if url:
user_urls.append(url)
st.markdown("---")
st.markdown("### 📊 Activity Log")
log_container = st.container()
with log_container:
logger.display_logs()
if st.button("🗑️ Clear Log"):
logger.clear_logs()
st.rerun()
# Main content area
st.markdown("## 🔍 Research Query")
# Query input
query_input = st.text_area(
"Enter your equity research question:",
placeholder="Example: Analyze Tesla's recent earnings performance and delivery numbers for Q4 2024",
height=100,
help="Be specific! Include company name and what you want to research."
)
col1, col2, col3 = st.columns([2, 1, 1])
with col1:
start_research = st.button("🚀 Start Research", type="primary", use_container_width=True)
with col2:
if st.session_state.research_complete:
if st.button("🔄 New Research", use_container_width=True):
st.session_state.research_complete = False
st.session_state.report = None
logger.clear_logs()
st.rerun()
# Execute research
if start_research and query_input:
run_research(query_input, mode, user_urls if mode == "manual" else None)
elif start_research:
st.warning("⚠️ Please enter a research query")
# Display existing report if available
if st.session_state.research_complete and st.session_state.report:
display_report(st.session_state.report)
if __name__ == "__main__":
main()