DrishtiSharma commited on
Commit
537b48c
·
verified ·
1 Parent(s): af85116

Update latest/app.py

Browse files
Files changed (1) hide show
  1. latest/app.py +612 -0
latest/app.py CHANGED
@@ -0,0 +1,612 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from crewai import Agent, Task, Crew
3
+ import os
4
+ from langchain_groq import ChatGroq
5
+ from langchain_openai import ChatOpenAI
6
+ from fpdf import FPDF
7
+ import pandas as pd
8
+ import plotly.express as px
9
+ import tempfile
10
+ import time
11
+ import ast
12
+ import logging
13
+ import traceback
14
+
15
+ # Setup logging
16
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
17
+
18
+ # Title and Application Introduction
19
+ st.title("Patent Strategy and Innovation Consultant")
20
+ st.sidebar.write(
21
+ "This application provides actionable insights and comprehensive analysis for patent-related strategies."
22
+ )
23
+
24
+ # User Input Section
25
+ st.sidebar.header("User Inputs")
26
+ patent_area = st.text_input("Enter Patent Technology Area", value="Transparent Antennas for Windshields")
27
+ stakeholder = st.text_input("Enter Stakeholder", value="Patent Attorneys")
28
+
29
+ # Initialize LLM
30
+ llm = None
31
+
32
+ # Model Selection
33
+ model_choice = st.radio("Select LLM", ["GPT-4o", "llama-3.3-70b"], index=0, horizontal=True)
34
+
35
+
36
+ # API Key Validation and LLM Initialization
37
+ groq_api_key = os.getenv("GROQ_API_KEY")
38
+ openai_api_key = os.getenv("OPENAI_API_KEY")
39
+
40
+ if model_choice == "llama-3.3-70b":
41
+ if not groq_api_key:
42
+ st.error("Groq API key is missing. Please set the GROQ_API_KEY environment variable.")
43
+ llm = None
44
+ else:
45
+ llm = ChatGroq(groq_api_key=groq_api_key, model="groq/llama-3.3-70b-versatile")
46
+ elif model_choice == "GPT-4o":
47
+ if not openai_api_key:
48
+ st.error("OpenAI API key is missing. Please set the OPENAI_API_KEY environment variable.")
49
+ llm = None
50
+ else:
51
+ llm = ChatOpenAI(api_key=openai_api_key, model="gpt-4o")
52
+
53
+
54
+ # Advanced Options
55
+ st.sidebar.header("Advanced Options")
56
+ enable_advanced_analysis = st.sidebar.checkbox("Enable Advanced Analysis", value=True)
57
+ enable_custom_visualization = st.sidebar.checkbox("Enable Custom Visualizations", value=True)
58
+
59
+ # Agent Customization
60
+ st.sidebar.header("Agent Customization")
61
+ with st.sidebar.expander("Customize Agent Goals", expanded=False):
62
+ enable_customization = st.checkbox("Enable Custom Goals")
63
+ if enable_customization:
64
+ planner_goal = st.text_area(
65
+ "Planner Goal",
66
+ value=(
67
+ "Conduct in-depth, data-driven research on leading companies' recent patent filings, technological innovation, R&D investments,"
68
+ "strategic partnerships and market dynamics strictly within the {topic} sector."
69
+ "Identify key players, emerging technologies, competitor strategies, and market gaps with factually accurate and verifiable data. "
70
+ "Develop a Competitor Benchmark Matrix highlighting leaders in material/structural/software innovations vs. integration techniques."
71
+ "Summarize key market trends and shifts in competitive positioning with bullet-point insights."
72
+ "Strictly avoid hallucinated, fabricated, or speculative findings. "
73
+ "Develop a phased Patent Filing Roadmap targeting emerging technologies and high-growth regions tailored to the specific needs"
74
+ "and strategic goals of {stakeholder}. Conduct a Risk Assessment covering regulatory barriers, supply chain risks, and material"
75
+ "costs with mitigation strategies. Suggest Investment Opportunities by identifying startups or strategic partnerships in {topic}."
76
+ "Present findings using a Phased Patent Strategy Table (short-term, mid-term, long-term) and a Risk Matrix."
77
+ "Avoid unrelated or generic recommendations."
78
+ )
79
+ )
80
+ writer_goal = st.text_area(
81
+ "Writer Goal",
82
+ value=(
83
+ "Develop a high-impact, professionally structured insights report that integrates verified research data and strategic analysis into the following sections:\n\n"
84
+ "1. Executive Summary: Provide a concise overview of key insights and strategic recommendations strictly aligned with {stakeholder}'s strategic objectives.\n"
85
+ "2. Market Trends & Competitive Landscape: Detailed profiles, benchmarking, and market entry insights.\n"
86
+ "3. Emerging Technologies: Spotlight key technologies, TRL classification, adoption timelines.\n"
87
+ "4. Untapped Innovation Hotspots: Highlight regions with potential, innovation clusters, and readiness analysis.\n"
88
+ "5. Strategic Opportunities & Actionable Recommendations: Patent filing roadmap, risk mitigation, partnerships, and differentiation.\n"
89
+ "6. Future Growth Projections: Market forecasts, technology adoption, scenario analysis.\n"
90
+ "7. Industry Risk & Compliance Analysis: Risk matrix and mitigation strategies.\n"
91
+ "8. Summary & Appendix: Bullet-point insights and supporting data.\n\n"
92
+
93
+ "Ensure all insights, emerging technologies, and identified innovation gaps are fact-based, verifiable, and directly relevant to the {topic}."
94
+ "Present findings with clarity through well-structured sections, bullet points, and tables. Explicitly avoid hallucinated, fabricated, or speculative content throughout the report."
95
+
96
+ )
97
+ )
98
+ analyst_goal = st.text_area(
99
+ "Analyst Goal",
100
+ value=(
101
+ "Perform precise, data-driven statistical analysis of patent filings, growth trends, and innovation distribution strictly within the {topic} sector, "
102
+ "specifically customized to the strategic needs of {stakeholder}. "
103
+ "Identify top regions, leading assignees/companies, and emerging technologies that are explicitly and directly relevant to {topic}. "
104
+ "Strictly avoid hallucinated, fabricated, or speculative statistical data and patent numbers in the analysis. "
105
+ "Conduct a thorough market gap analysis, identifying 4-5 highly actionable and verifiable innovation opportunities aligned with {topic}, "
106
+ "emphasizing sustainability, emerging technology integration, industry collaboration, and competitor positioning. "
107
+ "Evaluate competitor patent strategies with factual data to uncover untapped opportunities and competitive advantages. "
108
+ "All innovation hotspots and emerging technology suggestions must be strictly aligned with {topic} - no generic or unrelated recommendations are allowed. "
109
+ "Deliver highly actionable, data-driven insights to support strategic decision-making and long-term growth. "
110
+ "Present findings in a structured, well-organized format using 'Category' and 'Values' keys for easy data interpretation."
111
+
112
+ )
113
+ )
114
+ else:
115
+ #planner_goal = (
116
+ # "Conduct comprehensive, data-driven research on patent filings, technological innovation, and market dynamics strictly within the {topic} sector."
117
+ # "Avoid unrelated or generic recommendations."
118
+ # "Identify key players, emerging technologies, competitor strategies, and market gaps with factually accurate and verifiable data. "
119
+ # "Strictly avoid hallucinated, fabricated, or speculative findings. "
120
+ # "Develop a phased Patent Filing Roadmap targeting emerging technologies and high-growth regions tailored to the specific needs"
121
+ # "and strategic goals of {stakeholder}. Conduct a Risk Assessment covering regulatory barriers, supply chain risks, and"
122
+ # "material costs with mitigation strategies. Suggest Investment Opportunities by identifying startups or strategic partnerships in {topic}."
123
+ # "Present findings using a Phased Patent Strategy Table (short-term, mid-term, long-term) and a Risk Matrix."
124
+ #)
125
+ planner_goal = (
126
+ "Conduct comprehensive, data-driven research on patent filings, technological innovations, and market dynamics strictly within the {topic} sector. "
127
+ "Deliver factually accurate and verifiable insights with no hallucinated, fabricated, or speculative findings. Avoid unrelated or generic recommendations. "
128
+ "Provide detailed outputs in the following structure:\n\n"
129
+
130
+ "1. Market Trends & Competitive Landscape:\n"
131
+ "- In-depth company profiles (recent patent filings, R&D investments, partnerships).\n"
132
+ "- Competitor Benchmarking (material innovations vs. integration techniques, etc.).\n"
133
+ "- Competitor Comparison Matrix (tabular comparison on innovation focus, market share, partnerships).\n"
134
+ "- Competitor Expansion Plans (predict market entries, M&A, product launches).\n\n"
135
+
136
+ "2. Emerging Technologies:\n"
137
+ "- Identify 4–5 key emerging technologies shaping the {topic} market.\n"
138
+ "- Classify technologies by Technology Readiness Level (TRL).\n"
139
+ "- Highlight disruptive technologies (e.g., metamaterials, quantum antennas).\n"
140
+ "- Analyze breakthroughs related to {topic} domain.\n"
141
+ "- Predict adoption timelines with Technology Adoption Curves.\n\n"
142
+
143
+ "3. Untapped Innovation Hotspots:\n"
144
+ "- Provide a Regional Patent Trends.\n"
145
+ "- Identify Innovation Clusters (universities, startups).\n"
146
+ "- Conduct Regulatory Landscape Analysis.\n"
147
+ "- Explore Application Expansion (non-traditional sectors).\n"
148
+ "- Evaluate Infrastructure Readiness (supply chains, logistics).\n\n"
149
+
150
+ "4. Strategic Opportunities & Actionable Recommendations:\n"
151
+ "- Develop a Phased Patent Strategy (short-term, mid-term, long-term).\n"
152
+ "- Conduct Risk Assessments (regulatory, supply chain, operational risks).\n"
153
+ "- Suggest Open Innovation Partnerships (universities, labs).\n"
154
+ "- Propose a Supply Chain Strategy (risk mitigation, diversification).\n"
155
+ "- Identify Investment Opportunities (startups, partnerships).\n"
156
+ "- Recommend Competitive Differentiation Strategies (blocking patents, cross-licensing).\n"
157
+ "- Market Entry Roadmap aligning patent filings with market strategies.\n\n"
158
+
159
+ "5. Future Growth Projections:\n"
160
+ "- Provide Market Size Forecasts (CAGR, revenue projections for 1–3 and 5–10 years).\n"
161
+ "- Analyze Demand Drivers and Barriers (tech adoption, regulatory hurdles, etc.).\n"
162
+ "- Conduct Scenario Analysis (best-case, worst-case, base-case).\n"
163
+ "- Predict Competitor Expansion Plans.\n"
164
+ "- Present Growth Projection Graphs and Forecast Summaries.\n\n"
165
+
166
+ "6. Industry Risk & Compliance Analysis:\n"
167
+ "- Identify Market, Operational, and Regulatory Risks.\n"
168
+ "- Develop Mitigation Strategies (compliance pathways, supply chain diversification).\n"
169
+ "- Provide a Regulatory Risk Matrix.\n\n"
170
+
171
+ "7. Summary & Appendix:\n"
172
+ "- Summarize key insights in bullet-point format.\n"
173
+ "- Include supplementary data, raw analysis, and research methodology."
174
+ )
175
+
176
+
177
+ writer_goal = (
178
+ "Develop a high-impact, professionally structured insights report that integrates verified research data and strategic analysis into the following sections:\n\n"
179
+ "1. Executive Summary: Provide a concise overview of key insights and strategic recommendations strictly aligned with {stakeholder}'s strategic objectives.\n"
180
+ "2. Market Trends & Competitive Landscape: Detailed profiles, benchmarking, and market entry insights.\n"
181
+ "3. Emerging Technologies: Spotlight key technologies, TRL classification, adoption timelines.\n"
182
+ "4. Untapped Innovation Hotspots: Highlight regions with potential, innovation clusters, and readiness analysis.\n"
183
+ "5. Strategic Opportunities & Actionable Recommendations: Patent filing roadmap, risk mitigation, partnerships, and differentiation.\n"
184
+ "6. Future Growth Projections: Market forecasts, technology adoption, scenario analysis.\n"
185
+ "7. Industry Risk & Compliance Analysis: Risk matrix and mitigation strategies.\n"
186
+ "8. Summary & Appendix: Bullet-point insights and supporting data.\n\n"
187
+
188
+ "Ensure all insights, emerging technologies, and identified innovation gaps are fact-based, verifiable, and directly relevant to the {topic}."
189
+ "Present findings with clarity through well-structured sections, bullet points, and tables. Explicitly avoid hallucinated, fabricated, or speculative content throughout the report."
190
+
191
+ )
192
+
193
+
194
+ analyst_goal = (
195
+ "Perform precise, data-driven statistical analysis of patent filings, growth trends, and innovation distribution strictly within the {topic} sector, "
196
+ "specifically customized to the strategic needs of {stakeholder}. "
197
+ "Identify top regions, leading assignees/companies, and emerging technologies that are explicitly and directly relevant to {topic}. "
198
+ "Identify 4-5 emerging technologies shaping the {topic} market with brief impact analyses."
199
+ "Highlight untapped markets and geographic regions with low patent activity but high growth potential."
200
+ "Provide Technology Spotlight Cards detailing each technology's name, description, development stage, and potential market impact."
201
+ "Strictly avoid hallucinated, fabricated, or speculative statistical data and patent numbers in the analysis. "
202
+ "Conduct a thorough market gap analysis, identifying 4-5 highly actionable and verifiable innovation opportunities aligned with {topic}, "
203
+ "emphasizing sustainability, emerging technology integration, industry collaboration, and competitor positioning. "
204
+ "Evaluate competitor patent strategies with factual data to uncover untapped opportunities and competitive advantages. "
205
+ "All innovation hotspots and emerging technology suggestions must be strictly aligned with {topic} - no generic or unrelated recommendations are allowed. "
206
+ "Deliver highly actionable, data-driven insights to support strategic decision-making and long-term growth. "
207
+ "Present findings in a structured, well-organized format using 'Category' and 'Values' keys for easy data interpretation."
208
+ )
209
+
210
+ # Agent Definitions
211
+ planner = Agent(
212
+ role="Patent Research Consultant",
213
+ goal=planner_goal,
214
+ backstory=(
215
+ "You're tasked with researching {topic} patents and identifying key trends and players. Your work supports the Patent Writer and Data Analyst."
216
+ ),
217
+ allow_delegation=False,
218
+ verbose=True,
219
+ llm=llm
220
+ )
221
+
222
+ writer = Agent(
223
+ role="Patent Insights Writer",
224
+ goal=writer_goal,
225
+ backstory=(
226
+ "Using the research from the Planner and data from the Analyst, craft a professional document summarizing patent insights for {stakeholder}."
227
+ ),
228
+ allow_delegation=False,
229
+ verbose=True,
230
+ llm=llm
231
+ )
232
+
233
+ analyst = Agent(
234
+ role="Patent Data Analyst",
235
+ goal=analyst_goal,
236
+ backstory=(
237
+ "Analyze patent filing data and innovation trends in {topic} to provide statistical insights. Your analysis will guide the Writer's final report."
238
+ ),
239
+ allow_delegation=False,
240
+ verbose=True,
241
+ llm=llm
242
+ )
243
+
244
+ # Task Definitions
245
+ plan = Task(
246
+ description=(
247
+ "1. Conduct comprehensive, fact-based research on recent trends in {topic} patent filings and innovation.\n"
248
+ "2. Identify key players, emerging technologies, and market gaps that are strictly relevant to {topic}.\n"
249
+ "3. Ensure all findings—especially emerging technologies and innovation hotspots—are explicitly aligned with {topic}.\n"
250
+ "4. Avoid speculative, fabricated, or unrelated content entirely.\n"
251
+ "5. Provide actionable, data-backed strategic recommendations aligned with {stakeholder}'s goals.\n"
252
+ "6. Limit the output to 600 words."
253
+ ),
254
+ expected_output="A fact-driven research document with strictly relevant insights, strategic recommendations, and key statistics.",
255
+ agent=planner
256
+ )
257
+
258
+ write = Task(
259
+ description=(
260
+ "1. Use the Planner's and Analyst's strictly topic-aligned outputs to craft a professional patent insights document.\n"
261
+ "2. Include key findings, visual aids, and actionable strategies strictly related to {topic}.\n"
262
+ "3. Highlight strategic directions and strictly relevant innovation opportunities.\n"
263
+ "4. Incorporate well-structured tables for key statistics and example inventions without using any fabricated data or fake patent numbers.\n"
264
+ "5. Avoid any speculative, fabricated, or unrelated content.\n"
265
+ "6. Limit the document to 600 words."
266
+ ),
267
+ expected_output="A polished, stakeholder-ready patent insights report with actionable, strictly relevant recommendations.",
268
+ agent=writer
269
+ )
270
+
271
+ analyse = Task(
272
+ description=(
273
+ "1. Conduct a comprehensive statistical analysis of patent filing trends, innovation hot spots, and future growth projections in the {topic} sector.\n"
274
+ "2. Identify and rank the top regions, leading assignees/companies driving innovation.\n"
275
+ "3. Highlight regional innovation trends and the distribution of emerging technologies across different geographies.\n"
276
+ "4. Provide actionable insights and strategic recommendations based on the data.\n"
277
+ "5. Categorize outputs as either:\n"
278
+ " - 'Data Insight' for visualizations and tables (quantitative data, trends, technologies).\n"
279
+ " - 'Key Insight' for strategic recommendations and innovation opportunities.\n"
280
+ "6. Example Output Format:\n"
281
+ "[\n"
282
+ " {{'Category': 'Top Regions', 'Type': 'Data Insight', 'Values': {{'North America': 120, 'Europe': 95}},\n"
283
+ " {{'Category': 'Emerging Technologies', 'Type': 'Data Insight', 'Values': ['Transparent Conductive Films']}},\n"
284
+ " {{'Category': 'Strategic Insights', 'Type': 'Key Insight', 'Values': 'Collaborate with material science companies to develop advanced transparent antennas.'}},\n"
285
+ " {{'Category': 'Innovation Gaps', 'Type': 'Key Insight', 'Values': 'Limited patents in self-healing transparent materials present a growth opportunity.'}}\n"
286
+ "]\n"
287
+ "7. Ensure all data is factually accurate, verifiable, and strictly aligned with {topic}."
288
+ ),
289
+ expected_output="A structured dataset combining Data Insights for comprehensive visualizations and table reporting, and Key Insights for strategic actions.",
290
+ agent=analyst
291
+ )
292
+
293
+ crew = Crew(
294
+ agents=[planner, analyst, writer],
295
+ tasks=[plan, analyse, write],
296
+ verbose=True
297
+ )
298
+
299
+ # PDF Report Generation
300
+ def generate_pdf_report(result, charts=None, table_data=None, metadata=None, key_insights=None):
301
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf:
302
+ pdf = FPDF()
303
+ pdf.add_page()
304
+
305
+ # Add DejaVu fonts (regular and bold)
306
+ pdf.add_font('DejaVu', '', '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', uni=True)
307
+ pdf.add_font('DejaVu', 'B', '/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf', uni=True)
308
+
309
+ pdf.set_font("DejaVu", size=12)
310
+ pdf.set_auto_page_break(auto=True, margin=15)
311
+
312
+ # Title (Bold)
313
+ pdf.set_font("DejaVu", size=16, style="B")
314
+ pdf.cell(200, 10, txt="Patent Strategy and Innovation Report", ln=True, align="C")
315
+ pdf.ln(10)
316
+
317
+ # Metadata Section
318
+ if metadata:
319
+ pdf.set_font("DejaVu", size=10)
320
+ for key, value in metadata.items():
321
+ pdf.cell(200, 10, txt=f"{key}: {value}", ln=True)
322
+
323
+ # Report Content
324
+ pdf.set_font("DejaVu", size=12)
325
+ pdf.multi_cell(0, 10, txt=result)
326
+
327
+ # Key Insights Section
328
+ if key_insights:
329
+ pdf.add_page()
330
+ pdf.set_font("DejaVu", size=14, style="B")
331
+ pdf.cell(200, 10, txt="Key Strategic Insights", ln=True)
332
+ pdf.ln(5)
333
+ pdf.set_font("DejaVu", size=12)
334
+ for insight in key_insights:
335
+ pdf.multi_cell(0, 10, txt=f"- {insight}")
336
+
337
+ # Insert Charts
338
+ if charts:
339
+ for chart_path in charts:
340
+ try:
341
+ pdf.add_page()
342
+ pdf.image(chart_path, x=10, y=20, w=180)
343
+ except Exception as e:
344
+ logging.error(f"Error including chart: {e}")
345
+
346
+ # Insert Tables
347
+ if table_data:
348
+ pdf.add_page()
349
+ pdf.set_font("DejaVu", size=10)
350
+ pdf.cell(200, 10, txt="Consolidated Data Table:", ln=True, align="L")
351
+ for row in table_data:
352
+ pdf.cell(200, 10, txt=str(row), ln=True)
353
+
354
+ pdf.output(temp_pdf.name)
355
+ return temp_pdf.name
356
+
357
+
358
+ # Data Validation
359
+ def validate_analyst_output(analyst_output):
360
+ if not analyst_output:
361
+ st.warning("No data available for analysis.")
362
+ logging.warning("Validation Failed: Analyst output is empty.")
363
+ return None
364
+
365
+ if not isinstance(analyst_output, list):
366
+ st.warning("Analyst output must be a list of dictionaries.")
367
+ logging.warning(f"Validation Failed: Analyst output is not a list. Type: {type(analyst_output)}")
368
+ return None
369
+
370
+ required_keys = {'Category', 'Values'}
371
+ for idx, item in enumerate(analyst_output):
372
+ if not isinstance(item, dict):
373
+ st.warning(f"Item at index {idx} is not a dictionary.")
374
+ logging.warning(f"Validation Failed: Item {idx} is not a dictionary. Item: {item}")
375
+ return None
376
+ if not required_keys.issubset(item.keys()):
377
+ st.warning(f"Item at index {idx} missing keys: {required_keys - set(item.keys())}")
378
+ logging.warning(f"Validation Failed: Item {idx} missing keys: {required_keys - set(item.keys())}")
379
+ return None
380
+
381
+ return analyst_output
382
+
383
+
384
+ # Visualization and Table Display
385
+ def create_visualizations(analyst_output):
386
+ chart_paths = []
387
+ validated_data = validate_analyst_output(analyst_output)
388
+
389
+ if validated_data:
390
+ for item in validated_data:
391
+ category = item["Category"]
392
+ values = item["Values"]
393
+
394
+ try:
395
+ # Handle dictionary data for bar charts
396
+ if isinstance(values, dict):
397
+ df = pd.DataFrame(list(values.items()), columns=["Label", "Count"])
398
+ if len(df) <= 5:
399
+ chart = px.pie(df, names="Label", values="Count", title=f"{category} Distribution")
400
+ else:
401
+ chart = px.bar(df, x="Label", y="Count", title=f"{category} Analysis")
402
+
403
+ # Handle list data for bar/pie charts
404
+ elif isinstance(values, list):
405
+ # Check if it's a list of dictionaries (e.g., Technology Spotlight)
406
+ if all(isinstance(v, dict) for v in values):
407
+ df = pd.DataFrame(values)
408
+ st.subheader(f"{category} (Detailed View)")
409
+ st.dataframe(df)
410
+ continue # Skip chart for detailed data
411
+
412
+ # Frequency analysis for simple lists
413
+ else:
414
+ df = pd.DataFrame(values, columns=["Items"])
415
+ df = df["Items"].value_counts().reset_index()
416
+ df.columns = ["Label", "Count"]
417
+ chart = px.pie(df, names="Label", values="Count", title=f"{category} Distribution") if len(df) <= 5 else px.bar(df, x="Label", y="Count", title=f"{category} Frequency")
418
+
419
+ # Handle string data (Insights)
420
+ elif isinstance(values, str):
421
+ st.subheader(f"{category} Insights")
422
+ st.table(pd.DataFrame({"Insights": [values]}))
423
+ continue
424
+
425
+ else:
426
+ st.warning(f"Unsupported data format for category: {category}")
427
+ logging.warning(f"Unsupported data format in {category}: {values}")
428
+ continue
429
+
430
+ # Display in Streamlit
431
+ st.plotly_chart(chart)
432
+
433
+ # Save for PDF export
434
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_chart:
435
+ chart.write_image(temp_chart.name)
436
+ chart_paths.append(temp_chart.name)
437
+
438
+ except Exception as e:
439
+ st.error(f"Failed to generate visualization for {category}: {e}")
440
+ logging.error(f"Visualization error in {category}: {e}")
441
+
442
+ return chart_paths
443
+
444
+ def display_table(analyst_output):
445
+ table_data = []
446
+ validated_data = validate_analyst_output(analyst_output)
447
+
448
+ if validated_data:
449
+ for item in validated_data:
450
+ category = item["Category"]
451
+ values = item["Values"]
452
+
453
+ try:
454
+ # Handle dictionary data (Table View)
455
+ if isinstance(values, dict):
456
+ df = pd.DataFrame(list(values.items()), columns=["Label", "Count"])
457
+ st.subheader(f"{category} (Table View)")
458
+ st.dataframe(df)
459
+ table_data.extend(df.to_dict(orient="records"))
460
+
461
+ # Handle list data
462
+ elif isinstance(values, list):
463
+ # Handle complex lists (list of dictionaries)
464
+ if all(isinstance(v, dict) for v in values):
465
+ df = pd.DataFrame(values)
466
+ st.subheader(f"{category} (Detailed View)")
467
+ st.dataframe(df)
468
+ table_data.extend(df.to_dict(orient="records"))
469
+ # Handle simple lists
470
+ else:
471
+ df = pd.DataFrame(values, columns=["Items"])
472
+ st.subheader(f"{category} (List View)")
473
+ st.dataframe(df)
474
+ table_data.extend(df.to_dict(orient="records"))
475
+
476
+ # Handle text data
477
+ elif isinstance(values, str):
478
+ st.subheader(f"{category} (Summary)")
479
+ st.table(pd.DataFrame({"Insights": [values]}))
480
+ table_data.append({"Category": category, "Values": values})
481
+
482
+ else:
483
+ st.warning(f"Unsupported data format for {category}")
484
+ logging.warning(f"Unsupported data in {category}: {values}")
485
+
486
+ except Exception as e:
487
+ st.error(f"Error displaying {category}: {e}")
488
+ logging.error(f"Display error for {category}: {e}")
489
+
490
+ return table_data
491
+
492
+
493
+ def parse_analyst_output(raw_output):
494
+ key_insights = []
495
+ data_insights = []
496
+
497
+ try:
498
+ # Correctly parse the raw output
499
+ structured_data = ast.literal_eval(raw_output) if isinstance(raw_output, str) else raw_output
500
+
501
+ for item in structured_data:
502
+ if "Category" not in item or "Values" not in item:
503
+ logging.warning(f"Missing 'Category' or 'Values' in item: {item}")
504
+ continue
505
+
506
+ if item.get("Type") == "Key Insight":
507
+ key_insights.append(item["Values"])
508
+ elif item.get("Type") == "Data Insight":
509
+ # Handle nested structures (e.g., Technology Spotlight Cards)
510
+ if isinstance(item["Values"], list):
511
+ for sub_item in item["Values"]:
512
+ data_insights.append({"Category": item["Category"], "Values": sub_item})
513
+ else:
514
+ data_insights.append(item)
515
+ else:
516
+ data_insights.append(item)
517
+
518
+ except Exception as e:
519
+ logging.error(f"Error parsing analyst output: {e}")
520
+
521
+ return key_insights, data_insights
522
+
523
+
524
+ # Main Execution Block
525
+ if st.button("Generate Patent Insights"):
526
+ with st.spinner('Processing...'):
527
+ try:
528
+ # Start the timer
529
+ start_time = time.time()
530
+
531
+ # Kick off the crew with user inputs
532
+ if not patent_area or not stakeholder:
533
+ st.error("Please provide both Patent Technology Area and Stakeholder.")
534
+ else:
535
+ logging.info(f"Starting analysis with Topic: {patent_area}, Stakeholder: {stakeholder}")
536
+ results = crew.kickoff(inputs={"topic": patent_area, "stakeholder": stakeholder})
537
+
538
+ # Calculate elapsed time
539
+ elapsed_time = time.time() - start_time
540
+
541
+ # Extract Writer's Output
542
+ writer_output = getattr(results.tasks_output[2], "raw", "No details available.")
543
+ if writer_output and writer_output.strip():
544
+ st.markdown("### Final Report")
545
+ st.write(writer_output)
546
+ else:
547
+ st.warning("No final report available.")
548
+
549
+ # Expandable section for detailed insights
550
+ with st.expander("Explore Detailed Insights"):
551
+ tab1, tab2 = st.tabs(["Planner's Insights", "Analyst's Analysis"])
552
+
553
+ # Planner's Insights
554
+ with tab1:
555
+ planner_output = getattr(results.tasks_output[0], "raw", "No details available.")
556
+ if planner_output and planner_output.strip():
557
+ st.write(planner_output)
558
+ else:
559
+ st.warning("No planner insights available.")
560
+
561
+ # Analyst's Analysis
562
+ with tab2:
563
+ analyst_output = getattr(results.tasks_output[1], "raw", "No details available.")
564
+ if analyst_output and analyst_output.strip():
565
+ st.write(analyst_output)
566
+
567
+ # Parse Analyst Output (Key Insights + Data Insights)
568
+ key_insights, data_insights = parse_analyst_output(analyst_output)
569
+ st.subheader("Structured Analyst Output")
570
+ st.write(data_insights)
571
+
572
+ # Create Visualizations if enabled
573
+ charts = []
574
+ if enable_advanced_analysis and data_insights:
575
+ charts = create_visualizations(data_insights)
576
+ else:
577
+ st.info("No data insights available for visualizations.")
578
+
579
+ # Display Data Tables
580
+ table_data = display_table(data_insights)
581
+
582
+ else:
583
+ st.warning("No analyst analysis available.")
584
+
585
+ # Notify user that the analysis is complete
586
+ st.success(f"Analysis completed in {elapsed_time:.2f} seconds.")
587
+
588
+ # Generate the PDF report with Key Insights and Data Insights
589
+ if writer_output:
590
+ pdf_path = generate_pdf_report(
591
+ result=writer_output,
592
+ charts=charts,
593
+ table_data=data_insights,
594
+ metadata={"Technology Area": patent_area, "Stakeholder": stakeholder},
595
+ key_insights=key_insights # Pass key insights to the PDF
596
+ )
597
+
598
+ # Download button for the generated PDF
599
+ with open(pdf_path, "rb") as report_file:
600
+ st.download_button(
601
+ label="📄 Download Report",
602
+ data=report_file,
603
+ file_name="Patent_Strategy_Report.pdf",
604
+ mime="application/pdf"
605
+ )
606
+ else:
607
+ st.warning("Report generation skipped due to missing content.")
608
+
609
+ except Exception as e:
610
+ error_message = traceback.format_exc()
611
+ logging.error(f"An error occurred during execution:\n{error_message}")
612
+ st.error(f"⚠️ An unexpected error occurred:\n{e}")