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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
# Wrapper function for Gradio interface
|
| 4 |
def process_dashboard(api_key, pdf_files, language_name, goal_description=None, num_sections=4, model_name=DEFAULT_MODEL, custom_model=None):
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Dashboard Narrator - Powered by OpenRouter.ai
|
| 3 |
+
A tool to analyze dashboard PDFs and generate comprehensive reports.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
# Import required libraries
|
| 7 |
+
import os
|
| 8 |
+
import time
|
| 9 |
+
import threading
|
| 10 |
+
import io
|
| 11 |
+
import base64
|
| 12 |
+
import json
|
| 13 |
+
import requests
|
| 14 |
+
from PyPDF2 import PdfReader
|
| 15 |
+
from PIL import Image
|
| 16 |
+
import markdown
|
| 17 |
+
from weasyprint import HTML, CSS
|
| 18 |
+
from weasyprint.text.fonts import FontConfiguration
|
| 19 |
+
from pdf2image import convert_from_bytes
|
| 20 |
+
import gradio as gr
|
| 21 |
+
|
| 22 |
+
# Create a global progress tracker
|
| 23 |
+
class ProgressTracker:
|
| 24 |
+
def __init__(self):
|
| 25 |
+
self.progress = 0
|
| 26 |
+
self.message = "Ready"
|
| 27 |
+
self.is_processing = False
|
| 28 |
+
self.lock = threading.Lock()
|
| 29 |
+
|
| 30 |
+
def update(self, progress, message="Processing..."):
|
| 31 |
+
with self.lock:
|
| 32 |
+
self.progress = progress
|
| 33 |
+
self.message = message
|
| 34 |
+
|
| 35 |
+
def get_status(self):
|
| 36 |
+
with self.lock:
|
| 37 |
+
return f"{self.message} ({self.progress:.1f}%)"
|
| 38 |
+
|
| 39 |
+
def start_processing(self):
|
| 40 |
+
with self.lock:
|
| 41 |
+
self.is_processing = True
|
| 42 |
+
self.progress = 0
|
| 43 |
+
self.message = "Starting..."
|
| 44 |
+
|
| 45 |
+
def end_processing(self):
|
| 46 |
+
with self.lock:
|
| 47 |
+
self.is_processing = False
|
| 48 |
+
self.progress = 100
|
| 49 |
+
self.message = "Complete"
|
| 50 |
+
|
| 51 |
+
# Create a global instance
|
| 52 |
+
progress_tracker = ProgressTracker()
|
| 53 |
+
output_status = None
|
| 54 |
+
|
| 55 |
+
# Function to update the Gradio interface with progress
|
| 56 |
+
def update_progress():
|
| 57 |
+
global output_status
|
| 58 |
+
while progress_tracker.is_processing:
|
| 59 |
+
status = progress_tracker.get_status()
|
| 60 |
+
if output_status is not None:
|
| 61 |
+
output_status.update(value=status)
|
| 62 |
+
time.sleep(0.5)
|
| 63 |
+
return
|
| 64 |
+
|
| 65 |
+
# OpenRouter Client for making API calls
|
| 66 |
+
class OpenRouterClient:
|
| 67 |
+
def __init__(self, api_key):
|
| 68 |
+
self.api_key = api_key
|
| 69 |
+
self.base_url = "https://openrouter.ai/api/v1"
|
| 70 |
+
|
| 71 |
+
def messages_create(self, model, messages, system=None, temperature=0.7, max_tokens=None):
|
| 72 |
+
"""Send messages to the OpenRouter API and return the response"""
|
| 73 |
+
url = f"{self.base_url}/chat/completions"
|
| 74 |
+
|
| 75 |
+
headers = {
|
| 76 |
+
"Authorization": f"Bearer {self.api_key}",
|
| 77 |
+
"Content-Type": "application/json"
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
payload = {
|
| 81 |
+
"model": model,
|
| 82 |
+
"messages": messages,
|
| 83 |
+
"temperature": temperature,
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
# Add system message if provided
|
| 87 |
+
if system:
|
| 88 |
+
payload["messages"].insert(0, {"role": "system", "content": system})
|
| 89 |
+
|
| 90 |
+
# Add max_tokens if provided
|
| 91 |
+
if max_tokens:
|
| 92 |
+
payload["max_tokens"] = max_tokens
|
| 93 |
+
|
| 94 |
+
try:
|
| 95 |
+
response = requests.post(url, headers=headers, json=payload)
|
| 96 |
+
response.raise_for_status() # Raise an exception for HTTP errors
|
| 97 |
+
|
| 98 |
+
result = response.json()
|
| 99 |
+
|
| 100 |
+
# Format the response to match the expected structure
|
| 101 |
+
formatted_response = type('obj', (object,), {
|
| 102 |
+
'content': [
|
| 103 |
+
type('obj', (object,), {
|
| 104 |
+
'text': result['choices'][0]['message']['content']
|
| 105 |
+
})
|
| 106 |
+
]
|
| 107 |
+
})
|
| 108 |
+
|
| 109 |
+
return formatted_response
|
| 110 |
+
|
| 111 |
+
except requests.exceptions.RequestException as e:
|
| 112 |
+
print(f"API request error: {str(e)}")
|
| 113 |
+
if hasattr(e, 'response') and e.response:
|
| 114 |
+
print(f"Response: {e.response.text}")
|
| 115 |
+
raise
|
| 116 |
+
|
| 117 |
+
# Supported languages configuration
|
| 118 |
+
SUPPORTED_LANGUAGES = {
|
| 119 |
+
"italiano": {
|
| 120 |
+
"code": "it",
|
| 121 |
+
"name": "Italiano",
|
| 122 |
+
"report_title": "Analisi Dashboard",
|
| 123 |
+
"report_subtitle": "Report Dettagliato",
|
| 124 |
+
"date_label": "Data",
|
| 125 |
+
"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.",
|
| 126 |
+
"section_title": "ANALISI SEZIONE",
|
| 127 |
+
"multi_doc_title": "ANALISI DASHBOARD {index}"
|
| 128 |
+
},
|
| 129 |
+
"english": {
|
| 130 |
+
"code": "en",
|
| 131 |
+
"name": "English",
|
| 132 |
+
"report_title": "Dashboard Analysis",
|
| 133 |
+
"report_subtitle": "Detailed Report",
|
| 134 |
+
"date_label": "Date",
|
| 135 |
+
"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.",
|
| 136 |
+
"section_title": "SECTION ANALYSIS",
|
| 137 |
+
"multi_doc_title": "DASHBOARD {index} ANALYSIS"
|
| 138 |
+
},
|
| 139 |
+
"franΓ§ais": {
|
| 140 |
+
"code": "fr",
|
| 141 |
+
"name": "FranΓ§ais",
|
| 142 |
+
"report_title": "Analyse de Tableau de Bord",
|
| 143 |
+
"report_subtitle": "Rapport DΓ©taillΓ©",
|
| 144 |
+
"date_label": "Date",
|
| 145 |
+
"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.",
|
| 146 |
+
"section_title": "ANALYSE DE SECTION",
|
| 147 |
+
"multi_doc_title": "ANALYSE DU TABLEAU DE BORD {index}"
|
| 148 |
+
},
|
| 149 |
+
"espaΓ±ol": {
|
| 150 |
+
"code": "es",
|
| 151 |
+
"name": "EspaΓ±ol",
|
| 152 |
+
"report_title": "AnΓ‘lisis de Dashboard",
|
| 153 |
+
"report_subtitle": "Informe Detallado",
|
| 154 |
+
"date_label": "Fecha",
|
| 155 |
+
"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.",
|
| 156 |
+
"section_title": "ANΓLISIS DE SECCIΓN",
|
| 157 |
+
"multi_doc_title": "ANΓLISIS DEL DASHBOARD {index}"
|
| 158 |
+
},
|
| 159 |
+
"deutsch": {
|
| 160 |
+
"code": "de",
|
| 161 |
+
"name": "Deutsch",
|
| 162 |
+
"report_title": "Dashboard-Analyse",
|
| 163 |
+
"report_subtitle": "Detaillierter Bericht",
|
| 164 |
+
"date_label": "Datum",
|
| 165 |
+
"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.",
|
| 166 |
+
"section_title": "ABSCHNITTSANALYSE",
|
| 167 |
+
"multi_doc_title": "DASHBOARD-ANALYSE {index}"
|
| 168 |
+
}
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
# OpenRouter models
|
| 172 |
+
DEFAULT_MODEL = "meta-llama/llama-4-scout:free"
|
| 173 |
+
OPENROUTER_MODELS = [
|
| 174 |
+
"meta-llama/llama-4-scout:free",
|
| 175 |
+
"anthropic/claude-3-haiku:20240307",
|
| 176 |
+
"anthropic/claude-3-sonnet:20240229",
|
| 177 |
+
"anthropic/claude-3-opus:20240229",
|
| 178 |
+
"google/gemini-pro-1.5:latest",
|
| 179 |
+
"mistralai/mistral-large-latest"
|
| 180 |
+
]
|
| 181 |
+
|
| 182 |
+
# Utility Functions
|
| 183 |
+
def extract_text_from_pdf(pdf_bytes):
|
| 184 |
+
"""Extract text from a PDF file."""
|
| 185 |
+
try:
|
| 186 |
+
pdf_reader = PdfReader(io.BytesIO(pdf_bytes))
|
| 187 |
+
text = ""
|
| 188 |
+
for page_num in range(len(pdf_reader.pages)):
|
| 189 |
+
extracted = pdf_reader.pages[page_num].extract_text()
|
| 190 |
+
if extracted:
|
| 191 |
+
text += extracted + "\n"
|
| 192 |
+
return text
|
| 193 |
+
except Exception as e:
|
| 194 |
+
print(f"Error extracting text from PDF: {str(e)}")
|
| 195 |
+
return ""
|
| 196 |
+
|
| 197 |
+
def divide_image_vertically(image, num_sections):
|
| 198 |
+
"""Divide an image vertically into sections."""
|
| 199 |
+
width, height = image.size
|
| 200 |
+
section_height = height // num_sections
|
| 201 |
+
sections = []
|
| 202 |
+
for i in range(num_sections):
|
| 203 |
+
top = i * section_height
|
| 204 |
+
bottom = height if i == num_sections - 1 else (i + 1) * section_height
|
| 205 |
+
section = image.crop((0, top, width, bottom))
|
| 206 |
+
sections.append(section)
|
| 207 |
+
print(f"Section {i+1}: size {section.width}x{section.height} pixels")
|
| 208 |
+
return sections
|
| 209 |
+
|
| 210 |
+
def encode_image_with_resize(image, max_size_mb=4.5):
|
| 211 |
+
"""Encode an image in base64, resizing if necessary."""
|
| 212 |
+
max_bytes = max_size_mb * 1024 * 1024
|
| 213 |
+
img_byte_arr = io.BytesIO()
|
| 214 |
+
image.save(img_byte_arr, format='PNG')
|
| 215 |
+
current_size = len(img_byte_arr.getvalue())
|
| 216 |
+
if current_size > max_bytes:
|
| 217 |
+
scale_factor = (max_bytes / current_size) ** 0.5
|
| 218 |
+
new_width = int(image.width * scale_factor)
|
| 219 |
+
new_height = int(image.height * scale_factor)
|
| 220 |
+
resized_image = image.resize((new_width, new_height), Image.LANCZOS)
|
| 221 |
+
img_byte_arr = io.BytesIO()
|
| 222 |
+
resized_image.save(img_byte_arr, format='PNG', optimize=True)
|
| 223 |
+
print(f"Image resized from {current_size/1024/1024:.2f}MB to {len(img_byte_arr.getvalue())/1024/1024:.2f}MB")
|
| 224 |
+
image = resized_image
|
| 225 |
+
else:
|
| 226 |
+
print(f"Image size acceptable: {current_size/1024/1024:.2f}MB")
|
| 227 |
+
buffer = io.BytesIO()
|
| 228 |
+
image.save(buffer, format="PNG", optimize=True)
|
| 229 |
+
return base64.b64encode(buffer.getvalue()).decode("utf-8")
|
| 230 |
+
|
| 231 |
+
# Core Analysis Functions
|
| 232 |
+
def analyze_dashboard_section(client, model, section_number, total_sections, image_section, full_text, language, goal_description=None):
|
| 233 |
+
"""Analyze a vertical section of the dashboard in the specified language."""
|
| 234 |
+
print(f"Analyzing section {section_number}/{total_sections} in {language['name']} using {model}...")
|
| 235 |
+
try:
|
| 236 |
+
encoded_image = encode_image_with_resize(image_section)
|
| 237 |
+
except Exception as e:
|
| 238 |
+
print(f"Error encoding section {section_number}: {str(e)}")
|
| 239 |
+
return f"Error analyzing section {section_number}: {str(e)}"
|
| 240 |
+
|
| 241 |
+
section_prompt = f"""
|
| 242 |
+
Act as a senior data analyst examining this dashboard section for Customer Experience purpose.\n
|
| 243 |
+
Your analysis will be shared with top executives to inform about Customer Experience improvements and customer satisfaction level.\n
|
| 244 |
+
# Dashboard Analysis - Section {section_number} of {total_sections}\n
|
| 245 |
+
You are analyzing section {section_number} of {total_sections} of a long vertical dashboard. This is part of a broader analysis.\n
|
| 246 |
+
{f"The analysis objective is: {goal_description}" if goal_description else ""}\n\n
|
| 247 |
+
For this specific section:\n
|
| 248 |
+
1. Describe what these visualizations show, including their type (e.g., bar chart, line graph) and the data they represent\n
|
| 249 |
+
2. Quantitatively analyze the data, noting specific values, percentages, and numeric trends\n
|
| 250 |
+
3. Identify significant patterns, anomalies, or outliers visible in the data\n
|
| 251 |
+
4. Provide 2-3 actionable insights based on this analysis, explaining their business implications\n
|
| 252 |
+
5. Suggest possible reasons for any notable trends or unexpected findings\n
|
| 253 |
+
Focus exclusively on the visible section. Don't reference or speculate about unseen dashboard elements.\n
|
| 254 |
+
Answer completely in {language['name']}.\n\n
|
| 255 |
+
# Text extracted from the complete dashboard:\n
|
| 256 |
+
{full_text[:10000]}
|
| 257 |
+
|
| 258 |
+
# Image of this dashboard section:
|
| 259 |
+
[BASE64 IMAGE: {encoded_image[:20]}...]
|
| 260 |
+
This is a dashboard visualization showing various metrics and charts. Please analyze the content visible in this image.
|
| 261 |
+
"""
|
| 262 |
+
|
| 263 |
+
try:
|
| 264 |
+
response = client.messages_create(
|
| 265 |
+
model=model,
|
| 266 |
+
messages=[{"role": "user", "content": section_prompt}],
|
| 267 |
+
system=language['system_prompt'],
|
| 268 |
+
temperature=0.1,
|
| 269 |
+
max_tokens=10000
|
| 270 |
+
)
|
| 271 |
+
return response.content[0].text
|
| 272 |
+
except Exception as e:
|
| 273 |
+
print(f"Error analyzing section {section_number}: {str(e)}")
|
| 274 |
+
return f"Error analyzing section {section_number}: {str(e)}"
|
| 275 |
+
|
| 276 |
+
def create_comprehensive_report(client, model, section_analyses, full_text, language, goal_description=None):
|
| 277 |
+
"""Create a unified comprehensive report based on individual section analyses."""
|
| 278 |
+
print(f"Generating final comprehensive report in {language['name']} using {model}...")
|
| 279 |
+
comprehensive_prompt = f"""
|
| 280 |
+
# Comprehensive Dashboard Analysis Request
|
| 281 |
+
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
|
| 282 |
+
{f"The analysis objective is: {goal_description}" if goal_description else ""}\n\n
|
| 283 |
+
Here are the analyses of the individual dashboard sections:\n
|
| 284 |
+
{section_analyses}\n\n
|
| 285 |
+
Based on these partial analyses, generate a professional, structured, and coherent report that includes:\n
|
| 286 |
+
1. Executive Summary - Include key metrics, major findings, and critical recommendations (limit to 1 page equivalent)\n
|
| 287 |
+
2. Dashboard Performance Overview - Add a section that evaluates the overall health metrics before diving into categories\n
|
| 288 |
+
3 Detailed Analysis by Category - Keep this, it's essential\n
|
| 289 |
+
4 Trend Analysis - Broaden from just temporal to include cross-category patterns\n
|
| 290 |
+
5 Critical Issues and Opportunities - Combine anomalies with positive outliers to provide balanced insights\n
|
| 291 |
+
6 Strategic Implications and Recommendations - Consolidate your insights and recommendations into a single, stronger section\n
|
| 292 |
+
7 Implementation Roadmap - Convert your conclusions into a prioritized action plan with timeframes\n
|
| 293 |
+
8 Appendix: Monitoring Improvements - Move the monitoring suggestions to an appendix unless they're a primary focus\n\n
|
| 294 |
+
Integrate information from all sections to create a coherent and complete report.\n\n
|
| 295 |
+
# Text extracted from the complete dashboard:\n
|
| 296 |
+
{full_text[:10000]}
|
| 297 |
+
"""
|
| 298 |
+
try:
|
| 299 |
+
response = client.messages_create(
|
| 300 |
+
model=model,
|
| 301 |
+
messages=[{"role": "user", "content": comprehensive_prompt}],
|
| 302 |
+
system=language['system_prompt'],
|
| 303 |
+
temperature=0.1,
|
| 304 |
+
max_tokens=10000
|
| 305 |
+
)
|
| 306 |
+
return response.content[0].text
|
| 307 |
+
except Exception as e:
|
| 308 |
+
print(f"Error creating comprehensive report: {str(e)}")
|
| 309 |
+
return f"Error creating comprehensive report: {str(e)}"
|
| 310 |
+
|
| 311 |
+
def create_multi_dashboard_comparative_report(client, model, individual_reports, language, goal_description=None):
|
| 312 |
+
"""Create a comparative report analyzing multiple dashboards together."""
|
| 313 |
+
print(f"Generating comparative report for multiple dashboards in {language['name']} using {model}...")
|
| 314 |
+
comparative_prompt = f"""
|
| 315 |
+
# Multi-Dashboard Comparative Analysis Request
|
| 316 |
+
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.
|
| 317 |
+
{f"The analysis objective is: {goal_description}" if goal_description else ""}
|
| 318 |
+
Here are the analyses of the individual dashboards:
|
| 319 |
+
{individual_reports}
|
| 320 |
+
Based on these individual analyses, generate a professional, structured comparative report that includes:
|
| 321 |
+
1. Executive Overview of All Dashboards
|
| 322 |
+
2. Comparative Analysis of Key Metrics
|
| 323 |
+
3. Cross-Dashboard Patterns and Trends
|
| 324 |
+
4. Notable Differences Between Dashboards
|
| 325 |
+
5. Integrated Insights from All Sources
|
| 326 |
+
6. Comprehensive Strategic Recommendations
|
| 327 |
+
7. Suggestions for Cross-Dashboard Monitoring Improvements
|
| 328 |
+
8. Conclusions and Integrated Next Steps
|
| 329 |
+
Integrate information from all dashboards to create a coherent comparative report.
|
| 330 |
+
"""
|
| 331 |
+
try:
|
| 332 |
+
response = client.messages_create(
|
| 333 |
+
model=model,
|
| 334 |
+
messages=[{"role": "user", "content": comparative_prompt}],
|
| 335 |
+
system=language['system_prompt'],
|
| 336 |
+
temperature=0.1,
|
| 337 |
+
max_tokens=12000
|
| 338 |
+
)
|
| 339 |
+
return response.content[0].text
|
| 340 |
+
except Exception as e:
|
| 341 |
+
print(f"Error creating comparative report: {str(e)}")
|
| 342 |
+
return f"Error creating comparative report: {str(e)}"
|
| 343 |
+
|
| 344 |
+
def markdown_to_pdf(markdown_content, output_filename, language):
|
| 345 |
+
"""Convert Markdown content to a well-formatted PDF."""
|
| 346 |
+
print(f"Converting Markdown report to PDF in {language['name']}...")
|
| 347 |
+
css = CSS(string='''
|
| 348 |
+
@page { margin: 1.5cm; }
|
| 349 |
+
body { font-family: Arial, sans-serif; line-height: 1.5; font-size: 11pt; }
|
| 350 |
+
h1 { color: #2c3e50; font-size: 22pt; margin-top: 1cm; margin-bottom: 0.5cm; page-break-after: avoid; }
|
| 351 |
+
h2 { color: #3498db; font-size: 16pt; margin-top: 0.8cm; margin-bottom: 0.3cm; page-break-after: avoid; }
|
| 352 |
+
p { margin-bottom: 0.3cm; text-align: justify; }
|
| 353 |
+
''')
|
| 354 |
+
today = time.strftime("%d/%m/%Y")
|
| 355 |
+
cover_page = f"""
|
| 356 |
+
<div style="text-align: center; height: 100vh; display: flex; flex-direction: column; justify-content: center; align-items: center;">
|
| 357 |
+
<h1 style="font-size: 26pt; color: #2c3e50;">{language['report_title']}</h1>
|
| 358 |
+
<h2 style="font-size: 14pt; color: #7f8c8d;">{language['report_subtitle']}</h2>
|
| 359 |
+
<p style="font-size: 12pt; color: #7f8c8d;">{language['date_label']}: {today}</p>
|
| 360 |
+
</div>
|
| 361 |
+
<div style="page-break-after: always;"></div>
|
| 362 |
+
"""
|
| 363 |
+
html_content = markdown.markdown(markdown_content, extensions=['tables', 'fenced_code'])
|
| 364 |
+
full_html = f"""
|
| 365 |
+
<!DOCTYPE html>
|
| 366 |
+
<html lang="{language['code']}">
|
| 367 |
+
<head><meta charset="UTF-8"><title>{language['report_title']}</title></head>
|
| 368 |
+
<body>{cover_page}{html_content}</body>
|
| 369 |
+
</html>
|
| 370 |
+
"""
|
| 371 |
+
font_config = FontConfiguration()
|
| 372 |
+
HTML(string=full_html).write_pdf(output_filename, stylesheets=[css], font_config=font_config)
|
| 373 |
+
print(f"PDF created successfully: {output_filename}")
|
| 374 |
+
return output_filename
|
| 375 |
+
|
| 376 |
+
def analyze_vertical_dashboard(client, model, pdf_bytes, language, goal_description=None, num_sections=4, dashboard_index=None):
|
| 377 |
+
"""Analyze a vertical dashboard by dividing it into sections."""
|
| 378 |
+
dashboard_marker = f" {dashboard_index}" if dashboard_index is not None else ""
|
| 379 |
+
total_dashboards = progress_tracker.total_dashboards if hasattr(progress_tracker, 'total_dashboards') else 1
|
| 380 |
+
dashboard_progress_base = ((dashboard_index - 1) / total_dashboards * 100) if dashboard_index is not None else 0
|
| 381 |
+
dashboard_progress_step = (100 / total_dashboards) if total_dashboards > 0 else 100
|
| 382 |
+
|
| 383 |
+
progress_tracker.update(dashboard_progress_base, f"πΌοΈ Analyzing dashboard{dashboard_marker}...")
|
| 384 |
+
print(f"πΌοΈ Analyzing dashboard{dashboard_marker}...")
|
| 385 |
+
|
| 386 |
+
progress_tracker.update(dashboard_progress_base + dashboard_progress_step * 0.1, f"π Extracting text from dashboard{dashboard_marker}...")
|
| 387 |
+
print(f"π Extracting full text from PDF...")
|
| 388 |
+
full_text = extract_text_from_pdf(pdf_bytes)
|
| 389 |
+
if not full_text or len(full_text.strip()) < 100:
|
| 390 |
+
print("β οΈ Limited text extracted from PDF. Analysis will rely primarily on images.")
|
| 391 |
+
else:
|
| 392 |
+
print(f"β
Extracted {len(full_text)} characters of text from PDF.")
|
| 393 |
+
|
| 394 |
+
progress_tracker.update(dashboard_progress_base + dashboard_progress_step * 0.2, f"πΌοΈ Converting dashboard{dashboard_marker} to images...")
|
| 395 |
+
print("πΌοΈ Converting PDF to images...")
|
| 396 |
+
try:
|
| 397 |
+
pdf_images = convert_from_bytes(pdf_bytes, dpi=150)
|
| 398 |
+
if not pdf_images:
|
| 399 |
+
print("β Unable to convert PDF to images.")
|
| 400 |
+
return None, "Error: Unable to convert PDF to images."
|
| 401 |
+
print(f"β
PDF converted to {len(pdf_images)} image pages.")
|
| 402 |
+
main_image = pdf_images[0]
|
| 403 |
+
print(f"Main image size: {main_image.width}x{main_image.height} pixels")
|
| 404 |
+
|
| 405 |
+
progress_tracker.update(dashboard_progress_base + dashboard_progress_step * 0.3, f"Dividing dashboard{dashboard_marker} into {num_sections} sections...")
|
| 406 |
+
print(f"Dividing image into {num_sections} vertical sections...")
|
| 407 |
+
image_sections = divide_image_vertically(main_image, num_sections)
|
| 408 |
+
print(f"β
Image divided into {len(image_sections)} sections.")
|
| 409 |
+
except Exception as e:
|
| 410 |
+
print(f"β Error converting or dividing PDF: {str(e)}")
|
| 411 |
+
return None, f"Error: {str(e)}"
|
| 412 |
+
|
| 413 |
+
section_analyses = []
|
| 414 |
+
section_progress_step = dashboard_progress_step * 0.4 / len(image_sections)
|
| 415 |
+
|
| 416 |
+
for i, section in enumerate(image_sections):
|
| 417 |
+
section_progress = dashboard_progress_base + dashboard_progress_step * 0.3 + section_progress_step * i
|
| 418 |
+
progress_tracker.update(section_progress, f"Analyzing section {i+1}/{len(image_sections)} of dashboard{dashboard_marker}...")
|
| 419 |
+
|
| 420 |
+
print(f"\n{'='*50}")
|
| 421 |
+
print(f"Processing section {i+1}/{len(image_sections)}...")
|
| 422 |
+
section_result = analyze_dashboard_section(
|
| 423 |
+
client,
|
| 424 |
+
model,
|
| 425 |
+
i+1,
|
| 426 |
+
len(image_sections),
|
| 427 |
+
section,
|
| 428 |
+
full_text,
|
| 429 |
+
language,
|
| 430 |
+
goal_description
|
| 431 |
+
)
|
| 432 |
+
if section_result:
|
| 433 |
+
section_analyses.append(f"\n## {language['section_title']} {i+1}\n{section_result}")
|
| 434 |
+
print(f"β
Analysis of section {i+1} completed.")
|
| 435 |
+
else:
|
| 436 |
+
section_analyses.append(f"\n## {language['section_title']} {i+1}\nAnalysis not available for this section.")
|
| 437 |
+
print(f"β οΈ Analysis of section {i+1} not available.")
|
| 438 |
+
|
| 439 |
+
progress_tracker.update(dashboard_progress_base + dashboard_progress_step * 0.7, f"Generating final report for dashboard{dashboard_marker}...")
|
| 440 |
+
print("\n" + "="*50)
|
| 441 |
+
print(f"All section analyses completed. Generating report...")
|
| 442 |
+
combined_sections = "\n".join(section_analyses)
|
| 443 |
+
|
| 444 |
+
# If dashboard index is provided, add a header for the dashboard
|
| 445 |
+
if dashboard_index is not None:
|
| 446 |
+
dashboard_header = f"# {language['multi_doc_title'].format(index=dashboard_index)}\n\n"
|
| 447 |
+
combined_sections = dashboard_header + combined_sections
|
| 448 |
+
|
| 449 |
+
final_report = create_comprehensive_report(client, model, combined_sections, full_text, language, goal_description)
|
| 450 |
+
|
| 451 |
+
# If dashboard index is provided, prepend it to the report
|
| 452 |
+
if dashboard_index is not None and dashboard_index > 1:
|
| 453 |
+
# Only add header if it doesn't already exist (might have been added by Claude)
|
| 454 |
+
if not final_report.startswith(f"# {language['multi_doc_title'].format(index=dashboard_index)}"):
|
| 455 |
+
final_report = f"# {language['multi_doc_title'].format(index=dashboard_index)}\n\n{final_report}"
|
| 456 |
+
|
| 457 |
+
progress_tracker.update(dashboard_progress_base + dashboard_progress_step * 0.9, f"Finalizing dashboard{dashboard_marker} analysis...")
|
| 458 |
+
return final_report, combined_sections
|
| 459 |
+
|
| 460 |
+
def get_available_models(api_key):
|
| 461 |
+
"""Get available models from OpenRouter API."""
|
| 462 |
+
try:
|
| 463 |
+
headers = {
|
| 464 |
+
"Authorization": f"Bearer {api_key}",
|
| 465 |
+
"Content-Type": "application/json"
|
| 466 |
+
}
|
| 467 |
+
response = requests.get("https://openrouter.ai/api/v1/models", headers=headers)
|
| 468 |
+
if response.status_code == 200:
|
| 469 |
+
models_data = response.json()
|
| 470 |
+
available_models = [model["id"] for model in models_data.get("data", [])]
|
| 471 |
+
# Sort models to keep popular ones at the top
|
| 472 |
+
sorted_models = [model for model in OPENROUTER_MODELS if model in available_models]
|
| 473 |
+
# Add any additional models not in our predefined list
|
| 474 |
+
additional_models = [model for model in available_models if model not in OPENROUTER_MODELS]
|
| 475 |
+
additional_models.sort()
|
| 476 |
+
all_models = sorted_models + additional_models
|
| 477 |
+
return all_models
|
| 478 |
+
else:
|
| 479 |
+
print(f"Error fetching models: {response.status_code}")
|
| 480 |
+
return OPENROUTER_MODELS
|
| 481 |
+
except Exception as e:
|
| 482 |
+
print(f"Error fetching models: {str(e)}")
|
| 483 |
+
return OPENROUTER_MODELS
|
| 484 |
+
|
| 485 |
+
def process_multiple_dashboards(api_key, pdf_files, language_code="it", goal_description=None, num_sections=4, model_name=DEFAULT_MODEL, custom_model=None):
|
| 486 |
+
"""Process multiple dashboard PDFs and create individual and comparative reports."""
|
| 487 |
+
# Start progress tracking
|
| 488 |
+
progress_tracker.start_processing()
|
| 489 |
+
progress_tracker.total_dashboards = len(pdf_files)
|
| 490 |
+
|
| 491 |
+
# Step 1: Initialize language settings and API client
|
| 492 |
+
progress_tracker.update(1, "Initializing analysis...")
|
| 493 |
+
language = None
|
| 494 |
+
for lang_key, lang_data in SUPPORTED_LANGUAGES.items():
|
| 495 |
+
if lang_data['code'] == language_code:
|
| 496 |
+
language = lang_data
|
| 497 |
+
break
|
| 498 |
+
if not language:
|
| 499 |
+
print(f"β οΈ Language '{language_code}' not supported. Using Italian as fallback.")
|
| 500 |
+
language = SUPPORTED_LANGUAGES['italiano']
|
| 501 |
+
print(f"π Selected language: {language['name']}")
|
| 502 |
+
|
| 503 |
+
if not api_key:
|
| 504 |
+
progress_tracker.update(100, "β οΈ Error: API key not provided.")
|
| 505 |
+
progress_tracker.end_processing()
|
| 506 |
+
print("β οΈ Error: API key not provided.")
|
| 507 |
+
return None, None, "Error: API key not provided."
|
| 508 |
+
|
| 509 |
+
try:
|
| 510 |
+
client = OpenRouterClient(api_key=api_key)
|
| 511 |
+
print("β
OpenRouter client initialized successfully.")
|
| 512 |
+
except Exception as e:
|
| 513 |
+
progress_tracker.update(100, f"β Error initializing client: {str(e)}")
|
| 514 |
+
progress_tracker.end_processing()
|
| 515 |
+
print(f"β Error initializing client: {str(e)}")
|
| 516 |
+
return None, None, f"Error: {str(e)}"
|
| 517 |
+
|
| 518 |
+
# Determine which model to use
|
| 519 |
+
model = custom_model if model_name == "custom" and custom_model else model_name
|
| 520 |
+
print(f"π€ Using model: {model}")
|
| 521 |
+
|
| 522 |
+
# Step 2: Process each dashboard individually
|
| 523 |
+
individual_reports = []
|
| 524 |
+
individual_analyses = []
|
| 525 |
+
|
| 526 |
+
for i, pdf_bytes in enumerate(pdf_files):
|
| 527 |
+
dashboard_progress_base = (i / len(pdf_files) * 80) # 80% of progress for dashboard analysis
|
| 528 |
+
progress_tracker.update(dashboard_progress_base, f"Processing dashboard {i+1}/{len(pdf_files)}...")
|
| 529 |
+
print(f"\n{'#'*60}")
|
| 530 |
+
print(f"Processing dashboard {i+1}/{len(pdf_files)}...")
|
| 531 |
+
|
| 532 |
+
report, analysis = analyze_vertical_dashboard(
|
| 533 |
+
client=client,
|
| 534 |
+
model=model,
|
| 535 |
+
pdf_bytes=pdf_bytes,
|
| 536 |
+
language=language,
|
| 537 |
+
goal_description=goal_description,
|
| 538 |
+
num_sections=num_sections,
|
| 539 |
+
dashboard_index=i+1
|
| 540 |
+
)
|
| 541 |
+
|
| 542 |
+
if report:
|
| 543 |
+
individual_reports.append(report)
|
| 544 |
+
individual_analyses.append(analysis)
|
| 545 |
+
print(f"β
Analysis of dashboard {i+1} completed.")
|
| 546 |
+
else:
|
| 547 |
+
print(f"β Analysis of dashboard {i+1} failed.")
|
| 548 |
+
|
| 549 |
+
# For Hugging Face Space: use tmp directory for file output
|
| 550 |
+
tmp_dir = "/tmp"
|
| 551 |
+
if not os.path.exists(tmp_dir):
|
| 552 |
+
os.makedirs(tmp_dir, exist_ok=True)
|
| 553 |
+
|
| 554 |
+
# Step 3: Generate output files
|
| 555 |
+
progress_tracker.update(80, "Generating output files...")
|
| 556 |
+
timestamp = time.strftime("%Y%m%d_%H%M%S")
|
| 557 |
+
output_files = []
|
| 558 |
+
|
| 559 |
+
# Create individual report files
|
| 560 |
+
for i, report in enumerate(individual_reports):
|
| 561 |
+
file_progress = 80 + (i / len(individual_reports) * 10) # 10% for creating files
|
| 562 |
+
progress_tracker.update(file_progress, f"Creating files for dashboard {i+1}...")
|
| 563 |
+
|
| 564 |
+
md_filename = os.path.join(tmp_dir, f"dashboard_{i+1}_{language['code']}_{timestamp}.md")
|
| 565 |
+
pdf_filename = os.path.join(tmp_dir, f"dashboard_{i+1}_{language['code']}_{timestamp}.pdf")
|
| 566 |
+
|
| 567 |
+
with open(md_filename, 'w', encoding='utf-8') as f:
|
| 568 |
+
f.write(report)
|
| 569 |
+
output_files.append(md_filename)
|
| 570 |
+
|
| 571 |
+
try:
|
| 572 |
+
pdf_path = markdown_to_pdf(report, pdf_filename, language)
|
| 573 |
+
output_files.append(pdf_filename)
|
| 574 |
+
except Exception as e:
|
| 575 |
+
print(f"β οΈ Error converting dashboard {i+1} to PDF: {str(e)}")
|
| 576 |
+
|
| 577 |
+
# If there are multiple dashboards, create a comparative report
|
| 578 |
+
if len(individual_reports) > 1:
|
| 579 |
+
progress_tracker.update(90, "Creating comparative analysis...")
|
| 580 |
+
print("\n" + "#"*60)
|
| 581 |
+
print("Creating comparative analysis of all dashboards...")
|
| 582 |
+
|
| 583 |
+
# Combined report content
|
| 584 |
+
all_reports_content = "\n\n".join(individual_reports)
|
| 585 |
+
|
| 586 |
+
# Generate comparative analysis
|
| 587 |
+
comparative_report = create_multi_dashboard_comparative_report(
|
| 588 |
+
client=client,
|
| 589 |
+
model=model,
|
| 590 |
+
individual_reports=all_reports_content,
|
| 591 |
+
language=language,
|
| 592 |
+
goal_description=goal_description
|
| 593 |
+
)
|
| 594 |
+
|
| 595 |
+
# Save comparative report
|
| 596 |
+
progress_tracker.update(95, "Saving comparative analysis files...")
|
| 597 |
+
comparative_md = os.path.join(tmp_dir, f"comparative_analysis_{language['code']}_{timestamp}.md")
|
| 598 |
+
comparative_pdf = os.path.join(tmp_dir, f"comparative_analysis_{language['code']}_{timestamp}.pdf")
|
| 599 |
+
|
| 600 |
+
with open(comparative_md, 'w', encoding='utf-8') as f:
|
| 601 |
+
f.write(comparative_report)
|
| 602 |
+
output_files.append(comparative_md)
|
| 603 |
+
|
| 604 |
+
try:
|
| 605 |
+
pdf_path = markdown_to_pdf(comparative_report, comparative_pdf, language)
|
| 606 |
+
output_files.append(comparative_pdf)
|
| 607 |
+
except Exception as e:
|
| 608 |
+
print(f"β οΈ Error converting comparative report to PDF: {str(e)}")
|
| 609 |
+
|
| 610 |
+
# Complete progress tracking
|
| 611 |
+
progress_tracker.update(100, "β
Analysis completed successfully!")
|
| 612 |
+
progress_tracker.end_processing()
|
| 613 |
+
|
| 614 |
+
# Return the combined report content and all output files
|
| 615 |
+
combined_content = "\n\n---\n\n".join(individual_reports)
|
| 616 |
+
if len(individual_reports) > 1 and 'comparative_report' in locals():
|
| 617 |
+
combined_content += f"\n\n{'='*80}\n\n# COMPARATIVE ANALYSIS\n\n{comparative_report}"
|
| 618 |
+
return combined_content, output_files, "β
Analysis completed successfully!"
|
| 619 |
|
| 620 |
# Wrapper function for Gradio interface
|
| 621 |
def process_dashboard(api_key, pdf_files, language_name, goal_description=None, num_sections=4, model_name=DEFAULT_MODEL, custom_model=None):
|