DrishtiSharma commited on
Commit
fcdabc4
·
verified ·
1 Parent(s): a1002cf

Create app.py

Browse files
Files changed (1) hide show
  1. mylab/best/app.py +590 -0
mylab/best/app.py ADDED
@@ -0,0 +1,590 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ return None
363
+ if not isinstance(analyst_output, list) or not all(isinstance(item, dict) for item in analyst_output):
364
+ st.warning("Analyst output must be a list of dictionaries.")
365
+ return None
366
+ required_keys = {'Category', 'Values'}
367
+ if not all(required_keys.issubset(item.keys()) for item in analyst_output):
368
+ st.warning(f"Each dictionary must contain keys: {required_keys}")
369
+ return None
370
+ return analyst_output
371
+
372
+ # Visualization and Table Display
373
+ def create_visualizations(analyst_output):
374
+ chart_paths = []
375
+ validated_data = validate_analyst_output(analyst_output)
376
+
377
+ if validated_data:
378
+ for item in validated_data:
379
+ category = item["Category"]
380
+ values = item["Values"]
381
+
382
+ try:
383
+ # Handle dictionary data
384
+ if isinstance(values, dict):
385
+ df = pd.DataFrame(list(values.items()), columns=["Label", "Count"])
386
+
387
+ # Choose Pie Chart for fewer categories, else Bar Chart
388
+ if len(df) <= 5:
389
+ chart = px.pie(df, names="Label", values="Count", title=f"{category} Distribution")
390
+ else:
391
+ chart = px.bar(df, x="Label", y="Count", title=f"{category} Analysis")
392
+
393
+ # Handle list data
394
+ elif isinstance(values, list):
395
+ # Convert the list into a frequency count without dummy values
396
+ df = pd.DataFrame(values, columns=["Label"])
397
+ df = df["Label"].value_counts().reset_index()
398
+ df.columns = ["Label", "Count"]
399
+
400
+ # Plot as a bar chart or pie chart
401
+ if len(df) <= 5:
402
+ chart = px.pie(df, names="Label", values="Count", title=f"{category} Distribution")
403
+ else:
404
+ chart = px.bar(df, x="Label", y="Count", title=f"{category} Frequency")
405
+
406
+ # Handle text data
407
+ elif isinstance(values, str):
408
+ st.subheader(f"{category} Insights")
409
+ st.table(pd.DataFrame({"Insights": [values]}))
410
+ continue # No chart for text data
411
+
412
+ else:
413
+ st.warning(f"Unsupported data format for category: {category}")
414
+ continue
415
+
416
+ # Display the chart in Streamlit
417
+ st.plotly_chart(chart)
418
+
419
+ # Save the chart for PDF export
420
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_chart:
421
+ chart.write_image(temp_chart.name)
422
+ chart_paths.append(temp_chart.name)
423
+
424
+ except Exception as e:
425
+ st.error(f"Failed to generate visualization for {category}: {e}")
426
+ logging.error(f"Error in {category} visualization: {e}")
427
+
428
+ return chart_paths
429
+
430
+ def display_table(analyst_output):
431
+ table_data = []
432
+ validated_data = validate_analyst_output(analyst_output)
433
+
434
+ if validated_data:
435
+ for item in validated_data:
436
+ category = item["Category"]
437
+ values = item["Values"]
438
+
439
+ # Error handling to prevent crashes
440
+ try:
441
+ # Handle dictionary data (Table View)
442
+ if isinstance(values, dict):
443
+ df = pd.DataFrame(list(values.items()), columns=["Label", "Count"])
444
+ st.subheader(f"{category} (Table View)")
445
+ st.dataframe(df)
446
+ table_data.extend(df.to_dict(orient="records"))
447
+
448
+ # Handle list data (List View)
449
+ elif isinstance(values, list):
450
+ df = pd.DataFrame(values, columns=["Items"])
451
+ st.subheader(f"{category} (List View)")
452
+ st.dataframe(df)
453
+ table_data.extend(df.to_dict(orient="records"))
454
+
455
+ # Handle text data (Summary View)
456
+ elif isinstance(values, str):
457
+ st.subheader(f"{category} (Summary)")
458
+ st.table(pd.DataFrame({"Insights": [values]}))
459
+ table_data.append({"Category": category, "Values": values})
460
+
461
+ else:
462
+ st.warning(f"Unsupported data format for category: {category}")
463
+
464
+ except Exception as e:
465
+ logging.error(f"Error processing {category}: {e}")
466
+ st.error(f"Failed to display {category} as a table due to an error.")
467
+
468
+ return table_data
469
+
470
+ def parse_analyst_output(raw_output):
471
+ key_insights = []
472
+ data_insights = []
473
+
474
+ try:
475
+ # Correctly parse the raw output
476
+ structured_data = ast.literal_eval(raw_output) if isinstance(raw_output, str) else raw_output
477
+
478
+ for item in structured_data:
479
+ if "Category" not in item or "Values" not in item:
480
+ logging.warning(f"Missing 'Category' or 'Values' in item: {item}")
481
+ continue
482
+
483
+ if item.get("Type") == "Key Insight":
484
+ key_insights.append(item["Values"])
485
+ elif item.get("Type") == "Data Insight":
486
+ # Handle nested structures (e.g., Technology Spotlight Cards)
487
+ if isinstance(item["Values"], list):
488
+ for sub_item in item["Values"]:
489
+ data_insights.append({"Category": item["Category"], "Values": sub_item})
490
+ else:
491
+ data_insights.append(item)
492
+ else:
493
+ data_insights.append(item)
494
+
495
+ except Exception as e:
496
+ logging.error(f"Error parsing analyst output: {e}")
497
+
498
+ return key_insights, data_insights
499
+
500
+
501
+ # Main Execution Block
502
+ if st.button("Generate Patent Insights"):
503
+ with st.spinner('Processing...'):
504
+ try:
505
+ # Start the timer
506
+ start_time = time.time()
507
+
508
+ # Kick off the crew with user inputs
509
+ if not patent_area or not stakeholder:
510
+ st.error("Please provide both Patent Technology Area and Stakeholder.")
511
+ else:
512
+ logging.info(f"Starting analysis with Topic: {patent_area}, Stakeholder: {stakeholder}")
513
+ results = crew.kickoff(inputs={"topic": patent_area, "stakeholder": stakeholder})
514
+
515
+
516
+ # Calculate elapsed time
517
+ elapsed_time = time.time() - start_time
518
+
519
+ # Extract Writer's Output
520
+ writer_output = getattr(results.tasks_output[2], "raw", "No details available.")
521
+ if writer_output and writer_output.strip():
522
+ st.markdown("### Final Report")
523
+ st.write(writer_output)
524
+ else:
525
+ st.warning("No final report available.")
526
+
527
+ # Expandable section for detailed insights
528
+ with st.expander("Explore Detailed Insights"):
529
+ tab1, tab2 = st.tabs(["Planner's Insights", "Analyst's Analysis"])
530
+
531
+ # Planner's Insights
532
+ with tab1:
533
+ planner_output = getattr(results.tasks_output[0], "raw", "No details available.")
534
+ if planner_output and planner_output.strip():
535
+ st.write(planner_output)
536
+ else:
537
+ st.warning("No planner insights available.")
538
+
539
+ # Analyst's Analysis
540
+ with tab2:
541
+ analyst_output = getattr(results.tasks_output[1], "raw", "No details available.")
542
+ if analyst_output and analyst_output.strip():
543
+ st.write(analyst_output)
544
+
545
+ # Parse Analyst Output (Key Insights + Data Insights)
546
+ key_insights, data_insights = parse_analyst_output(analyst_output)
547
+ st.subheader("Structured Analyst Output")
548
+ st.write(data_insights)
549
+
550
+ # Create Visualizations if enabled
551
+ charts = []
552
+ if enable_advanced_analysis and data_insights:
553
+ charts = create_visualizations(data_insights)
554
+ else:
555
+ st.info("No data insights available for visualizations.")
556
+
557
+ # Display Data Tables
558
+ table_data = display_table(data_insights)
559
+
560
+ else:
561
+ st.warning("No analyst analysis available.")
562
+
563
+ # Notify user that the analysis is complete
564
+ st.success(f"Analysis completed in {elapsed_time:.2f} seconds.")
565
+
566
+ # Generate the PDF report with Key Insights and Data Insights
567
+ if writer_output:
568
+ pdf_path = generate_pdf_report(
569
+ result=writer_output,
570
+ charts=charts,
571
+ table_data=data_insights,
572
+ metadata={"Technology Area": patent_area, "Stakeholder": stakeholder},
573
+ key_insights=key_insights # Pass key insights to the PDF
574
+ )
575
+
576
+ # Download button for the generated PDF
577
+ with open(pdf_path, "rb") as report_file:
578
+ st.download_button(
579
+ label="📄 Download Report",
580
+ data=report_file,
581
+ file_name="Patent_Strategy_Report.pdf",
582
+ mime="application/pdf"
583
+ )
584
+ else:
585
+ st.warning("Report generation skipped due to missing content.")
586
+
587
+ except Exception as e:
588
+ error_message = traceback.format_exc()
589
+ logging.error(f"An error occurred during execution:\n{error_message}")
590
+ st.error(f"⚠️ An unexpected error occurred:\n{e}")