Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
-
|
| 2 |
|
|
|
|
| 3 |
def process_dashboard(api_key, pdf_files, language_name, goal_description=None, num_sections=4, model_name=DEFAULT_MODEL, custom_model=None):
|
| 4 |
"""Process dashboard PDFs and generate reports (wrapper function for Gradio interface)."""
|
| 5 |
# Start a thread to update progress
|
|
@@ -51,4 +52,109 @@ def process_dashboard(api_key, pdf_files, language_name, goal_description=None,
|
|
| 51 |
print(error_message)
|
| 52 |
progress_tracker.update(100, error_message)
|
| 53 |
progress_tracker.end_processing()
|
| 54 |
-
return None, None, error_message
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
return combined_content, output_files, "✅ Analysis completed successfully!"
|
| 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):
|
| 5 |
"""Process dashboard PDFs and generate reports (wrapper function for Gradio interface)."""
|
| 6 |
# Start a thread to update progress
|
|
|
|
| 52 |
print(error_message)
|
| 53 |
progress_tracker.update(100, error_message)
|
| 54 |
progress_tracker.end_processing()
|
| 55 |
+
return None, None, error_message
|
| 56 |
+
|
| 57 |
+
# Gradio Interface Functions
|
| 58 |
+
def toggle_custom_model(choice):
|
| 59 |
+
"""Toggle visibility of custom model input based on selection."""
|
| 60 |
+
return {"visible": choice == "custom"}
|
| 61 |
+
|
| 62 |
+
def refresh_models(api_key):
|
| 63 |
+
"""Refresh the list of available models based on the API key."""
|
| 64 |
+
if not api_key:
|
| 65 |
+
return gr.Dropdown(choices=["meta-llama/llama-4-scout:free", "custom"] + OPENROUTER_MODELS, value="meta-llama/llama-4-scout:free")
|
| 66 |
+
|
| 67 |
+
try:
|
| 68 |
+
available_models = get_available_models(api_key)
|
| 69 |
+
return gr.Dropdown(choices=["meta-llama/llama-4-scout:free", "custom"] + available_models, value="meta-llama/llama-4-scout:free")
|
| 70 |
+
except Exception as e:
|
| 71 |
+
print(f"Error refreshing models: {str(e)}")
|
| 72 |
+
return gr.Dropdown(choices=["meta-llama/llama-4-scout:free", "custom"] + OPENROUTER_MODELS, value="meta-llama/llama-4-scout:free")
|
| 73 |
+
|
| 74 |
+
# Define the Gradio interface
|
| 75 |
+
with gr.Blocks(title="Dashboard Narrator - Powered by OpenRouter.ai", theme=gr.themes.Soft()) as demo:
|
| 76 |
+
gr.Markdown("""
|
| 77 |
+
# 📊 Dashboard Narrator - Powered by OpenRouter.ai
|
| 78 |
+
Unlock the hidden stories in your dashboards!<br>
|
| 79 |
+
Dashboard Narrator leverages advanced AI models through OpenRouter.ai to dissect your PDF reports,<br>
|
| 80 |
+
analyze each segment with expert precision, and craft comprehensive insights in your preferred language.<br><br>
|
| 81 |
+
Turn complex data visualizations into clear, strategic recommendations and uncover trends you might have missed.<br>
|
| 82 |
+
From executive summaries to detailed breakdowns, get the full narrative behind your numbers in just a few clicks.<br><br>
|
| 83 |
+
""")
|
| 84 |
+
with gr.Row():
|
| 85 |
+
with gr.Column(scale=1):
|
| 86 |
+
api_key = gr.Textbox(label="OpenRouter API Key", placeholder="Enter your OpenRouter API key...", type="password")
|
| 87 |
+
refresh_btn = gr.Button("🔄 Refresh Available Models", size="sm")
|
| 88 |
+
|
| 89 |
+
model_choice = gr.Dropdown(
|
| 90 |
+
choices=["meta-llama/llama-4-scout:free", "custom"] + OPENROUTER_MODELS,
|
| 91 |
+
value="meta-llama/llama-4-scout:free",
|
| 92 |
+
label="Select Model"
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
custom_model = gr.Textbox(
|
| 96 |
+
label="Custom Model ID",
|
| 97 |
+
placeholder="Enter custom model ID (e.g., anthropic/claude-3-opus:latest)...",
|
| 98 |
+
visible=False
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
language = gr.Dropdown(
|
| 102 |
+
choices=["Italiano", "English", "Français", "Español", "Deutsch"],
|
| 103 |
+
value="English",
|
| 104 |
+
label="Report Language"
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
num_sections = gr.Slider(
|
| 108 |
+
minimum=2,
|
| 109 |
+
maximum=10,
|
| 110 |
+
value=4,
|
| 111 |
+
step=1,
|
| 112 |
+
label="Number of Vertical Sections per Dashboard"
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
goal = gr.Textbox(
|
| 116 |
+
label="Analysis Goal (optional)",
|
| 117 |
+
placeholder="E.g., Analyze Q1 2024 sales KPIs..."
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
pdf_files = gr.File(
|
| 121 |
+
label="Upload Dashboards (PDF)",
|
| 122 |
+
file_types=[".pdf"],
|
| 123 |
+
file_count="multiple"
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
analyze_btn = gr.Button("🔍 Analyze Dashboards", variant="primary")
|
| 127 |
+
|
| 128 |
+
with gr.Column(scale=2):
|
| 129 |
+
with gr.Tab("Report"):
|
| 130 |
+
output_md = gr.Markdown(label="Analysis Report", value="")
|
| 131 |
+
with gr.Tab("Output Files"):
|
| 132 |
+
output_files = gr.File(label="Download Files")
|
| 133 |
+
output_status = gr.Textbox(label="Progress", placeholder="Upload dashboards and press Analyze...", interactive=False)
|
| 134 |
+
# Progress component doesn't accept label in Gradio 5.21.0
|
| 135 |
+
progress_bar = gr.Progress()
|
| 136 |
+
|
| 137 |
+
# Handle model dropdown change
|
| 138 |
+
model_choice.change(
|
| 139 |
+
fn=toggle_custom_model,
|
| 140 |
+
inputs=model_choice,
|
| 141 |
+
outputs=custom_model,
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
# Handle refresh models button
|
| 145 |
+
refresh_btn.click(
|
| 146 |
+
fn=refresh_models,
|
| 147 |
+
inputs=api_key,
|
| 148 |
+
outputs=model_choice,
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
# Handle analyze button
|
| 152 |
+
analyze_btn.click(
|
| 153 |
+
fn=process_dashboard,
|
| 154 |
+
inputs=[api_key, pdf_files, language, goal, num_sections, model_choice, custom_model],
|
| 155 |
+
outputs=[output_md, output_files, output_status]
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
# Launch the app
|
| 159 |
+
if __name__ == "__main__":
|
| 160 |
+
demo.launch()
|