|
|
|
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!" |
|
|
|
|
|
def process_dashboard(api_key, pdf_files, language_name, goal_description=None, num_sections=4, model_name=DEFAULT_MODEL, custom_model=None): |
|
"""Process dashboard PDFs and generate reports (wrapper function for Gradio interface).""" |
|
|
|
progress_thread = threading.Thread(target=update_progress) |
|
progress_thread.daemon = True |
|
progress_thread.start() |
|
|
|
|
|
language_code = "en" |
|
for lang_key, lang_data in SUPPORTED_LANGUAGES.items(): |
|
if lang_data['name'].lower() == language_name.lower(): |
|
language_code = lang_data['code'] |
|
break |
|
|
|
|
|
pdf_bytes_list = [] |
|
if pdf_files is not None: |
|
for pdf_file in pdf_files: |
|
if isinstance(pdf_file, dict) and 'name' in pdf_file: |
|
|
|
with open(pdf_file['name'], 'rb') as f: |
|
pdf_bytes_list.append(f.read()) |
|
elif isinstance(pdf_file, str): |
|
|
|
with open(pdf_file, 'rb') as f: |
|
pdf_bytes_list.append(f.read()) |
|
else: |
|
|
|
try: |
|
if hasattr(pdf_file, 'read'): |
|
pdf_bytes_list.append(pdf_file.read()) |
|
except Exception as e: |
|
print(f"Error reading PDF file: {str(e)}") |
|
|
|
|
|
try: |
|
combined_content, output_files, status = process_multiple_dashboards( |
|
api_key=api_key, |
|
pdf_files=pdf_bytes_list, |
|
language_code=language_code, |
|
goal_description=goal_description, |
|
num_sections=num_sections, |
|
model_name=model_name, |
|
custom_model=custom_model |
|
) |
|
return combined_content, output_files, status |
|
except Exception as e: |
|
error_message = f"Error processing dashboards: {str(e)}" |
|
print(error_message) |
|
progress_tracker.update(100, error_message) |
|
progress_tracker.end_processing() |
|
return None, None, error_message |
|
|
|
|
|
def toggle_custom_model(choice): |
|
"""Toggle visibility of custom model input based on selection.""" |
|
return {"visible": choice == "custom"} |
|
|
|
def refresh_models(api_key): |
|
"""Refresh the list of available models based on the API key.""" |
|
if not api_key: |
|
return gr.Dropdown(choices=["custom"] + OPENROUTER_MODELS, value=DEFAULT_MODEL) |
|
|
|
try: |
|
available_models = get_available_models(api_key) |
|
return gr.Dropdown(choices=available_models, value=DEFAULT_MODEL) |
|
except Exception as e: |
|
print(f"Error refreshing models: {str(e)}") |
|
return gr.Dropdown(choices=["custom"] + OPENROUTER_MODELS, value=DEFAULT_MODEL) |
|
|
|
|
|
with gr.Blocks(title="Dashboard Narrator - Powered by OpenRouter.ai", theme=gr.themes.Soft()) as demo: |
|
gr.Markdown(""" |
|
# 📊 Dashboard Narrator - Powered by OpenRouter.ai |
|
Unlock the hidden stories in your dashboards!<br> |
|
Dashboard Narrator leverages advanced AI models through OpenRouter.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="OpenRouter API Key", placeholder="Enter your OpenRouter API key...", type="password") |
|
refresh_btn = gr.Button("🔄 Refresh Available Models", size="sm") |
|
|
|
model_choice = gr.Dropdown( |
|
choices=["custom"] + OPENROUTER_MODELS, |
|
value=DEFAULT_MODEL, |
|
label="Select Model" |
|
) |
|
|
|
custom_model = gr.Textbox( |
|
label="Custom Model ID", |
|
placeholder="Enter custom model ID (e.g., anthropic/claude-3-opus:latest)...", |
|
visible=False |
|
) |
|
|
|
language = gr.Dropdown( |
|
choices=["Italiano", "English", "Français", "Español", "Deutsch"], |
|
value="English", |
|
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_bar = gr.Progress() |
|
|
|
|
|
model_choice.change( |
|
fn=toggle_custom_model, |
|
inputs=model_choice, |
|
outputs=custom_model, |
|
) |
|
|
|
|
|
refresh_btn.click( |
|
fn=refresh_models, |
|
inputs=api_key, |
|
outputs=model_choice, |
|
) |
|
|
|
|
|
analyze_btn.click( |
|
fn=process_dashboard, |
|
inputs=[api_key, pdf_files, language, goal, num_sections, model_choice, custom_model], |
|
outputs=[output_md, output_files, output_status] |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch() |