DrishtiSharma commited on
Commit
0d3b200
·
verified ·
1 Parent(s): 1c40a57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +409 -138
app.py CHANGED
@@ -1,4 +1,3 @@
1
- # to-do: add inventor + assignee
2
  import streamlit as st
3
  from crewai import Agent, Task, Crew
4
  import os
@@ -9,7 +8,9 @@ import pandas as pd
9
  import plotly.express as px
10
  import tempfile
11
  import time
 
12
  import logging
 
13
 
14
  # Setup logging
15
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
@@ -17,7 +18,7 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(
17
  # Title and Application Introduction
18
  st.title("Patent Strategy and Innovation Consultant")
19
  st.sidebar.write(
20
- "This application uses AI to provide actionable insights and comprehensive analysis for patent-related strategies."
21
  )
22
 
23
  # User Input Section
@@ -29,17 +30,14 @@ stakeholder = st.text_input("Enter Stakeholder", value="Patent Attorneys")
29
  llm = None
30
 
31
  # Model Selection
32
- #st.header("Model Selection")
33
- model_choice = st.radio("Select LLM", ["GPT-4o", "llama-3.3-70b-versatile"], 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
- #llm = ChatGroq(groq_api_key=os.getenv("GROQ_API_KEY"), model="groq/llama-3.3-70b-versatile")
41
-
42
- if model_choice == "llama-3.3-70b-versatile":
43
  if not groq_api_key:
44
  st.error("Groq API key is missing. Please set the GROQ_API_KEY environment variable.")
45
  llm = None
@@ -52,6 +50,7 @@ elif model_choice == "GPT-4o":
52
  else:
53
  llm = ChatOpenAI(api_key=openai_api_key, model="gpt-4o")
54
 
 
55
  # Advanced Options
56
  st.sidebar.header("Advanced Options")
57
  enable_advanced_analysis = st.sidebar.checkbox("Enable Advanced Analysis", value=True)
@@ -64,21 +63,149 @@ with st.sidebar.expander("Customize Agent Goals", expanded=False):
64
  if enable_customization:
65
  planner_goal = st.text_area(
66
  "Planner Goal",
67
- value="Research trends in patent filings and technological innovation, identify key players, and provide strategic recommendations."
 
 
 
 
 
 
 
 
 
 
 
 
68
  )
69
  writer_goal = st.text_area(
70
  "Writer Goal",
71
- value="Craft a professional insights document summarizing trends, strategies, and actionable outcomes for stakeholders."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  )
73
  analyst_goal = st.text_area(
74
  "Analyst Goal",
75
- value="Perform detailed statistical analysis of patent filings, growth trends, and innovation distribution."
 
 
 
 
 
 
 
 
 
 
 
 
76
  )
77
  else:
78
- planner_goal = "Research trends in patent filings and technological innovation, identify key players, and provide strategic recommendations."
79
- writer_goal = "Craft a professional insights document summarizing trends, strategies, and actionable outcomes for stakeholders."
80
- analyst_goal = "Perform detailed statistical analysis of patent filings, growth trends, and innovation distribution."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
  # Agent Definitions
84
  planner = Agent(
@@ -117,72 +244,52 @@ analyst = Agent(
117
  # Task Definitions
118
  plan = Task(
119
  description=(
120
- "1. Research recent trends in {topic} patent filings and innovation.\n"
121
- "2. Identify key players and emerging technologies.\n"
122
- "3. Provide recommendations for stakeholders on strategic directions.\n"
123
- "4. Identify key statistics such as top regions, top players, and hot areas of innovation.\n"
124
- "5. Limit the output to 500 words."
 
125
  ),
126
- expected_output="A research document with structured insights, strategic recommendations, and key statistics.",
127
  agent=planner
128
  )
129
 
130
  write = Task(
131
  description=(
132
- "1. Use the Planner's and Analyst's outputs to craft a professional patent insights document.\n"
133
- "2. Include key findings, visual aids, and actionable strategies.\n"
134
- "3. Suggest strategic directions and highlight untapped innovation areas.\n"
135
- "4. Incorporate summarized tables for key statistics and example inventions.\n"
136
- "5. Limit the document to 650 words."
 
137
  ),
138
- expected_output="A polished, stakeholder-ready patent insights document with actionable recommendations.",
139
  agent=writer
140
  )
141
 
142
- #analyse = Task(
143
- # description=(
144
- # "1. Perform a detailed statistical analysis of patent filing trends, innovation hot spots, and growth projections.\n"
145
- # "2. Identify and list the top regions, top assignees, prominent inventors, and leading companies in the transparent antenna industry.\n"
146
- # "3. Highlight regional innovation trends and technology distribution across different geographies.\n"
147
- # "4. Generate structured output in a list of dictionaries with 'Category' and 'Values' fields for each analysis area. "
148
- # "This structured output will be utilized to create visualizations later on.\n"
149
- # "5. Example Output Format:\n"
150
- # "[{'Category': 'Top Regions', 'Values': ['North America', 'Europe', 'Asia-Pacific']},\n"
151
- # " {'Category': 'Top Assignees', 'Values': ['Company A', 'Company B']},\n"
152
- # " {'Category': 'Top Inventors', 'Values': ['Inventor X', 'Inventor Y']},\n"
153
- # " {'Category': 'Hot Areas of Innovation', 'Values': ['5G Integration', 'Graphene Antennas']}]\n"
154
- # "6. Collaborate with the Planner and Writer to align on data needs."
155
- # ),
156
- # expected_output="A structured list of dictionaries covering regions, assignees, inventors, and innovation trends for visualization and tabular reporting.",
157
- # agent=analyst
158
- #)
159
-
160
  analyse = Task(
161
  description=(
162
- "1. Conduct a comprehensive statistical analysis of patent filing trends, innovation hot spots, and future growth projections in the transparent antenna industry.\n"
163
- "2. Identify and rank the top regions, leading assignees, prominent inventors, and key companies driving innovation.\n"
164
  "3. Highlight regional innovation trends and the distribution of emerging technologies across different geographies.\n"
165
  "4. Provide actionable insights and strategic recommendations based on the data.\n"
166
- "5. Generate structured data in a list of dictionaries with 'Category' and 'Values' fields for each analysis area.\n"
167
- " - 'Values' must be structured to support visualizations and tables:\n"
168
- " a) Use a dictionary for counts (e.g., {'Region A': 120, 'Region B': 95}),\n"
169
- " b) Use a list for qualitative data (e.g., ['Technology A', 'Technology B']),\n"
170
- " c) Use descriptive text for strategic insights.\n"
171
  "6. Example Output Format:\n"
172
  "[\n"
173
- " {'Category': 'Top Regions', 'Values': {'North America': 120, 'Europe': 95, 'Asia-Pacific': 85}},\n"
174
- " {'Category': 'Top Assignees', 'Values': {'Company A': 40, 'Company B': 35}},\n"
175
- " {'Category': 'Emerging Technologies', 'Values': ['Graphene Antennas', '5G Integration']},\n"
176
- " {'Category': 'Strategic Insights', 'Values': 'Collaborations between automotive and material science industries are accelerating innovation.'}\n"
177
  "]\n"
178
- "7. Do NOT generate any visualizations directly. Focus only on delivering structured data that can be used to create visualizations and tables.\n"
179
- "8. Collaborate with the Planner and Writer to align analysis with reporting needs."
180
  ),
181
- expected_output="A structured dataset with numeric, list-based, and descriptive data, ready for visualizations and tabular presentation.",
182
  agent=analyst
183
  )
184
 
185
-
186
  crew = Crew(
187
  agents=[planner, analyst, writer],
188
  tasks=[plan, analyse, write],
@@ -190,152 +297,316 @@ crew = Crew(
190
  )
191
 
192
  # PDF Report Generation
193
- def generate_pdf_report(result, charts=None, table_data=None, metadata=None):
194
  with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf:
195
  pdf = FPDF()
196
  pdf.add_page()
197
- pdf.set_font("Arial", size=12)
 
 
 
 
 
198
  pdf.set_auto_page_break(auto=True, margin=15)
199
 
200
- pdf.set_font("Arial", size=16, style="B")
 
201
  pdf.cell(200, 10, txt="Patent Strategy and Innovation Report", ln=True, align="C")
202
  pdf.ln(10)
203
 
 
204
  if metadata:
205
- pdf.set_font("Arial", size=10)
206
  for key, value in metadata.items():
207
  pdf.cell(200, 10, txt=f"{key}: {value}", ln=True)
208
 
209
- pdf.set_font("Arial", size=12)
 
210
  pdf.multi_cell(0, 10, txt=result)
211
 
 
 
 
 
 
 
 
 
 
 
 
212
  if charts:
213
  for chart_path in charts:
214
  try:
215
  pdf.add_page()
216
  pdf.image(chart_path, x=10, y=20, w=180)
217
- logging.info(f"Successfully included chart: {chart_path}")
218
  except Exception as e:
219
- logging.error(f"Failed to include chart in PDF: {chart_path}. Error: {e}")
220
 
 
221
  if table_data:
222
  pdf.add_page()
223
- pdf.set_font("Arial", size=10)
224
- pdf.cell(200, 10, txt="Consolidated Table:", ln=True, align="L")
225
  for row in table_data:
226
  pdf.cell(200, 10, txt=str(row), ln=True)
227
 
228
  pdf.output(temp_pdf.name)
229
  return temp_pdf.name
230
 
 
231
  # Data Validation
232
  def validate_analyst_output(analyst_output):
233
  if not analyst_output:
234
  st.warning("No data available for analysis.")
 
235
  return None
236
- if not isinstance(analyst_output, list) or not all(isinstance(item, dict) for item in analyst_output):
 
237
  st.warning("Analyst output must be a list of dictionaries.")
 
238
  return None
 
239
  required_keys = {'Category', 'Values'}
240
- if not all(required_keys.issubset(item.keys()) for item in analyst_output):
241
- st.warning(f"Each dictionary must contain keys: {required_keys}")
242
- return None
 
 
 
 
 
 
 
243
  return analyst_output
244
 
 
245
  # Visualization and Table Display
246
  def create_visualizations(analyst_output):
247
  chart_paths = []
248
  validated_data = validate_analyst_output(analyst_output)
 
249
  if validated_data:
250
- data = pd.DataFrame(validated_data)
251
- try:
252
- if data.empty:
253
- raise ValueError("Data for visualizations is empty.")
254
-
255
- bar_chart = px.bar(data, x="Category", y="Values", title="Patent Trends by Category")
256
- st.plotly_chart(bar_chart)
257
- with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_chart:
258
- bar_chart.write_image(temp_chart.name)
259
- chart_paths.append(temp_chart.name)
260
-
261
- pie_chart = px.pie(data, names="Category", values="Values", title="Category Distribution")
262
- st.plotly_chart(pie_chart)
263
- with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_chart:
264
- pie_chart.write_image(temp_chart.name)
265
- chart_paths.append(temp_chart.name)
266
-
267
- heatmap_chart = px.density_heatmap(data, x="Category", y="Values", title="Regional Patent Density")
268
- st.plotly_chart(heatmap_chart)
269
- with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_chart:
270
- heatmap_chart.write_image(temp_chart.name)
271
- chart_paths.append(temp_chart.name)
272
-
273
- multi_line_chart = px.line(data, x="Category", y="Values", title="Trends Over Time")
274
- st.plotly_chart(multi_line_chart)
275
- with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_chart:
276
- multi_line_chart.write_image(temp_chart.name)
277
- chart_paths.append(temp_chart.name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
278
 
279
- except Exception as e:
280
- logging.error(f"Error generating visualization: {e}")
281
- st.error(f"Error generating visualization: {e}")
282
  return chart_paths
283
 
284
  def display_table(analyst_output):
285
  table_data = []
286
  validated_data = validate_analyst_output(analyst_output)
 
287
  if validated_data:
288
- data = pd.DataFrame(validated_data)
289
- st.dataframe(data)
290
- table_data = data.to_dict(orient="records")
291
- return table_data
292
 
293
- # Main Execution Block
294
- if st.button("Generate Insights"):
295
- if llm is None:
296
- st.error("Cannot proceed without a valid API key for the selected model.")
297
- else:
298
- with st.spinner('Processing...'):
299
  try:
300
- start_time = time.time()
301
- results = crew.kickoff(inputs={"topic": patent_area, "stakeholder": stakeholder})
302
- elapsed_time = time.time() - start_time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
303
 
304
- writer_output = getattr(results.tasks_output[2], "raw", "No details available.")
305
- if writer_output:
306
- st.markdown("### Final Report")
307
- st.write(writer_output)
 
 
 
 
 
 
 
 
 
 
 
 
308
  else:
309
- st.warning("No final report available.")
 
 
 
 
 
 
 
310
 
311
- with st.expander("Explore Detailed Insights"):
312
- tab1, tab2 = st.tabs(["Planner's Insights", "Analyst's Analysis"])
313
 
314
- with tab1:
315
- planner_output = getattr(results.tasks_output[0], "raw", "No details available.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
316
  st.write(planner_output)
 
 
317
 
318
- with tab2:
319
- analyst_output = getattr(results.tasks_output[1], "raw", "No details available.")
320
- st.subheader("Raw Analyst Output")
 
321
  st.write(analyst_output)
322
 
 
 
 
 
 
 
323
  charts = []
324
- if enable_advanced_analysis:
325
- charts = create_visualizations(analyst_output)
 
 
 
 
 
 
 
 
326
 
327
- table_data = display_table(analyst_output)
 
328
 
329
- st.success(f"Analysis completed in {elapsed_time:.2f} seconds.")
 
330
  pdf_path = generate_pdf_report(
331
- writer_output,
332
  charts=charts,
333
- table_data=table_data,
334
- metadata={"Technology Area": patent_area, "Stakeholder": stakeholder}
 
335
  )
 
 
336
  with open(pdf_path, "rb") as report_file:
337
- st.download_button("Download Report", data=report_file, file_name="Patent_Strategy_Report.pdf")
 
 
 
 
 
 
 
338
 
339
- except Exception as e:
340
- logging.error(f"An error occurred during execution: {e}")
341
- st.error(f"An error occurred during execution: {e}")
 
 
 
1
  import streamlit as st
2
  from crewai import Agent, Task, Crew
3
  import os
 
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')
 
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
 
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
 
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)
 
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(
 
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],
 
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}")