Spaces:
Sleeping
Sleeping
DrishtiSharma
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -381,7 +381,7 @@ def validate_analyst_output(analyst_output):
|
|
381 |
return analyst_output
|
382 |
|
383 |
|
384 |
-
# Visualization
|
385 |
def create_visualizations(analyst_output):
|
386 |
chart_paths = []
|
387 |
validated_data = validate_analyst_output(analyst_output)
|
@@ -392,7 +392,7 @@ def create_visualizations(analyst_output):
|
|
392 |
values = item["Values"]
|
393 |
|
394 |
try:
|
395 |
-
# Handle dictionary data for bar
|
396 |
if isinstance(values, dict):
|
397 |
df = pd.DataFrame(list(values.items()), columns=["Label", "Count"])
|
398 |
if len(df) <= 5:
|
@@ -402,27 +402,21 @@ def create_visualizations(analyst_output):
|
|
402 |
|
403 |
# Handle list data for bar/pie charts
|
404 |
elif isinstance(values, list):
|
405 |
-
#
|
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 |
-
#
|
411 |
-
|
412 |
-
|
413 |
-
chart = px.bar(df, x=df.index, y=col, title=f"{category} - {col} Analysis")
|
414 |
-
st.plotly_chart(chart)
|
415 |
-
break
|
416 |
-
continue
|
417 |
-
|
418 |
-
# Handle simple lists
|
419 |
else:
|
420 |
df = pd.DataFrame(values, columns=["Items"])
|
421 |
df = df["Items"].value_counts().reset_index()
|
422 |
df.columns = ["Label", "Count"]
|
423 |
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")
|
424 |
|
425 |
-
# Handle
|
426 |
elif isinstance(values, str):
|
427 |
st.subheader(f"{category} Insights")
|
428 |
st.table(pd.DataFrame({"Insights": [values]}))
|
@@ -433,10 +427,10 @@ def create_visualizations(analyst_output):
|
|
433 |
logging.warning(f"Unsupported data format in {category}: {values}")
|
434 |
continue
|
435 |
|
436 |
-
# Display
|
437 |
st.plotly_chart(chart)
|
438 |
|
439 |
-
# Save
|
440 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_chart:
|
441 |
chart.write_image(temp_chart.name)
|
442 |
chart_paths.append(temp_chart.name)
|
@@ -447,7 +441,6 @@ def create_visualizations(analyst_output):
|
|
447 |
|
448 |
return chart_paths
|
449 |
|
450 |
-
|
451 |
def display_table(analyst_output):
|
452 |
table_data = []
|
453 |
validated_data = validate_analyst_output(analyst_output)
|
@@ -458,35 +451,34 @@ def display_table(analyst_output):
|
|
458 |
values = item["Values"]
|
459 |
|
460 |
try:
|
461 |
-
# Handle dictionary data
|
462 |
if isinstance(values, dict):
|
463 |
df = pd.DataFrame(list(values.items()), columns=["Label", "Count"])
|
464 |
st.subheader(f"{category} (Table View)")
|
465 |
st.dataframe(df)
|
466 |
table_data.extend(df.to_dict(orient="records"))
|
467 |
|
468 |
-
# Handle list data
|
469 |
elif isinstance(values, list):
|
|
|
470 |
if all(isinstance(v, dict) for v in values):
|
471 |
-
# Detailed View for list of dictionaries
|
472 |
df = pd.DataFrame(values)
|
473 |
st.subheader(f"{category} (Detailed View)")
|
474 |
st.dataframe(df)
|
475 |
table_data.extend(df.to_dict(orient="records"))
|
|
|
476 |
else:
|
477 |
-
# Simple List View
|
478 |
df = pd.DataFrame(values, columns=["Items"])
|
479 |
st.subheader(f"{category} (List View)")
|
480 |
st.dataframe(df)
|
481 |
table_data.extend(df.to_dict(orient="records"))
|
482 |
|
483 |
-
# Handle
|
484 |
elif isinstance(values, str):
|
485 |
st.subheader(f"{category} (Summary)")
|
486 |
st.table(pd.DataFrame({"Insights": [values]}))
|
487 |
table_data.append({"Category": category, "Values": values})
|
488 |
|
489 |
-
# Handle unsupported data types
|
490 |
else:
|
491 |
st.warning(f"Unsupported data format for {category}")
|
492 |
logging.warning(f"Unsupported data in {category}: {values}")
|
@@ -497,37 +489,31 @@ def display_table(analyst_output):
|
|
497 |
|
498 |
return table_data
|
499 |
|
|
|
500 |
def parse_analyst_output(raw_output):
|
501 |
key_insights = []
|
502 |
data_insights = []
|
503 |
|
504 |
try:
|
505 |
-
#
|
506 |
structured_data = ast.literal_eval(raw_output) if isinstance(raw_output, str) else raw_output
|
507 |
|
508 |
for item in structured_data:
|
509 |
if "Category" not in item or "Values" not in item:
|
510 |
logging.warning(f"Missing 'Category' or 'Values' in item: {item}")
|
511 |
-
continue
|
512 |
|
513 |
if item.get("Type") == "Key Insight":
|
514 |
key_insights.append(item["Values"])
|
515 |
-
|
516 |
elif item.get("Type") == "Data Insight":
|
517 |
-
#
|
518 |
if isinstance(item["Values"], list):
|
519 |
for sub_item in item["Values"]:
|
520 |
-
|
521 |
-
data_insights.append({"Category": item["Category"], "Values": sub_item})
|
522 |
-
else:
|
523 |
-
data_insights.append({"Category": item["Category"], "Values": sub_item})
|
524 |
-
elif isinstance(item["Values"], dict):
|
525 |
-
data_insights.append(item)
|
526 |
else:
|
527 |
data_insights.append(item)
|
528 |
-
|
529 |
else:
|
530 |
-
|
531 |
|
532 |
except Exception as e:
|
533 |
logging.error(f"Error parsing analyst output: {e}")
|
@@ -536,40 +522,13 @@ def parse_analyst_output(raw_output):
|
|
536 |
|
537 |
|
538 |
# Main Execution Block
|
539 |
-
# Initialize placeholders for outputs to ensure tabs are always visible
|
540 |
-
planner_output = "Planner insights will appear here after generating insights."
|
541 |
-
analyst_output = "Analyst data will appear here after generating insights."
|
542 |
-
writer_output = "Final report will appear here after generating insights."
|
543 |
-
charts = []
|
544 |
-
table_data = []
|
545 |
-
key_insights, data_insights = [], []
|
546 |
-
|
547 |
-
# Create tabs at the start so they are always visible
|
548 |
-
tab1, tab2, tab3 = st.tabs(["📄 Final Report", "📝 Planner's Insights", "📊 Analyst's Analysis"])
|
549 |
-
|
550 |
-
# Final Report Tab (Initial State)
|
551 |
-
with tab1:
|
552 |
-
st.header("Final Patent Strategy Report")
|
553 |
-
st.info(writer_output)
|
554 |
-
|
555 |
-
# Planner's Insights Tab (Initial State)
|
556 |
-
with tab2:
|
557 |
-
st.header("Planner's Research Insights")
|
558 |
-
st.info(planner_output)
|
559 |
-
|
560 |
-
# Analyst's Analysis Tab (Initial State)
|
561 |
-
with tab3:
|
562 |
-
st.header("Analyst's Data Analysis")
|
563 |
-
st.info(analyst_output)
|
564 |
-
|
565 |
-
# Button to Generate Insights
|
566 |
if st.button("Generate Patent Insights"):
|
567 |
with st.spinner('Processing...'):
|
568 |
try:
|
569 |
# Start the timer
|
570 |
start_time = time.time()
|
571 |
|
572 |
-
#
|
573 |
if not patent_area or not stakeholder:
|
574 |
st.error("Please provide both Patent Technology Area and Stakeholder.")
|
575 |
else:
|
@@ -579,68 +538,49 @@ if st.button("Generate Patent Insights"):
|
|
579 |
# Calculate elapsed time
|
580 |
elapsed_time = time.time() - start_time
|
581 |
|
582 |
-
# Extract
|
583 |
-
planner_output = getattr(results.tasks_output[0], "raw", "No details available.")
|
584 |
-
analyst_output = getattr(results.tasks_output[1], "raw", "No details available.")
|
585 |
writer_output = getattr(results.tasks_output[2], "raw", "No details available.")
|
|
|
|
|
|
|
|
|
|
|
586 |
|
587 |
-
#
|
588 |
-
|
589 |
-
|
590 |
-
|
591 |
-
# Create Visualizations if enabled
|
592 |
-
if enable_advanced_analysis and data_insights:
|
593 |
-
charts = create_visualizations(data_insights)
|
594 |
-
else:
|
595 |
-
st.info("No data insights available for visualizations.")
|
596 |
-
|
597 |
-
# Display Data Tables
|
598 |
-
table_data = display_table(data_insights)
|
599 |
-
|
600 |
-
# Update Tabs with Results
|
601 |
-
|
602 |
-
# Final Report Tab
|
603 |
-
with tab1:
|
604 |
-
st.header("Final Patent Strategy Report")
|
605 |
-
if writer_output and writer_output.strip():
|
606 |
-
st.write(writer_output)
|
607 |
-
else:
|
608 |
-
st.warning("No final report available.")
|
609 |
|
610 |
-
|
611 |
-
|
612 |
-
|
613 |
-
|
614 |
-
|
615 |
-
else:
|
616 |
-
st.warning("No planner insights available.")
|
617 |
-
|
618 |
-
# Analyst's Analysis Tab
|
619 |
-
with tab3:
|
620 |
-
st.header("Analyst's Data Analysis")
|
621 |
-
if analyst_output and analyst_output.strip():
|
622 |
-
st.write(analyst_output)
|
623 |
-
|
624 |
-
# Structured Analyst Output
|
625 |
-
st.subheader("Structured Analyst Output")
|
626 |
-
st.write(data_insights)
|
627 |
-
|
628 |
-
# Visualizations Section
|
629 |
-
if charts:
|
630 |
-
st.subheader("Generated Visualizations")
|
631 |
-
for chart_path in charts:
|
632 |
-
st.image(chart_path, use_column_width=True)
|
633 |
else:
|
634 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
635 |
|
636 |
-
# Data Tables Section
|
637 |
-
if table_data:
|
638 |
-
st.subheader("Detailed Data Tables")
|
639 |
-
st.write(table_data)
|
640 |
else:
|
641 |
-
st.
|
642 |
-
else:
|
643 |
-
st.warning("No analyst analysis available.")
|
644 |
|
645 |
# Notify user that the analysis is complete
|
646 |
st.success(f"Analysis completed in {elapsed_time:.2f} seconds.")
|
@@ -669,5 +609,4 @@ if st.button("Generate Patent Insights"):
|
|
669 |
except Exception as e:
|
670 |
error_message = traceback.format_exc()
|
671 |
logging.error(f"An error occurred during execution:\n{error_message}")
|
672 |
-
st.error(f"⚠️ An unexpected error occurred:\n{e}")
|
673 |
-
|
|
|
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)
|
|
|
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:
|
|
|
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]}))
|
|
|
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)
|
|
|
441 |
|
442 |
return chart_paths
|
443 |
|
|
|
444 |
def display_table(analyst_output):
|
445 |
table_data = []
|
446 |
validated_data = validate_analyst_output(analyst_output)
|
|
|
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}")
|
|
|
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}")
|
|
|
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:
|
|
|
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.")
|
|
|
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}")
|
|