DrishtiSharma commited on
Commit
ca0f677
·
verified ·
1 Parent(s): 705c2ea

Update test.py

Browse files
Files changed (1) hide show
  1. test.py +329 -144
test.py CHANGED
@@ -10,6 +10,7 @@ import tempfile
10
  import time
11
  import ast
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,15 +30,13 @@ stakeholder = st.text_input("Enter Stakeholder", value="Patent Attorneys")
29
  llm = None
30
 
31
  # Model Selection
32
- model_choice = st.radio("Select LLM", ["GPT-4o", "llama-3.3-70b"], index=1, horizontal=True)
33
 
34
 
35
  # API Key Validation and LLM Initialization
36
  groq_api_key = os.getenv("GROQ_API_KEY")
37
  openai_api_key = os.getenv("OPENAI_API_KEY")
38
 
39
- #llm = ChatGroq(groq_api_key=os.getenv("GROQ_API_KEY"), model="groq/llama-3.3-70b-versatile")
40
-
41
  if model_choice == "llama-3.3-70b":
42
  if not groq_api_key:
43
  st.error("Groq API key is missing. Please set the GROQ_API_KEY environment variable.")
@@ -64,27 +63,148 @@ 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=(
76
- "Perform detailed statistical analysis of patent filings, growth trends, and innovation distribution. "
77
- "Identify top assignees/companies in the transparent antenna industry. "
78
- "Provide structured output in a list of dictionaries with 'Category' and 'Values' keys for clear data presentation."
 
 
 
 
 
 
 
 
79
  )
80
  )
81
  else:
82
- planner_goal = "Research trends in patent filings and technological innovation, identify key players, and provide strategic recommendations."
83
- writer_goal = "Craft a professional insights document summarizing trends, strategies, and actionable outcomes for stakeholders."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  analyst_goal = (
85
- "Perform detailed statistical analysis of patent filings, growth trends, and innovation distribution. "
86
- "Identify top assignees/companies in the transparent antenna industry. "
87
- "Provide structured output in a list of dictionaries with 'Category' and 'Values' keys for clear data presentation."
 
 
 
 
 
 
 
 
 
 
88
  )
89
 
90
  # Agent Definitions
