provider_select = st.radio("Select AI Provider", ["Azure GPT", "Google Gemini"])
# Initialize client and agents for dashboard
initialize_clients_and_agents(provider_select, mode="dashboard")
if "session" not in st.session_state:
st.session_state.session = {
'uploaded_files': {},
'processed_files_overall': {},
'processed_files_tariff': {},
'processed_files_sales': {},
'processed_files_supplychain': {},
'active_file': None,
'active_topic': None,
'topic_details_cache': {},
'chat_history': [],
'consolidated_summary_overall': None,
'consolidated_summary_tariff': None,
'consolidated_summary_sales': None,
'consolidated_summary_supplychain': None,
'last_analysis_mode': None,
'custom_prompt_enabled': {},
'custom_prompt_text': {}
}
sess = st.session_state.session
# --- File Upload ---
st.subheader("1. Upload Documents")
uploaded_files = st.file_uploader(
"Upload PDF documents",
type=["pdf"],
accept_multiple_files=True,
key="file_uploader",
)
if uploaded_files:
new_files_added = False
for file in uploaded_files:
if file.name not in sess['uploaded_files']:
sess['uploaded_files'][file.name] = file.getvalue()
new_files_added = True
if new_files_added:
with st.spinner("🔍 Analyzing new documents with Overall Agent..."):
# Always run initial overall analysis for new files
analyze_documents(
mode="overall",
agent=st.session_state.agent_overall,
processed_files=sess['processed_files_overall'],
sess=sess
)
# --- Custom Prompts & Analysis ---
st.subheader("2. Custom Prompts & Analysis")
# Initialize custom prompts in session state
if 'custom_prompts' not in sess:
sess['custom_prompts'] = {
'overall': {
'summary': OverallAgent.prompt_summary,
'analyze': OverallAgent.prompt_analyze,
'detail': OverallAgent.prompt_detail
},
'tariff': {
'summary': TariffTradeAgent.prompt_summary,
'analyze': TariffTradeAgent.prompt_analyze,
'detail': TariffTradeAgent.prompt_detail
},
'sales': {
'summary': SalesAgent.prompt_summary,
'analyze': SalesAgent.prompt_analyze,
'detail': SalesAgent.prompt_detail
},
'supplychain': {
'summary': SupplyChainAgent.prompt_summary,
'analyze': SupplyChainAgent.prompt_analyze,
'detail': SupplyChainAgent.prompt_detail
}
}
# Create tabs for each agent's custom prompts
tab_overall, tab_tariff, tab_sales, tab_supply = st.tabs([
"📑 Overall", "🔍 Tariff", "📈 Sales", "🚚 Supply Chain"
])
# Store which mode we're generating for
generate_mode = None
with tab_overall:
st.subheader("Overall Prompts")
use_custom = st.checkbox("✏️ Use Custom Prompts", key="custom_overall")
if use_custom:
sess['custom_prompts']['overall']['summary'] = st.text_area(
"Summary Prompt:",
value=sess['custom_prompts']['overall']['summary'],
height=150,
key="overall_prompt_summary"
)
sess['custom_prompts']['overall']['analyze'] = st.text_area(
"Analysis Prompt:",
value=sess['custom_prompts']['overall']['analyze'],
height=150,
key="overall_prompt_analyze"
)
sess['custom_prompts']['overall']['detail'] = st.text_area(
"Detail Prompt:",
value=sess['custom_prompts']['overall']['detail'],
height=150,
key="overall_prompt_detail"
)
if st.button("🔄 Generate with Custom Prompts", key="generate_overall"):
generate_mode = "overall"
with tab_tariff:
st.subheader("Tariff Prompts")
use_custom = st.checkbox("✏️ Use Custom Prompts", key="custom_tariff")
if use_custom:
sess['custom_prompts']['tariff']['summary'] = st.text_area(
"Summary Prompt:",
value=sess['custom_prompts']['tariff']['summary'],
height=150,
key="tariff_prompt_summary"
)
sess['custom_prompts']['tariff']['analyze'] = st.text_area(
"Analysis Prompt:",
value=sess['custom_prompts']['tariff']['analyze'],
height=150,
key="tariff_prompt_analyze"
)
sess['custom_prompts']['tariff']['detail'] = st.text_area(
"Detail Prompt:",
value=sess['custom_prompts']['tariff']['detail'],
height=150,
key="tariff_prompt_detail"
)
if st.button("🔄 Generate with Custom Prompts", key="generate_tariff"):
generate_mode = "tariff"
with tab_sales:
st.subheader("Sales Prompts")
use_custom = st.checkbox("✏️ Use Custom Prompts", key="custom_sales")
if use_custom:
sess['custom_prompts']['sales']['summary'] = st.text_area(
"Summary Prompt:",
value=sess['custom_prompts']['sales']['summary'],
height=150,
key="sales_prompt_summary"
)
sess['custom_prompts']['sales']['analyze'] = st.text_area(
"Analysis Prompt:",
value=sess['custom_prompts']['sales']['analyze'],
height=150,
key="sales_prompt_analyze"
)
sess['custom_prompts']['sales']['detail'] = st.text_area(
"Detail Prompt:",
value=sess['custom_prompts']['sales']['detail'],
height=150,
key="sales_prompt_detail"
)
if st.button("🔄 Generate with Custom Prompts", key="generate_sales"):
generate_mode = "sales"
with tab_supply:
st.subheader("Supply Chain Prompts")
use_custom = st.checkbox("✏️ Use Custom Prompts", key="custom_supply")
if use_custom:
sess['custom_prompts']['supplychain']['summary'] = st.text_area(
"Summary Prompt:",
value=sess['custom_prompts']['supplychain']['summary'],
height=150,
key="supply_prompt_summary"
)
sess['custom_prompts']['supplychain']['analyze'] = st.text_area(
"Analysis Prompt:",
value=sess['custom_prompts']['supplychain']['analyze'],
height=150,
key="supply_prompt_analyze"
)
sess['custom_prompts']['supplychain']['detail'] = st.text_area(
"Detail Prompt:",
value=sess['custom_prompts']['supplychain']['detail'],
height=150,
key="supply_prompt_detail"
)
if st.button("🔄 Generate with Custom Prompts", key="generate_supplychain"):
generate_mode = "supplychain"
# --- Analysis Buttons ---
st.subheader("Quick Analysis")
col0, col1, col2, col3 = st.columns(4)
analyze_overall = col0.button("📑 Overall Summary")
analyze_tariff = col1.button("🔍 Analyze Tariffs")
analyze_sales = col2.button("📈 Analyze Sales")
analyze_supplychain = col3.button("🚚 Supply Chain")
# Handle generation requests
if generate_mode or analyze_overall or analyze_tariff or analyze_sales or analyze_supplychain:
if not sess['uploaded_files']:
st.error("Please upload documents first.")
st.stop()
mode = generate_mode or (
"overall" if analyze_overall else
"tariff" if analyze_tariff else
"sales" if analyze_sales else
"supplychain" if analyze_supplychain else None
)
agent = None
processed_files = None
if mode == "overall":
agent = st.session_state.agent_overall
processed_files = sess['processed_files_overall']
elif mode == "tariff":
agent = st.session_state.agent_tariff
processed_files = sess['processed_files_tariff']
elif mode == "sales":
agent = st.session_state.agent_sales
processed_files = sess['processed_files_sales']
elif mode == "supplychain":
agent = st.session_state.agent_supplychain
processed_files = sess['processed_files_supplychain']
# Update agent with custom prompts if used
if generate_mode:
agent.prompt_summary = sess['custom_prompts'][mode]['summary']
agent.prompt_analyze = sess['custom_prompts'][mode]['analyze']
agent.prompt_detail = sess['custom_prompts'][mode]['detail']
st.toast(f"Using custom prompts for {mode} analysis")
else:
# Reset to default prompts
if mode == "overall":
agent.prompt_summary = OverallAgent.prompt_summary
agent.prompt_analyze = OverallAgent.prompt_analyze
agent.prompt_detail = OverallAgent.prompt_detail
elif mode == "tariff":
agent.prompt_summary = TariffTradeAgent.prompt_summary
agent.prompt_analyze = TariffTradeAgent.prompt_analyze
agent.prompt_detail = TariffTradeAgent.prompt_detail
elif mode == "sales":
agent.prompt_summary = SalesAgent.prompt_summary
agent.prompt_analyze = SalesAgent.prompt_analyze
agent.prompt_detail = SalesAgent.prompt_detail
elif mode == "supplychain":
agent.prompt_summary = SupplyChainAgent.prompt_summary
agent.prompt_analyze = SupplyChainAgent.prompt_analyze
agent.prompt_detail = SupplyChainAgent.prompt_detail
with st.spinner(f"🔍 Analyzing {mode.replace('supplychain', 'supply chain')} documents..."):
analyze_documents(
mode,
agent,
processed_files,
sess
)
# --- Main Display ---
last_mode = sess['last_analysis_mode']
if last_mode is None:
st.info("Select an analysis type to begin.")
return
# Consolidated data setup
mode_config = {
'overall': {
'files': sess['processed_files_overall'],
'summary': sess['consolidated_summary_overall'],
'sidebar_header': "Overall Topics",
'main_header': "🌟 Executive Summary",
'agent': st.session_state.agent_overall
},
'tariff': {
'files': sess['processed_files_tariff'],
'summary': sess['consolidated_summary_tariff'],
'sidebar_header': "Tariff Topics",
'main_header': "🌟 Tariff Analysis",
'agent': st.session_state.agent_tariff
},
'sales': {
'files': sess['processed_files_sales'],
'summary': sess['consolidated_summary_sales'],
'sidebar_header': "Sales Topics",
'main_header': "🌟 Sales Analysis",
'agent': st.session_state.agent_sales
},
'supplychain': {
'files': sess['processed_files_supplychain'],
'summary': sess['consolidated_summary_supplychain'],
'sidebar_header': "Supply Chain Topics",
'main_header': "🌟 Supply Chain Analysis",
'agent': st.session_state.agent_supplychain
}
}
current_mode = mode_config[last_mode]
# --- Sidebar Topics ---
st.sidebar.header(current_mode['sidebar_header'])
if last_mode == "overall":
topics_by_file = {}
for filename, filedata in current_mode['files'].items():
for topic in filedata["topics"]:
topics_by_file.setdefault(topic['name'], []).append({
'file': filename,
'score': topic['score']
})
sorted_topics = sorted(topics_by_file.items(),
key=lambda x: max(x[1], key=lambda y: y['score'])['score'],
reverse=True)
before_view_more, after_view_more = sorted_topics[:3], sorted_topics[3:]
for topic_name, occurrences in before_view_more:
total_score = sum(occ['score'] for occ in occurrences)
file_count = len(occurrences)
btn_label = f"{topic_name} (📈 {total_score:.1f} | 📑 {file_count})"
if st.sidebar.button(
btn_label,
key=f"overall_topic_{topic_name}",
use_container_width=True,
):
sess['active_topic'] = topic_name
sess['active_file'] = None
if after_view_more and st.sidebar.button('View More', key='1234', use_container_width=True):
for topic_name, occurrences in after_view_more:
total_score = sum(occ['score'] for occ in occurrences)
file_count = len(occurrences)
btn_label = f"{topic_name} (📈 {total_score:.1f} | 📑 {file_count})"
if st.sidebar.button(
btn_label,
key=f"overall_topic_more_{topic_name}",
use_container_width=True,
):
sess['active_topic'] = topic_name
sess['active_file'] = None
else: # For Tariff, Sales, Supply Chain modes
for filename, filedata in current_mode['files'].items():
expanded = (filename == sess['active_file'])
with st.sidebar.expander(f"📄 {filename}", expanded=expanded):
topics = sorted(filedata["topics"], key=lambda t: t["score"], reverse=True)
for topic in topics[:3]:
is_selected = (
sess['active_file'] == filename and sess['active_topic'] == topic['name']
)
style = "primary" if is_selected else "secondary"
if st.button(
f"• {topic['name']} (Score: {topic['score']})",
key=f"{last_mode}_topic_{filename}_{topic['name']}",
type=style,
use_container_width=True,
):
sess['active_file'] = filename
sess['active_topic'] = topic['name']
if len(topics) > 3 and st.sidebar.button(f'View More for {filename}', key=f'{filename}_view_more', use_container_width=True):
for topic in topics[3:]:
is_selected = (
sess['active_file'] == filename and sess['active_topic'] == topic['name']
)
style = "primary" if is_selected else "secondary"
if st.button(
f"• {topic['name']} (Score: {topic['score']})",
key=f"{last_mode}_topic_more_{filename}_{topic['name']}",
type=style,
use_container_width=True,
):
sess['active_file'] = filename
sess['active_topic'] = topic['name']
# --- Topic Analysis ---
st.subheader("Detailed Analysis")
if last_mode == "overall" and sess['active_topic']:
mentions = []
for filename, filedata in current_mode['files'].items():
for topic in filedata["topics"]:
if topic['name'] == sess['active_topic']:
mentions.append({
'file': filename,
'summary': topic.get('summary', ''),
'score': topic['score'],
'context': filedata['context']
})
with st.expander("📊 Topic Prevalence"):
cols = st.columns(3)
cols[0].metric("Total Documents", len(mentions))
avg_relevance = (sum(m['score'] for m in mentions) / len(mentions)) if mentions else 0
cols[1].metric("Average Relevance", f"{avg_relevance:.1f}")
cols[2].metric("First Mentioned", mentions[0]['file'] if mentions else "N/A")
if mentions:
combined_context = "\n\n".join(f"=== {m['file']} ===\n{m['context']}" for m in mentions)
key_cache = (last_mode, sess['active_topic'])
if key_cache in sess['topic_details_cache']:
st.markdown(sess['topic_details_cache'][key_cache])
else:
placeholder = st.empty()
collected_text = ""
try:
for chunk in current_mode['agent'].generate_topic_detail(combined_context, sess['active_topic']):
collected_text = chunk
placeholder.markdown(collected_text + (" ▌" if "▌" in collected_text else ""))
sess['topic_details_cache'][key_cache] = collected_text
except Exception as e:
st.error(f"Analysis error: {str(e)}")
elif sess['active_file'] and sess['active_topic']:
key_cache = (last_mode, sess['active_file'], sess['active_topic'])
if key_cache in sess['topic_details_cache']:
st.markdown(sess['topic_details_cache'][key_cache])
else:
placeholder = st.empty()
collected_text = ""
try:
for chunk in current_mode['agent'].generate_topic_detail(
current_mode['files'][sess['active_file']]['context'],
sess['active_topic'],
):
collected_text = chunk
placeholder.markdown(collected_text + (" ▌" if "▌" in collected_text else ""))
sess['topic_details_cache'][key_cache] = collected_text
except Exception as e:
st.error(f"Analysis error: {str(e)}")
else:
st.info("Select a topic from the sidebar")
st.markdown("---")
# --- Consolidated Summary ---
st.header(current_mode['main_header'])
if current_mode['summary']:
st.markdown(current_mode['summary'])
else:
st.info("Summary not available yet")
st.divider()
# --- Chat Interface ---
st.subheader("💬 Document Chat")
# File selection
available_files = list(current_mode['files'].keys())
selected_files = st.multiselect(
"Select documents for context:",
available_files,
key=f"chat_selector_{last_mode}"
)
# Chat history
for msg in sess['chat_history']:
with st.chat_message(msg["role"]):
st.markdown(msg["content"])
# Question handling
question = st.chat_input("Ask about the documents...")
if question:
if not selected_files:
st.error("Please select documents first")
st.stop()
sess['chat_history'].append({
"role": "user",
"content": question,
"timestamp": datetime.utcnow().isoformat(),
})
try:
context = "\n\n".join([
f"=== {fname} ===\n{current_mode['files'][fname]['context']}"
for fname in selected_files
])
with st.chat_message("assistant"):
response_placeholder = st.empty()
full_response = ""
q_type = None
for chunk, q_type in st.session_state.chat_agent.generate_answer(question, context):
full_response = chunk
response_placeholder.markdown(full_response + " ▌")
response_placeholder.markdown(full_response)
sess['chat_history'].append({
"role": "assistant",
"content": full_response,
"timestamp": datetime.utcnow().isoformat(),
"analysis_type": q_type
})
except Exception as e:
st.error(f"Chat error: {str(e)}")
sess['chat_history'].append({
"role": "assistant",
"content": f"Error: {str(e)}",
"timestamp": datetime.utcnow().isoformat(),
})
# Reset button
if st.button("🔄 Reset Session", type="secondary"):
preserved_keys = [
"client", "agent_overall", "agent_tariff",
"agent_sales", "agent_supplychain", "chat_agent", "provider"
]
preserved = {k: st.session_state[k] for k in preserved_keys if k in st.session_state}
st.session_state.clear()
st.session_state.update(preserved)
st.session_state.session = {
'uploaded_files': {},
'processed_files_overall': {},
'processed_files_tariff': {},
'processed_files_sales': {},
'processed_files_supplychain': {},
'active_file': None,
'active_topic': None,
'topic_details_cache': {},
'chat_history': [],
'consolidated_summary_overall': None,
'consolidated_summary_tariff': None,
'consolidated_summary_sales': None,
'consolidated_summary_supplychain': None,
'last_analysis_mode': None,
'custom_prompt_enabled': {},
'custom_prompt_text': {}
}
st.experimental_rerun()
if 'qa_page' not in st.session_state:
st.session_state.qa_page = {
'uploaded_files': {},
'processed_files': {},
'active_file': None,
'active_topic': None,
'chat_history': [],
'selected_files': []
}
qa_state = st.session_state.qa_page
provider_select = st.sidebar.radio("Select AI Provider", ["Azure GPT", "Google Gemini"])
# Initialize client and QA agents
initialize_clients_and_agents(provider_select, mode="qa")
# --- File Upload ---
st.subheader("Upload Documents")
uploaded_files = st.file_uploader(
"Upload PDF documents for Q&A extraction",
type=["pdf"],
accept_multiple_files=True,
key="qa_file_uploader"
)
if uploaded_files:
new_files_added = False
for file in uploaded_files:
if file.name not in qa_state['uploaded_files']:
qa_state['uploaded_files'][file.name] = file.getvalue()
new_files_added = True
if new_files_added:
with st.spinner("🔍 Extracting Q&A from new documents..."):
for filename in qa_state['uploaded_files']:
if filename not in qa_state['processed_files']:
try:
file_bytes = qa_state['uploaded_files'][filename]
file_obj = BytesIO(file_bytes)
file_obj.name = filename
text = extract_text(file_obj)
if text:
# Use the QA Agent to generate structured Q&A content
qa_data = st.session_state.agent_qa.analyze_question(text)
# Store the structured data
qa_state['processed_files'][filename] = {
'text': text,
'qa_data': qa_data
}
st.toast(f"✅ Processed {filename}")
# Automatically select the first topic
if qa_data.get('topics'):
qa_state['active_topic'] = qa_data['topics'][0]
else:
st.error(f"❌ Failed to extract text for {filename}")
except Exception as e:
st.error(f"Error processing {filename}: {str(e)}")
# Auto-select the first file if none is selected
if not qa_state['active_file'] and qa_state['processed_files']:
qa_state['active_file'] = next(iter(qa_state['processed_files'].keys()))
# --- Document Selection ---
st.sidebar.header("📄 Processed Documents")
for filename in qa_state['processed_files']:
if st.sidebar.button(
f"📄 {filename}",
key=f"qa_file_{filename}",
use_container_width=True,
):
qa_state['active_file'] = filename
# Automatically select the first topic for this document
file_data = qa_state['processed_files'][filename]
if file_data['qa_data'].get('topics'):
qa_state['active_topic'] = file_data['qa_data']['topics'][0]
# --- Topic Sidebar ---
if qa_state['active_file']:
file_data = qa_state['processed_files'][qa_state['active_file']]
qa_data = file_data['qa_data']
st.sidebar.header("📌 Topics")
if 'topics' in qa_data:
topics = qa_data['topics']
# Display each topic in the sidebar
for topic in topics:
btn_label = f"{topic['topic']} ({topic['mention_count']} mentions)"
if st.sidebar.button(
btn_label,
key=f"topic_{topic['topic']}_{qa_state['active_file']}",
use_container_width=True,
):
qa_state['active_topic'] = topic
else:
st.sidebar.info("No topics found in this document")
# --- Q&A Display ---
st.subheader("📝 Extracted Q&A Content")
if qa_state['active_file']:
file_data = qa_state['processed_files'][qa_state['active_file']]
qa_data = file_data['qa_data']
st.markdown(f"**Document:** {qa_state['active_file']}")
# Display key takeaways
if 'key_takeaways' in qa_data:
st.subheader("Key Takeaways")
for takeaway in qa_data['key_takeaways']:
st.markdown(f"- {takeaway}")
# Display selected topic
if qa_state['active_topic']:
topic = qa_state['active_topic']
st.divider()
st.subheader(topic['topic'])
st.markdown(f"**Mentioned:** {topic['mention_count']} times")
st.divider()
# Display questions for this topic
for idx, question in enumerate(topic['questions']):
st.markdown(f"**Firm:** {question['firm']}")
st.markdown(f"**Question {idx+1}:** {question['question']}")
st.write("---")
elif 'topics' in qa_data and qa_data['topics']:
# If no topic selected but topics exist, show the first one
qa_state['active_topic'] = qa_data['topics'][0]
st.experimental_rerun()
else:
st.info("No Q&A content found for this document")
else:
st.info("Upload documents to see extracted Q&A content")
st.divider()
# --- Chat Interface ---
st.subheader("💬 Chat with Documents")
available_files = list(qa_state['processed_files'].keys())
selected_files = st.multiselect(
"Select documents to include in chat context:",
available_files,
default=available_files[:min(3, len(available_files))],
key="qa_chat_selector"
)
qa_state['selected_files'] = selected_files
for msg in qa_state['chat_history']:
with st.chat_message(msg["role"]):
st.markdown(msg["content"])
question = st.chat_input("Ask about the documents...")
if question:
if not selected_files:
st.error("Please select at least one document for context")
st.stop()
qa_state['chat_history'].append({
"role": "user",
"content": question,
"timestamp": datetime.utcnow().isoformat(),
})
try:
context = "\n\n".join([
f"=== {filename} ===\n{qa_state['processed_files'][filename]['text']}"
for filename in selected_files
])
with st.chat_message("assistant"):
response_placeholder = st.empty()
full_response = ""
for chunk, _ in st.session_state.chat_agent.generate_answer(question, context):
full_response = chunk
response_placeholder.markdown(full_response + " ▌")
response_placeholder.markdown(full_response)
qa_state['chat_history'].append({
"role": "assistant",
"content": full_response,
"timestamp": datetime.utcnow().isoformat(),
})
except Exception as e:
st.error(f"Chat error: {str(e)}")
qa_state['chat_history'].append({
"role": "assistant",
"content": f"Error: {str(e)}",
"timestamp": datetime.utcnow().isoformat(),
})
# Reset button for QA page
if st.button("🔄 Reset Q&A Session", key="reset_qa_page"):
qa_state.clear()
qa_state.update({
'uploaded_files': {},
'processed_files': {},
'active_file': None,
'active_topic': None,
'chat_history': [],
'selected_files': []
})
st.experimental_rerun()
The error:
Error processing AI Request - Post-1Q Questions.pdf: 'str' object has no attribute 'get'
TypeError: string indices must be integers
...
topics = qa_data['topics']
indicates that in this line:
topics = qa_data['topics']
qa_data is expected to be a dictionary (or similar mapping) but is actually a string. Python strings do not support indexing with string keys like ['topics'], so you get this error.
Where this happens:
In qa_page(), during file processing you call:
qa_data = st.session_state.agent_qa.analyze_question(text)
and then store it as:
qa_state['processed_files'][filename] = {
'text': text,
'qa_data': qa_data
}
Later when displaying the sidebar and QA content you access:
topics = qa_data['topics']
Expecting qa_data to be a dictionary with a 'topics' key.
Why might qa_data be a string?
analyze_question(text) may sometimes return a raw string (e.g., error message or raw JSON string) instead of a parsed dictionary.
- Or
analyze_question returns JSON in string form that you never parse.
- Or the function fails and returns an error string.
How to fix it
Step 1: Verify the type and content of qa_data.
Add a debug print or st.write just after assignment:
qa_data = st.session_state.agent_qa.analyze_question(text)
st.write(type(qa_data), qa_data)
Check what qa_data actually is.
Step 2: If qa_data is a JSON string, parse it
import json
qa_data_raw = st.session_state.agent_qa.analyze_question(text)
try:
qa_data = json.loads(qa_data_raw) if isinstance(qa_data_raw, str) else qa_data_raw
except Exception as e:
st.error(f"Failed to parse QA data JSON: {e}")
qa_data = {}
Then store qa_data, not the raw string.
Step 3: Defensive coding when accessing qa_data
Since qa_data might sometimes be missing keys, guard access:
topics = qa_data.get('topics') if isinstance(qa_data, dict) else None
if not topics:
st.info("No Q&A content found for this document")
Step 4: Modify your processing loop like this:
import json
# inside qa_page() where you process files
for filename in qa_state['uploaded_files']:
if filename not in qa_state['processed_files']:
try:
file_bytes = qa_state['uploaded_files'][filename]
file_obj = BytesIO(file_bytes)
file_obj.name = filename
text = extract_text(file_obj)
if text:
qa_data_raw = st.session_state.agent_qa.analyze_question(text)
# Parse JSON if returned string, else keep as is
if isinstance(qa_data_raw, str):
try:
qa_data = json.loads(qa_data_raw)
except Exception as e:
st.error(f"Error parsing QA data JSON for {filename}: {e}")
qa_data = {}
else:
qa_data = qa_data_raw
qa_state['processed_files'][filename] = {
'text': text,
'qa_data': qa_data
}
st.toast(f"✅ Processed {filename}")
# Automatically select the first topic safely
if qa_data.get('topics') and isinstance(qa_data.get('topics'), list) and len(qa_data['topics'])>0:
qa_state['active_topic'] = qa_data['topics'][0]
else:
st.error(f"❌ Failed to extract text for {filename}")
except Exception as e:
st.error(f"Error processing {filename}: {str(e)}")
Step 5: Anywhere you access qa_data['topics'] later, always check first:
if isinstance(qa_data, dict) and 'topics' in qa_data:
topics = qa_data['topics']
else:
topics = []
This will prevent your code from crashing if the data is malformed.
Summary
- The root cause is that
qa_data is a string, not a dict, so qa_data['topics'] fails.
- You must ensure that the
qa_data received from analyze_question is parsed into a Python dict (via json.loads) if it's a JSON string.
- Add type checks and exception handling to avoid crashing on unexpected data.
- Add debug logging (
st.write) to understand the actual qa_data content during development.
If you want, I can generate a minimal patch diff or updated function with these changes applied. Let me know!