app.py•3.61 kB
import streamlit as st
import asyncio
import os
import sys
from typing import Optional
# Add parent directory to path for imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from client.client_runner import Client # Adjust the import path as needed
st.title('SEC Filing Chatbot')
st.markdown('Ask questions about SEC filings for companies like AAPL, AMZN, FL, KO, META, MSFT, NVDA, TSLA')
# Initialize session state for chat history
if 'messages' not in st.session_state:
st.session_state.messages = []
# Cache the client initialization
@st.cache_resource
def get_client():
'''Initialize and cache the client'''
return Client()
# Async function to handle queries
async def handle_query(client: Client, query: str) -> str:
'''Handle the async query to the client'''
try:
result = await client.query(query)
return result.output if hasattr(result, 'output') else str(result)
except Exception as e:
return f'Error processing query: {str(e)}'
# Display chat history
for message in st.session_state.messages:
with st.chat_message(message['role']):
st.markdown(message['content'])
# Chat input
if prompt := st.chat_input('Ask a question about SEC filings...'):
# Add user message to chat history
st.session_state.messages.append({'role': 'user', 'content': prompt})
# Display user message
with st.chat_message('user'):
st.markdown(prompt)
# Display assistant response
with st.chat_message('assistant'):
message_placeholder = st.empty()
try:
# Get cached client
client = get_client()
# Show loading state
with st.spinner('Analyzing SEC filings...'):
# Create new event loop for this query
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
answer = loop.run_until_complete(handle_query(client, prompt))
finally:
loop.close()
message_placeholder.markdown(answer)
st.session_state.messages.append({'role': 'assistant', 'content': answer})
except Exception as e:
error_msg = f'❌ Error: {str(e)}'
message_placeholder.markdown(error_msg)
st.session_state.messages.append({'role': 'assistant', 'content': error_msg})
# Sidebar with information
with st.sidebar:
st.header('About')
st.markdown('''
This chatbot uses AI to answer questions about SEC filings for major companies.
### Supported Companies:
- **AAPL** (Apple)
- **AMZN** (Amazon)
- **FL** (Foot Locker)
- **KO** (Coca-Cola)
- **META** (Meta)
- **MSFT** (Microsoft)
- **NVDA** (NVIDIA)
- **TSLA** (Tesla)
### Example Questions:
- What is Apple's revenue for 2023?
- What are the main risks for Tesla?
- Compare Microsoft and Meta's profit margins
- What is NVIDIA's business strategy?
- Show me Coca-Cola's debt levels
''')
# Add clear chat button
st.divider()
if st.button('🗑️ Clear Chat History'):
st.session_state.messages = []
st.rerun()
# Configuration info
st.divider()
st.subheader('Configuration')
st.info('Using GPT-5/GPT-4.1 models via pydantic_ai')
# Check environment
if os.environ.get('OPENAI_API_KEY'):
st.success('✅ OpenAI API key found')
else:
st.error('❌ OpenAI API key not found')