@@ -124,53 +244,52 @@ analyst = Agent(
124
  # Task Definitions
125
  plan = Task(
126
  description=(
127
- "1. Research recent trends in {topic} patent filings and innovation.\n"
128
- "2. Identify key players and emerging technologies.\n"
129
- "3. Provide recommendations for stakeholders on strategic directions.\n"
130
- "4. Identify key statistics such as top regions, top players, and hot areas of innovation.\n"
131
- "5. Limit the output to 600 words."
 
132
  ),
133
- expected_output="A research document with structured insights, strategic recommendations, and key statistics.",
134
  agent=planner
135
  )
136
 
137
  write = Task(
138
  description=(
139
- "1. Use the Planner's and Analyst's outputs to craft a professional patent insights document.\n"
140
- "2. Include key findings, visual aids, and actionable strategies.\n"
141
- "3. Suggest strategic directions and highlight untapped innovation areas.\n"
142
- "4. Incorporate summarized tables for key statistics and example inventions.\n"
143
- "5. Limit the document to 600 words."
 
144
  ),
145
- expected_output="A polished, stakeholder-ready patent insights document with actionable recommendations.",
146
  agent=writer
147
  )
148
 
149
  analyse = Task(
150
  description=(
151
- "1. Conduct a comprehensive statistical analysis of patent filing trends, innovation hot spots, and future growth projections in the transparent antenna industry.\n"
152
  "2. Identify and rank the top regions, leading assignees/companies driving innovation.\n"
153
  "3. Highlight regional innovation trends and the distribution of emerging technologies across different geographies.\n"
154
  "4. Provide actionable insights and strategic recommendations based on the data.\n"
155
- "5. Deliver structured output in a list of dictionaries with 'Category' and 'Values' fields:\n"
156
- " - 'Values' can be:\n"
157
- " a) A dictionary with counts for quantitative data (e.g., {{'Region A': 120, 'Region B': 95}}),\n"
158
- " b) A list of key items (technologies, companies, inventors), or\n"
159
- " c) Descriptive text for qualitative insights.\n"
160
  "6. Example Output Format:\n"
161
  "[\n"
162
- " {{'Category': 'Top Regions', 'Values': {{'North America': 120, 'Europe': 95, 'Asia-Pacific': 85}}}},\n"
163
- " {{'Category': 'Top Assignees', 'Values': {{'Company A': 40, 'Company B': 35}}}},\n"
164
- " {{'Category': 'Emerging Technologies', 'Values': ['Graphene Antennas', '5G Integration']}},\n"
165
- " {{'Category': 'Strategic Insights', 'Values': 'Collaborations between automotive and material science industries are accelerating innovation.'}}\n"
166
  "]\n"
167
- "7. Ensure that the output is clean, well-structured, and formatted for use in visualizations and tables."
168
  ),
169
- expected_output="A structured, well-organized dataset with numeric, list-based, and descriptive insights for comprehensive visual and tabular reporting.",
170
  agent=analyst
171
  )
172
 
173
-
174
  crew = Crew(
175
  agents=[planner, analyst, writer],
176
  tasks=[plan, analyse, write],
@@ -178,58 +297,90 @@ crew = Crew(
178
  )
179
 
180
  # PDF Report Generation
181
- def generate_pdf_report(result, charts=None, table_data=None, metadata=None):
182
  with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf:
183
  pdf = FPDF()
184
  pdf.add_page()
185
- pdf.set_font("Arial", size=12)
 
 
 
 
 
186
  pdf.set_auto_page_break(auto=True, margin=15)
187
 
188
- pdf.set_font("Arial", size=16, style="B")
 
189
  pdf.cell(200, 10, txt="Patent Strategy and Innovation Report", ln=True, align="C")
190
  pdf.ln(10)
191
 
 
192
  if metadata:
193
- pdf.set_font("Arial", size=10)
194
  for key, value in metadata.items():
195
  pdf.cell(200, 10, txt=f"{key}: {value}", ln=True)
196
 
197
- pdf.set_font("Arial", size=12)
 
198
  pdf.multi_cell(0, 10, txt=result)
199
 
 
 
 
 
 
 
 
 
 
 
 
200
  if charts:
201
  for chart_path in charts:
202
  try:
203
  pdf.add_page()
204
  pdf.image(chart_path, x=10, y=20, w=180)
205
- logging.info(f"Successfully included chart: {chart_path}")
206
  except Exception as e:
207
- logging.error(f"Failed to include chart in PDF: {chart_path}. Error: {e}")
208
 
 
209
  if table_data:
210
  pdf.add_page()
211
- pdf.set_font("Arial", size=10)
212
- pdf.cell(200, 10, txt="Consolidated Table:", ln=True, align="L")
213
  for row in table_data:
214
  pdf.cell(200, 10, txt=str(row), ln=True)
215
 
216
  pdf.output(temp_pdf.name)
217
  return temp_pdf.name
218
 
 
219
  # Data Validation
220
  def validate_analyst_output(analyst_output):
221
  if not analyst_output:
222
  st.warning("No data available for analysis.")
 
223
  return None
224
- if not isinstance(analyst_output, list) or not all(isinstance(item, dict) for item in analyst_output):
 
225
  st.warning("Analyst output must be a list of dictionaries.")
 
226
  return None
 
227
  required_keys = {'Category', 'Values'}
228
- if not all(required_keys.issubset(item.keys()) for item in analyst_output):
229
- st.warning(f"Each dictionary must contain keys: {required_keys}")
230
- return None
 
 
 
 
 
 
 
231
  return analyst_output
232
 
 
233
  # Visualization and Table Display
234
  def create_visualizations(analyst_output):
235
  chart_paths = []
@@ -241,55 +392,55 @@ def create_visualizations(analyst_output):
241
  values = item["Values"]
242
 
243
  try:
244
- # Handle dictionary data
245
  if isinstance(values, dict):
246
  df = pd.DataFrame(list(values.items()), columns=["Label", "Count"])
247
-
248
- # Choose Pie Chart for fewer categories, else Bar Chart
249
  if len(df) <= 5:
250
  chart = px.pie(df, names="Label", values="Count", title=f"{category} Distribution")
251
  else:
252
  chart = px.bar(df, x="Label", y="Count", title=f"{category} Analysis")
253
 
254
- # Handle list data
255
  elif isinstance(values, list):
256
- # Convert the list into a frequency count without dummy values
257
- df = pd.DataFrame(values, columns=["Label"])
258
- df = df["Label"].value_counts().reset_index()
259
- df.columns = ["Label", "Count"]
260
-
261
- # Plot as a bar chart or pie chart
262
- if len(df) <= 5:
263
- chart = px.pie(df, names="Label", values="Count", title=f"{category} Distribution")
264
  else:
265
- chart = px.bar(df, x="Label", y="Count", title=f"{category} Frequency")
 
 
 
266
 
267
- # Handle text data
268
  elif isinstance(values, str):
269
  st.subheader(f"{category} Insights")
270
  st.table(pd.DataFrame({"Insights": [values]}))
271
- continue # No chart for text data
272
 
273
  else:
274
  st.warning(f"Unsupported data format for category: {category}")
 
275
  continue
276
 
277
- # Display the chart in Streamlit
278
  st.plotly_chart(chart)
279
 
280
- # Save the chart for PDF export
281
  with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_chart:
282
  chart.write_image(temp_chart.name)
283
  chart_paths.append(temp_chart.name)
284
 
285
  except Exception as e:
286
  st.error(f"Failed to generate visualization for {category}: {e}")
287
- logging.error(f"Error in {category} visualization: {e}")
288
 
289
  return chart_paths
290
 
291
-
292
-
293
  def display_table(analyst_output):
294
  table_data = []
295
  validated_data = validate_analyst_output(analyst_output)
@@ -299,7 +450,6 @@ def display_table(analyst_output):
299
  category = item["Category"]
300
  values = item["Values"]
301
 
302
- # Error handling to prevent crashes
303
  try:
304
  # Handle dictionary data (Table View)
305
  if isinstance(values, dict):
@@ -308,120 +458,155 @@ def display_table(analyst_output):
308
  st.dataframe(df)
309
  table_data.extend(df.to_dict(orient="records"))
310
 
311
- # Handle list data (List View)
312
  elif isinstance(values, list):
313
- df = pd.DataFrame(values, columns=["Items"])
314
- st.subheader(f"{category} (List View)")
315
- st.dataframe(df)
316
- table_data.extend(df.to_dict(orient="records"))
 
 
 
 
 
 
 
 
317
 
318
- # Handle text data (Summary View)
319
  elif isinstance(values, str):
320
  st.subheader(f"{category} (Summary)")
321
  st.table(pd.DataFrame({"Insights": [values]}))
322
  table_data.append({"Category": category, "Values": values})
323
 
324
  else:
325
- st.warning(f"Unsupported data format for category: {category}")
 
326
 
327
  except Exception as e:
328
- logging.error(f"Error processing {category}: {e}")
329
- st.error(f"Failed to display {category} as a table due to an error.")
330
 
331
  return table_data
332
 
333
 
334
-
335
  def parse_analyst_output(raw_output):
336
- structured_data = []
337
- current_category = None
338
- current_values = []
339
-
340
- # Split raw output by line
341
- lines = raw_output.split('\n')
342
-
343
- for line in lines:
344
- line = line.strip()
345
-
346
- # Detect the start of a new category
347
- if line.startswith("Category:"):
348
- # Save the previous category and its values
349
- if current_category and current_values:
350
- structured_data.append({
351
- "Category": current_category,
352
- "Values": current_values if len(current_values) > 1 else current_values[0]
353
- })
354
- # Start processing the new category
355
- current_category = line.replace("Category:", "").strip()
356
- current_values = []
357
-
358
- # Skip 'Values:' header
359
- elif line.startswith("Values:"):
360
- continue
361
-
362
- # Process the values under the current category
363
- elif line and current_category:
364
- try:
365
- # Attempt to convert the line into Python data (dict/list)
366
- parsed_value = ast.literal_eval(line)
367
- current_values.append(parsed_value)
368
- except (ValueError, SyntaxError):
369
- # If parsing fails, treat it as plain text
370
- current_values.append(line)
371
 
372
- # Save the last processed category
373
- if current_category and current_values:
374
- structured_data.append({
375
- "Category": current_category,
376
- "Values": current_values if len(current_values) > 1 else current_values[0]
377
- })
378
 
379
- return structured_data
380
 
381
 
382
  # Main Execution Block
383
  if st.button("Generate Patent Insights"):
384
  with st.spinner('Processing...'):
385
  try:
 
386
  start_time = time.time()
387
- results = crew.kickoff(inputs={"topic": patent_area, "stakeholder": stakeholder})
 
 
 
 
 
 
 
 
388
  elapsed_time = time.time() - start_time
389
 
 
390
  writer_output = getattr(results.tasks_output[2], "raw", "No details available.")
391
- if writer_output:
392
  st.markdown("### Final Report")
393
  st.write(writer_output)
394
  else:
395
  st.warning("No final report available.")
396
 
 
397
  with st.expander("Explore Detailed Insights"):
398
  tab1, tab2 = st.tabs(["Planner's Insights", "Analyst's Analysis"])
399
 
 
400
  with tab1:
401
  planner_output = getattr(results.tasks_output[0], "raw", "No details available.")
402
- st.write(planner_output)
 
 
 
403
 
 
404
  with tab2:
405
  analyst_output = getattr(results.tasks_output[1], "raw", "No details available.")
406
- st.write(analyst_output)
407
- # Convert raw text to structured data
408
- if isinstance(analyst_output, str):
409
- analyst_output = parse_analyst_output(analyst_output)
410
- st.subheader("Structured Analyst Output")
411
- st.write(analyst_output)
412
-
413
 
414
- charts = []
415
- if enable_advanced_analysis:
416
- charts = create_visualizations(analyst_output)
 
417
 
418
- table_data = display_table(analyst_output)
 
 
 
 
 
419
 
 
 
 
 
 
 
 
420
  st.success(f"Analysis completed in {elapsed_time:.2f} seconds.")
421
- pdf_path = generate_pdf_report(writer_output, charts=charts, table_data=table_data, metadata={"Technology Area": patent_area, "Stakeholder": stakeholder})
422
- with open(pdf_path, "rb") as report_file:
423
- st.download_button("Download Report", data=report_file, file_name="Patent_Strategy_Report.pdf")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
424
 
425
  except Exception as e:
426
- logging.error(f"An error occurred during execution: {e}")
427
- st.error(f"An error occurred during execution: {e}")
 
 
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.")
 
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
 
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 = []
 
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)
 
450
  category = item["Category"]
451
  values = item["Values"]
452
 
 
453
  try:
454
  # Handle dictionary data (Table View)
455
  if isinstance(values, dict):
 
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}")