DrishtiSharma commited on
Commit
73cb56f
·
verified ·
1 Parent(s): f020a05

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -54
app.py CHANGED
@@ -385,63 +385,36 @@ def display_table(analyst_output):
385
 
386
  return table_data
387
 
388
-
389
-
390
  def parse_analyst_output(raw_output):
391
- structured_data = []
392
- current_category = None
393
- current_values = []
394
-
395
- # Split raw output by line
396
- lines = raw_output.split('\n')
397
-
398
- for line in lines:
399
- line = line.strip()
400
-
401
- # Detect the start of a new category
402
- if line.startswith("Category:"):
403
- # Save the previous category and its values
404
- if current_category and current_values:
405
- structured_data.append({
406
- "Category": current_category,
407
- "Values": current_values if len(current_values) > 1 else current_values[0]
408
- })
409
- # Start processing the new category
410
- current_category = line.replace("Category:", "").strip()
411
- current_values = []
412
-
413
- # Skip 'Values:' header
414
- elif line.startswith("Values:"):
415
- continue
416
-
417
- # Process the values under the current category
418
- elif line and current_category:
419
- try:
420
- # Attempt to convert the line into Python data (dict/list)
421
- parsed_value = ast.literal_eval(line)
422
- current_values.append(parsed_value)
423
- except (ValueError, SyntaxError):
424
- # If parsing fails, treat it as plain text
425
- current_values.append(line)
426
 
427
- # Save the last processed category
428
- if current_category and current_values:
429
- structured_data.append({
430
- "Category": current_category,
431
- "Values": current_values if len(current_values) > 1 else current_values[0]
432
- })
433
 
434
- return structured_data
 
 
 
 
 
 
435
 
 
436
 
437
  # Main Execution Block
438
  if st.button("Generate Patent Insights"):
439
  with st.spinner('Processing...'):
440
  try:
 
441
  start_time = time.time()
 
 
442
  results = crew.kickoff(inputs={"topic": patent_area, "stakeholder": stakeholder})
 
 
443
  elapsed_time = time.time() - start_time
444
 
 
445
  writer_output = getattr(results.tasks_output[2], "raw", "No details available.")
446
  if writer_output:
447
  st.markdown("### Final Report")
@@ -449,34 +422,55 @@ if st.button("Generate Patent Insights"):
449
  else:
450
  st.warning("No final report available.")
451
 
 
452
  with st.expander("Explore Detailed Insights"):
453
  tab1, tab2 = st.tabs(["Planner's Insights", "Analyst's Analysis"])
454
 
 
455
  with tab1:
456
  planner_output = getattr(results.tasks_output[0], "raw", "No details available.")
457
  st.write(planner_output)
458
 
 
459
  with tab2:
460
  analyst_output = getattr(results.tasks_output[1], "raw", "No details available.")
461
  st.write(analyst_output)
462
- # Convert raw text to structured data
463
- if isinstance(analyst_output, str):
464
- analyst_output = parse_analyst_output(analyst_output)
465
  st.subheader("Structured Analyst Output")
466
- st.write(analyst_output)
467
-
468
 
 
469
  charts = []
470
  if enable_advanced_analysis:
471
- charts = create_visualizations(analyst_output)
472
 
473
- table_data = display_table(analyst_output)
 
474
 
 
475
  st.success(f"Analysis completed in {elapsed_time:.2f} seconds.")
476
- pdf_path = generate_pdf_report(writer_output, charts=charts, table_data=table_data, metadata={"Technology Area": patent_area, "Stakeholder": stakeholder})
 
 
 
 
 
 
 
 
 
 
477
  with open(pdf_path, "rb") as report_file:
478
- st.download_button("Download Report", data=report_file, file_name="Patent_Strategy_Report.pdf")
 
 
 
 
 
479
 
480
  except Exception as e:
 
481
  logging.error(f"An error occurred during execution: {e}")
482
- st.error(f"An error occurred during execution: {e}")
 
385
 
386
  return table_data
387
 
 
 
388
  def parse_analyst_output(raw_output):
389
+ key_insights = []
390
+ data_insights = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
391
 
392
+ structured_data = ast.literal_eval(raw_output) if isinstance(raw_output, str) else raw_output
 
 
 
 
 
393
 
394
+ for item in structured_data:
395
+ if item.get("Type") == "Key Insight":
396
+ key_insights.append(item["Values"])
397
+ elif item.get("Type") == "Data Insight":
398
+ data_insights.append(item)
399
+ else:
400
+ data_insights.append(item)
401
 
402
+ return key_insights, data_insights
403
 
404
  # Main Execution Block
405
  if st.button("Generate Patent Insights"):
406
  with st.spinner('Processing...'):
407
  try:
408
+ # Start the timer
409
  start_time = time.time()
410
+
411
+ # Kick off the crew with user inputs
412
  results = crew.kickoff(inputs={"topic": patent_area, "stakeholder": stakeholder})
413
+
414
+ # Calculate elapsed time
415
  elapsed_time = time.time() - start_time
416
 
417
+ # Extract Writer's Output
418
  writer_output = getattr(results.tasks_output[2], "raw", "No details available.")
419
  if writer_output:
420
  st.markdown("### Final Report")
 
422
  else:
423
  st.warning("No final report available.")
424
 
425
+ # Expandable section for detailed insights
426
  with st.expander("Explore Detailed Insights"):
427
  tab1, tab2 = st.tabs(["Planner's Insights", "Analyst's Analysis"])
428
 
429
+ # Planner's Insights
430
  with tab1:
431
  planner_output = getattr(results.tasks_output[0], "raw", "No details available.")
432
  st.write(planner_output)
433
 
434
+ # Analyst's Analysis
435
  with tab2:
436
  analyst_output = getattr(results.tasks_output[1], "raw", "No details available.")
437
  st.write(analyst_output)
438
+
439
+ # Convert raw analyst output into structured key/data insights
440
+ key_insights, data_insights = parse_analyst_output(analyst_output)
441
  st.subheader("Structured Analyst Output")
442
+ st.write(data_insights)
 
443
 
444
+ # Create Visualizations if enabled
445
  charts = []
446
  if enable_advanced_analysis:
447
+ charts = create_visualizations(data_insights)
448
 
449
+ # Display Data Tables
450
+ table_data = display_table(data_insights)
451
 
452
+ # Notify that analysis is complete
453
  st.success(f"Analysis completed in {elapsed_time:.2f} seconds.")
454
+
455
+ # Generate the PDF report with Key Insights and Data Insights
456
+ pdf_path = generate_pdf_report(
457
+ result=writer_output,
458
+ charts=charts,
459
+ table_data=data_insights,
460
+ metadata={"Technology Area": patent_area, "Stakeholder": stakeholder},
461
+ key_insights=key_insights # 🔑 Pass key insights to the PDF
462
+ )
463
+
464
+ # Download button for the generated PDF
465
  with open(pdf_path, "rb") as report_file:
466
+ st.download_button(
467
+ label="Download Report",
468
+ data=report_file,
469
+ file_name="Patent_Strategy_Report.pdf",
470
+ mime="application/pdf"
471
+ )
472
 
473
  except Exception as e:
474
+ # Handle errors
475
  logging.error(f"An error occurred during execution: {e}")
476
+ st.error(f"An error occurred during execution: {e}")