From 9cb9d4846690238255060f97db38a8a2abb98899 Mon Sep 17 00:00:00 2001 From: Steve White Date: Mon, 17 Mar 2025 12:20:54 -0500 Subject: [PATCH] Fix AttributeError in report progress callback by using direct value assignment instead of update method --- ui/gradio_interface.py | 148 +++++++++++++++++++++++++++++++++++------ 1 file changed, 126 insertions(+), 22 deletions(-) diff --git a/ui/gradio_interface.py b/ui/gradio_interface.py index b0c6f92..cf15262 100644 --- a/ui/gradio_interface.py +++ b/ui/gradio_interface.py @@ -40,6 +40,10 @@ class GradioInterface: # The report generator will be initialized in the async init method self.report_generator = None + + # Progress tracking elements (will be set in create_interface) + self.report_progress = None + self.report_progress_bar = None async def async_init(self): """Asynchronously initialize components that require async initialization.""" @@ -185,7 +189,8 @@ class GradioInterface: return markdown async def generate_report(self, query, detail_level="standard", query_type="auto-detect", custom_model=None, - results_file=None, process_thinking_tags=False, progress=gr.Progress()): + results_file=None, process_thinking_tags=False, initial_results=10, final_results=7, + progress=gr.Progress()): """ Generate a report for the given query. @@ -219,6 +224,14 @@ class GradioInterface: # Get detail level configuration config = self.detail_level_manager.get_detail_level_config(detail_level) + # Override num_results if provided + if initial_results: + config["initial_results_per_engine"] = initial_results + + # Set final results after reranking if provided + if final_results: + config["final_results_after_reranking"] = final_results + # If custom model is provided, use it if custom_model: config["model"] = custom_model @@ -257,9 +270,11 @@ class GradioInterface: ) # Execute the search with the structured query + # Use initial_results_per_engine if available, otherwise fall back to num_results + num_results_to_fetch = config.get("initial_results_per_engine", config.get("num_results", 10)) search_results_dict = self.search_executor.execute_search( structured_query, - num_results=config["num_results"] + num_results=num_results_to_fetch ) # Add debug logging @@ -324,28 +339,49 @@ class GradioInterface: # Rerank results if we have a reranker if hasattr(self, 'reranker') and self.reranker: + # Use final_results_after_reranking if available, otherwise fall back to num_results + top_n_results = config.get("final_results_after_reranking", config.get("num_results", 7)) search_results = self.reranker.rerank_with_metadata( query, search_results, document_key='snippet', - top_n=config["num_results"] + top_n=top_n_results ) # Set up progress tracking - self.progress_status = "Preparing documents..." - self.progress_value = 0 - self.progress_total = 1 # Will be updated when we know the total chunks - # Define progress callback function def progress_callback(current_progress, total_chunks, current_report): - self.progress_value = current_progress - self.progress_total = total_chunks - # Update the progress bar - progress(current_progress) + # Calculate current chunk number + current_chunk = int(current_progress * total_chunks) if total_chunks > 0 else 0 + + # Determine the status message based on progress + if current_progress == 0: + status_message = "Preparing documents..." + elif current_progress >= 1.0: + status_message = "Finalizing report..." + else: + status_message = f"Processing chunk {current_chunk}/{total_chunks}..." + + # Add current chunk title if available + if hasattr(self.report_generator, 'current_chunk_title'): + chunk_title = self.report_generator.current_chunk_title + if chunk_title: + status_message += f" ({chunk_title})" + + # Update the progress status directly + return status_message # Set the progress callback for the report generator if hasattr(self.report_generator, 'set_progress_callback'): - self.report_generator.set_progress_callback(progress_callback) + # Create a wrapper function that updates the UI elements + def ui_progress_callback(current_progress, total_chunks, current_report): + status_message = progress_callback(current_progress, total_chunks, current_report) + # Update the UI elements directly - use value assignment instead of update method + self.report_progress.value = status_message + self.report_progress_bar.value = int(current_progress * 100) + return status_message + + self.report_generator.set_progress_callback(ui_progress_callback) # Generate the report print(f"Generating report with {len(search_results)} search results") @@ -358,8 +394,9 @@ class GradioInterface: else: self.progress_status = "Processing document chunks..." - # Initial progress update - progress(0) + # Set up initial progress state + self.report_progress.value = "Preparing documents..." + self.report_progress_bar.value = 0 # Handle query_type parameter actual_query_type = None @@ -556,7 +593,7 @@ class GradioInterface: info="Controls the depth and breadth of the report" ) report_query_type = gr.Dropdown( - choices=["auto-detect", "factual", "exploratory", "comparative"], + choices=["auto-detect", "factual", "exploratory", "comparative", "code"], value="auto-detect", label="Query Type", info="Type of query determines the report structure" @@ -568,12 +605,63 @@ class GradioInterface: label="Custom Model (Optional)", info="Select a custom model for report generation" ) - report_process_thinking = gr.Checkbox( - label="Process Thinking Tags", - value=False, - info="Process tags in model output" + + with gr.Row(): + with gr.Column(): + gr.Markdown("### Advanced Settings") + + with gr.Row(): + with gr.Column(): + with gr.Accordion("Search Parameters", open=False): + with gr.Row(): + initial_results_slider = gr.Slider( + minimum=5, + maximum=50, + value=10, + step=5, + label="Initial Results Per Engine", + info="Number of results to fetch from each search engine" + ) + final_results_slider = gr.Slider( + minimum=3, + maximum=30, + value=7, + step=1, + label="Final Results After Reranking", + info="Number of results to keep after reranking" + ) + + with gr.Accordion("Processing Options", open=False): + with gr.Row(): + report_process_thinking = gr.Checkbox( + label="Process Thinking Tags", + value=False, + info="Process tags in model output" + ) + + with gr.Row(): + report_button = gr.Button("Generate Report", variant="primary", size="lg") + + with gr.Row(): + with gr.Column(): + # Progress indicator that will be updated by the progress callback + self.report_progress = gr.Textbox( + label="Progress Status", + value="Ready", + interactive=False + ) + + with gr.Row(): + with gr.Column(): + # Progress bar to show visual progress + self.report_progress_bar = gr.Slider( + minimum=0, + maximum=100, + value=0, + step=1, + label="Progress", + interactive=False ) - report_button = gr.Button("Generate Report", variant="primary") gr.Examples( examples=[ @@ -609,6 +697,7 @@ class GradioInterface: - **factual**: For queries seeking specific information (e.g., "What is...", "How does...") - **exploratory**: For queries investigating a topic broadly (e.g., "Tell me about...") - **comparative**: For queries comparing multiple items (e.g., "Compare X and Y", "Differences between...") + - **code**: For queries related to programming, software development, or technical implementation """ gr.Markdown(f"### Detail Levels\n{detail_levels_info}") @@ -621,10 +710,25 @@ class GradioInterface: outputs=[search_results_output, search_file_output] ) + # Connect the progress callback to the report button + def update_progress_display(progress_value, status_message): + percentage = int(progress_value * 100) + return status_message, percentage + + # Update the progress tracking in the generate_report method + async def generate_report_with_progress(query, detail_level, query_type, model_name, rerank, token_budget, initial_results, final_results): + # Set up progress tracking + progress_data = gr.Progress(track_tqdm=True) + + # Call the original generate_report method + result = await self.generate_report(query, detail_level, query_type, model_name, rerank, token_budget, initial_results, final_results) + + return result + report_button.click( - fn=lambda q, d, t, m, r, p: asyncio.run(self.generate_report(q, d, t, m, r, p)), + fn=lambda q, d, t, m, r, p, i, f: asyncio.run(generate_report_with_progress(q, d, t, m, r, p, i, f)), inputs=[report_query_input, report_detail_level, report_query_type, report_custom_model, - search_file_output, report_process_thinking], + search_file_output, report_process_thinking, initial_results_slider, final_results_slider], outputs=[report_output, report_file_output] )