Spaces:
Running
Running
| """ | |
| Dashboard Narrator - Powered by sandsiv+ | |
| A tool to analyze dashboard PDFs and generate comprehensive reports. | |
| """ | |
| # Import required libraries | |
| import os | |
| import time | |
| import threading | |
| import io | |
| import base64 | |
| from PyPDF2 import PdfReader | |
| import anthropic | |
| from PIL import Image | |
| import markdown | |
| from weasyprint import HTML, CSS | |
| from weasyprint.text.fonts import FontConfiguration | |
| from pdf2image import convert_from_bytes | |
| import gradio as gr | |
| # Create a global progress tracker | |
| class ProgressTracker: | |
| def __init__(self): | |
| self.progress = 0 | |
| self.message = "Ready" | |
| self.is_processing = False | |
| self.lock = threading.Lock() | |
| def update(self, progress, message="Processing..."): | |
| with self.lock: | |
| self.progress = progress | |
| self.message = message | |
| def get_status(self): | |
| with self.lock: | |
| return f"{self.message} ({self.progress:.1f}%)" | |
| def start_processing(self): | |
| with self.lock: | |
| self.is_processing = True | |
| self.progress = 0 | |
| self.message = "Starting..." | |
| def end_processing(self): | |
| with self.lock: | |
| self.is_processing = False | |
| self.progress = 100 | |
| self.message = "Complete" | |
| # Create a global instance | |
| progress_tracker = ProgressTracker() | |
| output_status = None | |
| # Function to update the Gradio interface with progress | |
| def update_progress(): | |
| global output_status | |
| while progress_tracker.is_processing: | |
| status = progress_tracker.get_status() | |
| if output_status is not None: | |
| output_status.update(value=status) | |
| time.sleep(0.5) | |
| return | |
| # Model configuration | |
| AVAILABLE_MODELS = { | |
| "Claude Sonnet 4.5": "claude-sonnet-4-5-20250929", | |
| "Claude Opus 4.1": "claude-opus-4-1-20250514" | |
| } | |
| # Supported languages configuration | |
| SUPPORTED_LANGUAGES = { | |
| "italiano": { | |
| "code": "it", | |
| "name": "Italiano", | |
| "report_title": "Analisi Dashboard", | |
| "report_subtitle": "Report Dettagliato", | |
| "date_label": "Data", | |
| "system_prompt": "Sei un esperto analista di business intelligence specializzato nell'interpretazione di dashboard e dati visualizzati. Fornisci analisi in italiano approfondite e insight actionable basati sui dati forniti.", | |
| "section_title": "ANALISI SEZIONE", | |
| "multi_doc_title": "ANALISI DASHBOARD {index}" | |
| }, | |
| "english": { | |
| "code": "en", | |
| "name": "English", | |
| "report_title": "Dashboard Analysis", | |
| "report_subtitle": "Detailed Report", | |
| "date_label": "Date", | |
| "system_prompt": "You are an expert business intelligence analyst specialized in interpreting dashboards and data visualizations. Provide in-depth analysis and actionable insights based on the data provided.", | |
| "section_title": "SECTION ANALYSIS", | |
| "multi_doc_title": "DASHBOARD {index} ANALYSIS" | |
| }, | |
| "français": { | |
| "code": "fr", | |
| "name": "Français", | |
| "report_title": "Analyse de Tableau de Bord", | |
| "report_subtitle": "Rapport Détaillé", | |
| "date_label": "Date", | |
| "system_prompt": "Vous êtes un analyste expert en business intelligence spécialisé dans l'interprétation des tableaux de bord et des visualisations de données. Fournissez en français une analyse approfondie et des insights actionnables basés sur les données fournies.", | |
| "section_title": "ANALYSE DE SECTION", | |
| "multi_doc_title": "ANALYSE DU TABLEAU DE BORD {index}" | |
| }, | |
| "español": { | |
| "code": "es", | |
| "name": "Español", | |
| "report_title": "Análisis de Dashboard", | |
| "report_subtitle": "Informe Detallado", | |
| "date_label": "Fecha", | |
| "system_prompt": "Eres un analista experto en inteligencia empresarial especializado en interpretar dashboards y visualizaciones de datos. Proporciona en español un análisis en profundidad e insights accionables basados en los datos proporcionados.", | |
| "section_title": "ANÁLISIS DE SECCIÓN", | |
| "multi_doc_title": "ANÁLISIS DEL DASHBOARD {index}" | |
| }, | |
| "deutsch": { | |
| "code": "de", | |
| "name": "Deutsch", | |
| "report_title": "Dashboard-Analyse", | |
| "report_subtitle": "Detaillierter Bericht", | |
| "date_label": "Datum", | |
| "system_prompt": "Sie sind ein Experte für Business Intelligence-Analyse, der auf die Interpretation von Dashboards und Datenvisualisierungen spezialisiert ist. Bieten Sie auf Deutsch eine eingehende Analyse und umsetzbare Erkenntnisse auf Grundlage der bereitgestellten Daten.", | |
| "section_title": "ABSCHNITTSANALYSE", | |
| "multi_doc_title": "DASHBOARD-ANALYSE {index}" | |
| } | |
| } | |
| # Utility Functions | |
| def extract_text_from_pdf(pdf_bytes): | |
| """Extract text from a PDF file.""" | |
| try: | |
| pdf_reader = PdfReader(io.BytesIO(pdf_bytes)) | |
| text = "" | |
| for page_num in range(len(pdf_reader.pages)): | |
| extracted = pdf_reader.pages[page_num].extract_text() | |
| if extracted: | |
| text += extracted + "\n" | |
| return text | |
| except Exception as e: | |
| print(f"Error extracting text from PDF: {str(e)}") | |
| return "" | |
| def divide_image_vertically(image, num_sections): | |
| """Divide an image vertically into sections.""" | |
| width, height = image.size | |
| section_height = height // num_sections | |
| sections = [] | |
| for i in range(num_sections): | |
| top = i * section_height | |
| bottom = height if i == num_sections - 1 else (i + 1) * section_height | |
| section = image.crop((0, top, width, bottom)) | |
| sections.append(section) | |
| print(f"Section {i+1}: size {section.width}x{section.height} pixels") | |
| return sections | |
| def encode_image_with_resize(image, max_size_mb=4.5): | |
| """Encode an image in base64, resizing if necessary.""" | |
| max_bytes = max_size_mb * 1024 * 1024 | |
| img_byte_arr = io.BytesIO() | |
| image.save(img_byte_arr, format='PNG') | |
| current_size = len(img_byte_arr.getvalue()) | |
| if current_size > max_bytes: | |
| scale_factor = (max_bytes / current_size) ** 0.5 | |
| new_width = int(image.width * scale_factor) | |
| new_height = int(image.height * scale_factor) | |
| resized_image = image.resize((new_width, new_height), Image.LANCZOS) | |
| img_byte_arr = io.BytesIO() | |
| resized_image.save(img_byte_arr, format='PNG', optimize=True) | |
| print(f"Image resized from {current_size/1024/1024:.2f}MB to {len(img_byte_arr.getvalue())/1024/1024:.2f}MB") | |
| image = resized_image | |
| else: | |
| print(f"Image size acceptable: {current_size/1024/1024:.2f}MB") | |
| buffer = io.BytesIO() | |
| image.save(buffer, format="PNG", optimize=True) | |
| return base64.b64encode(buffer.getvalue()).decode("utf-8") | |
| # Core Analysis Functions | |
| def analyze_dashboard_section(client, section_number, total_sections, image_section, full_text, language, model_name, goal_description=None): | |
| """Analyze a vertical section of the dashboard in the specified language.""" | |
| print(f"Analyzing section {section_number}/{total_sections} in {language['name']} using {model_name}...") | |
| try: | |
| encoded_image = encode_image_with_resize(image_section) | |
| except Exception as e: | |
| print(f"Error encoding section {section_number}: {str(e)}") | |
| return f"Error analyzing section {section_number}: {str(e)}" | |
| messages = [{"role": "user", "content": []}] | |
| section_prompt = f""" | |
| Act as a senior data analyst examining this dashboard section for Customer Experience purpose.\n | |
| Your analysis will be shared with top executives to inform about Customer Experience improvements and customer satisfaction level.\n | |
| # Dashboard Analysis - Section {section_number} of {total_sections}\n | |
| You are analyzing section {section_number} of {total_sections} of a long vertical dashboard. This is part of a broader analysis.\n | |
| {f"The analysis objective is: {goal_description}" if goal_description else ""}\n\n | |
| For this specific section:\n | |
| 1. Describe what these visualizations show, including their type (e.g., bar chart, line graph) and the data they represent\n | |
| 2. Quantitatively analyze the data, noting specific values, percentages, and numeric trends\n | |
| 3. Identify significant patterns, anomalies, or outliers visible in the data\n | |
| 4. Provide 2-3 actionable insights based on this analysis, explaining their business implications\n | |
| 5. Suggest possible reasons for any notable trends or unexpected findings\n | |
| Focus exclusively on the visible section. Don't reference or speculate about unseen dashboard elements.\n | |
| Answer completely in {language['name']}.\n\n | |
| # Text extracted from the complete dashboard:\n | |
| {full_text[:10000]} | |
| """ | |
| messages[0]["content"].append({"type": "text", "text": section_prompt}) | |
| messages[0]["content"].append({ | |
| "type": "image", | |
| "source": {"type": "base64", "media_type": "image/png", "data": encoded_image} | |
| }) | |
| try: | |
| response = client.messages.create( | |
| model=model_name, | |
| max_tokens=10000, | |
| temperature=0.1, | |
| system=language['system_prompt'], | |
| messages=messages | |
| ) | |
| return response.content[0].text | |
| except Exception as e: | |
| print(f"Error analyzing section {section_number}: {str(e)}") | |
| return f"Error analyzing section {section_number}: {str(e)}" | |
| def create_comprehensive_report(client, section_analyses, full_text, language, model_name, goal_description=None): | |
| """Create a unified comprehensive report based on individual section analyses.""" | |
| print(f"Generating final comprehensive report in {language['name']} using {model_name}...") | |
| comprehensive_prompt = f""" | |
| # Comprehensive Dashboard Analysis Request | |
| You have analyzed a long vertical dashboard in multiple sections. Now you need to create a unified and coherent report based on all the partial analyses.\n | |
| {f"The analysis objective is: {goal_description}" if goal_description else ""}\n\n | |
| Here are the analyses of the individual dashboard sections:\n | |
| {section_analyses}\n\n | |
| Based on these partial analyses, generate a professional, structured, and coherent report that includes:\n | |
| 1. Executive Summary - Include key metrics, major findings, and critical recommendations (limit to 1 page equivalent)\n | |
| 2. Dashboard Performance Overview - Add a section that evaluates the overall health metrics before diving into categories\n | |
| 3 Detailed Analysis by Category - Keep this, it's essential\n | |
| 4 Trend Analysis - Broaden from just temporal to include cross-category patterns\n | |
| 5 Critical Issues and Opportunities - Combine anomalies with positive outliers to provide balanced insights\n | |
| 6 Strategic Implications and Recommendations - Consolidate your insights and recommendations into a single, stronger section\n | |
| 7 Implementation Roadmap - Convert your conclusions into a prioritized action plan with timeframes\n | |
| 8 Appendix: Monitoring Improvements - Move the monitoring suggestions to an appendix unless they're a primary focus\n\n | |
| Integrate information from all sections to create a coherent and complete report.\n\n | |
| # Text extracted from the complete dashboard:\n | |
| {full_text[:10000]} | |
| """ | |
| try: | |
| response = client.messages.create( | |
| model=model_name, | |
| max_tokens=10000, | |
| temperature=0.1, | |
| system=language['system_prompt'], | |
| messages=[{"role": "user", "content": comprehensive_prompt}] | |
| ) | |
| return response.content[0].text | |
| except Exception as e: | |
| print(f"Error creating comprehensive report: {str(e)}") | |
| return f"Error creating comprehensive report: {str(e)}" | |
| def create_multi_dashboard_comparative_report(client, individual_reports, language, model_name, goal_description=None): | |
| """Create a comparative report analyzing multiple dashboards together.""" | |
| print(f"Generating comparative report for multiple dashboards in {language['name']} using {model_name}...") | |
| comparative_prompt = f""" | |
| # Multi-Dashboard Comparative Analysis Request | |
| You have analyzed multiple dashboards individually. Now you need to create a comparative analysis report that identifies patterns, similarities, differences, and insights across all dashboards. | |
| {f"The analysis objective is: {goal_description}" if goal_description else ""} | |
| Here are the analyses of the individual dashboards: | |
| {individual_reports} | |
| Based on these individual analyses, generate a professional, structured comparative report that includes: | |
| 1. Executive Overview of All Dashboards | |
| 2. Comparative Analysis of Key Metrics | |
| 3. Cross-Dashboard Patterns and Trends | |
| 4. Notable Differences Between Dashboards | |
| 5. Integrated Insights from All Sources | |
| 6. Comprehensive Strategic Recommendations | |
| 7. Suggestions for Cross-Dashboard Monitoring Improvements | |
| 8. Conclusions and Integrated Next Steps | |
| Integrate information from all dashboards to create a coherent comparative report. | |
| """ | |
| try: | |
| response = client.messages.create( | |
| model=model_name, | |
| max_tokens=12000, | |
| temperature=0.1, | |
| system=language['system_prompt'], | |
| messages=[{"role": "user", "content": comparative_prompt}] | |
| ) | |
| return response.content[0].text | |
| except Exception as e: | |
| print(f"Error creating comparative report: {str(e)}") | |
| return f"Error creating comparative report: {str(e)}" | |
| def markdown_to_pdf(markdown_content, output_filename, language): | |
| """Convert Markdown content to a well-formatted PDF.""" | |
| print(f"Converting Markdown report to PDF in {language['name']}...") | |
| css = CSS(string=''' | |
| @page { margin: 1.5cm; } | |
| body { font-family: Arial, sans-serif; line-height: 1.5; font-size: 11pt; } | |
| h1 { color: #2c3e50; font-size: 22pt; margin-top: 1cm; margin-bottom: 0.5cm; page-break-after: avoid; } | |
| h2 { color: #3498db; font-size: 16pt; margin-top: 0.8cm; margin-bottom: 0.3cm; page-break-after: avoid; } | |
| p { margin-bottom: 0.3cm; text-align: justify; } | |
| ''') | |
| today = time.strftime("%d/%m/%Y") | |
| cover_page = f""" | |
| <div style="text-align: center; height: 100vh; display: flex; flex-direction: column; justify-content: center; align-items: center;"> | |
| <h1 style="font-size: 26pt; color: #2c3e50;">{language['report_title']}</h1> | |
| <h2 style="font-size: 14pt; color: #7f8c8d;">{language['report_subtitle']}</h2> | |
| <p style="font-size: 12pt; color: #7f8c8d;">{language['date_label']}: {today}</p> | |
| </div> | |
| <div style="page-break-after: always;"></div> | |
| """ | |
| html_content = markdown.markdown(markdown_content, extensions=['tables', 'fenced_code']) | |
| full_html = cover_page + html_content | |
| font_config = FontConfiguration() | |
| HTML(string=full_html).write_pdf(output_filename, stylesheets=[css], font_config=font_config) | |
| print(f"PDF report created: {output_filename}") | |
| return output_filename | |
| # Main Processing Functions | |
| def process_single_dashboard(api_key, pdf_bytes, language_code, model_name, goal_description=None, num_sections=4, dashboard_index=1): | |
| """Process a single dashboard PDF and return the markdown report and analysis list.""" | |
| print("\n" + "="*60) | |
| print(f"Starting analysis for dashboard {dashboard_index} in {SUPPORTED_LANGUAGES[language_code]['name']}...") | |
| print("="*60 + "\n") | |
| # Initialize the Anthropic client | |
| client = anthropic.Anthropic(api_key=api_key) | |
| language = SUPPORTED_LANGUAGES[language_code] | |
| # Extract text from PDF | |
| progress_tracker.update(5, f"Extracting text from dashboard {dashboard_index}...") | |
| full_text = extract_text_from_pdf(pdf_bytes) | |
| print(f"Text extracted: {len(full_text)} characters") | |
| # Convert PDF to images | |
| progress_tracker.update(10, f"Converting dashboard {dashboard_index} to images...") | |
| images = convert_from_bytes(pdf_bytes, dpi=150) | |
| if not images: | |
| print("Error: Could not convert PDF to images.") | |
| return None, None | |
| # Combine all pages into one vertical image | |
| progress_tracker.update(15, f"Combining pages for dashboard {dashboard_index}...") | |
| total_width = max(img.width for img in images) | |
| total_height = sum(img.height for img in images) | |
| combined_image = Image.new('RGB', (total_width, total_height), 'white') | |
| y_offset = 0 | |
| for img in images: | |
| combined_image.paste(img, (0, y_offset)) | |
| y_offset += img.height | |
| print(f"Combined image size: {combined_image.width}x{combined_image.height} pixels") | |
| # Divide the image into sections | |
| progress_tracker.update(20, f"Dividing dashboard {dashboard_index} into {num_sections} sections...") | |
| sections = divide_image_vertically(combined_image, num_sections) | |
| # Analyze each section | |
| section_analyses = [] | |
| for i, section in enumerate(sections): | |
| section_progress = 20 + (i / num_sections * 50) # 50% of progress for all sections | |
| progress_tracker.update(section_progress, f"Analyzing section {i+1}/{num_sections} of dashboard {dashboard_index}...") | |
| analysis = analyze_dashboard_section( | |
| client=client, | |
| section_number=i+1, | |
| total_sections=num_sections, | |
| image_section=section, | |
| full_text=full_text, | |
| language=language, | |
| model_name=model_name, | |
| goal_description=goal_description | |
| ) | |
| section_analyses.append(f"\n## {language['section_title']} {i+1}\n{analysis}") | |
| print(f"Section {i+1}/{num_sections} analyzed.") | |
| # Combine section analyses | |
| combined_analyses = "\n\n".join(section_analyses) | |
| # Generate the comprehensive report | |
| progress_tracker.update(70, f"Creating comprehensive report for dashboard {dashboard_index}...") | |
| comprehensive_report = create_comprehensive_report( | |
| client=client, | |
| section_analyses=combined_analyses, | |
| full_text=full_text, | |
| language=language, | |
| model_name=model_name, | |
| goal_description=goal_description | |
| ) | |
| # Format the final report with title | |
| final_report = f"# {language['multi_doc_title'].format(index=dashboard_index)}\n\n{comprehensive_report}" | |
| return final_report, section_analyses | |
| def process_multiple_dashboards(api_key, pdf_files, language_code, model_name, goal_description=None, num_sections=4): | |
| """Process multiple dashboard PDFs and generate individual and comparative reports.""" | |
| print("\n" + "#"*60) | |
| print(f"Starting multi-dashboard analysis with {len(pdf_files)} dashboard(s)...") | |
| print(f"Model: {model_name}") | |
| print("#"*60 + "\n") | |
| progress_tracker.start_processing() | |
| language = SUPPORTED_LANGUAGES[language_code] | |
| # Initialize the Anthropic client | |
| client = anthropic.Anthropic(api_key=api_key) | |
| # Step 1: Process each dashboard individually | |
| individual_reports = [] | |
| individual_analyses = [] | |
| for i, pdf_bytes in enumerate(pdf_files): | |
| # Update progress for this dashboard | |
| dashboard_progress = 10 + (i / len(pdf_files) * 70) # 70% for all dashboards | |
| progress_tracker.update(dashboard_progress, f"Processing dashboard {i+1}/{len(pdf_files)}...") | |
| report, analysis = process_single_dashboard( | |
| api_key=api_key, | |
| pdf_bytes=pdf_bytes, | |
| language_code=language_code, | |
| model_name=model_name, | |
| goal_description=goal_description, | |
| num_sections=num_sections, | |
| dashboard_index=i+1 | |
| ) | |
| if report: | |
| individual_reports.append(report) | |
| individual_analyses.append(analysis) | |
| print(f"✅ Analysis of dashboard {i+1} completed.") | |
| else: | |
| print(f"❌ Analysis of dashboard {i+1} failed.") | |
| # Step 3: Generate output files | |
| progress_tracker.update(80, "Generating output files...") | |
| timestamp = time.strftime("%Y%m%d_%H%M%S") | |
| output_files = [] | |
| # Create individual report files | |
| for i, report in enumerate(individual_reports): | |
| file_progress = 80 + (i / len(individual_reports) * 10) # 10% for creating files | |
| progress_tracker.update(file_progress, f"Creating files for dashboard {i+1}...") | |
| md_filename = f"dashboard_{i+1}_{language['code']}_{timestamp}.md" | |
| pdf_filename = f"dashboard_{i+1}_{language['code']}_{timestamp}.pdf" | |
| with open(md_filename, 'w', encoding='utf-8') as f: | |
| f.write(report) | |
| output_files.append(md_filename) | |
| try: | |
| pdf_path = markdown_to_pdf(report, pdf_filename, language) | |
| output_files.append(pdf_filename) | |
| except Exception as e: | |
| print(f"⚠️ Error converting dashboard {i+1} to PDF: {str(e)}") | |
| # If there are multiple dashboards, create a comparative report | |
| if len(individual_reports) > 1: | |
| progress_tracker.update(90, "Creating comparative analysis...") | |
| print("\n" + "#"*60) | |
| print("Creating comparative analysis of all dashboards...") | |
| # Combined report content | |
| all_reports_content = "\n\n".join(individual_reports) | |
| # Generate comparative analysis | |
| comparative_report = create_multi_dashboard_comparative_report( | |
| client=client, | |
| individual_reports=all_reports_content, | |
| language=language, | |
| model_name=model_name, | |
| goal_description=goal_description | |
| ) | |
| # Save comparative report | |
| progress_tracker.update(95, "Saving comparative analysis files...") | |
| comparative_md = f"comparative_analysis_{language['code']}_{timestamp}.md" | |
| comparative_pdf = f"comparative_analysis_{language['code']}_{timestamp}.pdf" | |
| with open(comparative_md, 'w', encoding='utf-8') as f: | |
| f.write(comparative_report) | |
| output_files.append(comparative_md) | |
| try: | |
| pdf_path = markdown_to_pdf(comparative_report, comparative_pdf, language) | |
| output_files.append(comparative_pdf) | |
| except Exception as e: | |
| print(f"⚠️ Error converting comparative report to PDF: {str(e)}") | |
| # Complete progress tracking | |
| progress_tracker.update(100, "✅ Analysis completed successfully!") | |
| progress_tracker.end_processing() | |
| # Return the combined report content and all output files | |
| combined_content = "\n\n---\n\n".join(individual_reports) | |
| if len(individual_reports) > 1 and 'comparative_report' in locals(): | |
| combined_content += f"\n\n{'='*80}\n\n# COMPARATIVE ANALYSIS\n\n{comparative_report}" | |
| return combined_content, output_files, "✅ Analysis completed successfully!" | |
| # Gradio Interface Functions | |
| def process_dashboard(api_key, pdf_files, language_choice, model_choice, analysis_goal, num_sections, progress=gr.Progress()): | |
| """Process the dashboard via the Gradio interface.""" | |
| if not api_key or not pdf_files: | |
| return None, None, "Please provide both the API key and at least one PDF file." | |
| # Map language choice to language code (matching SUPPORTED_LANGUAGES keys) | |
| language_codes = { | |
| "Italiano": "italiano", | |
| "English": "english", | |
| "Français": "français", | |
| "Español": "español", | |
| "Deutsch": "deutsch" | |
| } | |
| language_code = language_codes.get(language_choice, "italiano") | |
| # Get the model name from the choice | |
| model_name = AVAILABLE_MODELS.get(model_choice, AVAILABLE_MODELS["Claude Sonnet 4.5"]) | |
| # Validate number of sections | |
| try: | |
| num_sections = int(num_sections) | |
| if num_sections < 1: | |
| num_sections = 4 # Default | |
| except: | |
| num_sections = 4 # Default | |
| # Convert the uploaded files to a list of bytes | |
| progress(0, desc="Preparing files...") | |
| pdf_bytes_list = [] | |
| for pdf_file in pdf_files: | |
| with open(pdf_file.name, "rb") as f: | |
| pdf_bytes_list.append(f.read()) | |
| # Start progress thread for text updates | |
| progress_thread = threading.Thread(target=update_progress) | |
| progress_thread.daemon = True | |
| progress_thread.start() | |
| # Process the dashboards | |
| try: | |
| # Override the progress_tracker update function to also update the Gradio progress bar | |
| original_update = progress_tracker.update | |
| def combined_update(percent, message="Processing..."): | |
| original_update(percent, message) | |
| progress(percent/100, desc=message) | |
| progress_tracker.update = combined_update | |
| md_content, output_files, status_message = process_multiple_dashboards( | |
| api_key=api_key, | |
| pdf_files=pdf_bytes_list, | |
| language_code=language_code, | |
| model_name=model_name, | |
| goal_description=analysis_goal, | |
| num_sections=num_sections | |
| ) | |
| if md_content and output_files: | |
| progress(1.0, desc="Complete!") | |
| return md_content, output_files, progress_tracker.get_status() | |
| else: | |
| return None, None, "❌ Error during dashboard analysis." | |
| except Exception as e: | |
| progress_tracker.update(100, f"❌ Error: {str(e)}") | |
| progress_tracker.end_processing() | |
| progress(1.0, desc=f"Error: {str(e)}") | |
| return None, None, f"❌ Error: {str(e)}" | |
| def create_interface(): | |
| global output_status | |
| with gr.Blocks(title="Dashboard Narrator - Powered by sandsiv+", theme=gr.themes.Soft()) as app: | |
| gr.Markdown(""" | |
| # 📊 Dashboard Narrator - Powered by sandsiv+ | |
| Unlock the hidden stories in your dashboards!<br> | |
| Dashboard Narrator leverages sandsiv+ advanced AI to dissect your PDF reports,<br> | |
| analyze each segment with expert precision, and craft comprehensive insights in your preferred language.<br><br> | |
| Turn complex data visualizations into clear, strategic recommendations and uncover trends you might have missed.<br> | |
| From executive summaries to detailed breakdowns, get the full narrative behind your numbers in just a few clicks.<br><br> | |
| """) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| api_key = gr.Textbox(label="API Key Anthropic", placeholder="Enter your Claude API key...", type="password") | |
| model_choice = gr.Dropdown( | |
| choices=list(AVAILABLE_MODELS.keys()), | |
| value="Claude Sonnet 4.5", | |
| label="AI Model", | |
| info="Sonnet 4.5 is faster and cost-effective. Opus 4.1 provides deeper analysis." | |
| ) | |
| language = gr.Dropdown(choices=["Italiano", "English", "Français", "Español", "Deutsch"], value="Italiano", label="Report Language") | |
| num_sections = gr.Slider(minimum=2, maximum=10, value=4, step=1, label="Number of Vertical Sections per Dashboard") | |
| goal = gr.Textbox(label="Analysis Goal (optional)", placeholder="E.g., Analyze Q1 2024 sales KPIs...") | |
| pdf_files = gr.File(label="Upload Dashboards (PDF)", file_types=[".pdf"], file_count="multiple") | |
| analyze_btn = gr.Button("🔍 Analyze Dashboards", variant="primary") | |
| with gr.Column(scale=2): | |
| with gr.Tab("Report"): | |
| output_md = gr.Markdown(label="Analysis Report", value="") | |
| with gr.Tab("Output Files"): | |
| output_files = gr.File(label="Download Files") | |
| output_status = gr.Textbox(label="Progress", placeholder="Upload dashboards and press Analyze...", interactive=False) | |
| # Progress component doesn't accept label in Gradio 5.21.0 | |
| progress_bar = gr.Progress() | |
| analyze_btn.click( | |
| fn=process_dashboard, | |
| inputs=[api_key, pdf_files, language, model_choice, goal, num_sections], | |
| outputs=[output_md, output_files, output_status] | |
| ) | |
| return app | |
| def main(): | |
| app = create_interface() | |
| app.launch(share=True, debug=True) | |
| if __name__ == "__main__": | |
| main() |