Fix Gradio interface to properly handle query processing and search execution
This commit is contained in:
parent
bdc3a8fe60
commit
124aa109a1
|
@ -0,0 +1,55 @@
|
|||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for the UI search functionality.
|
||||
This script tests the search functionality of the UI without launching the Gradio interface.
|
||||
"""
|
||||
|
||||
import os
|
||||
import json
|
||||
from pathlib import Path
|
||||
from ui.gradio_interface import GradioInterface
|
||||
|
||||
|
||||
def main():
|
||||
"""Main function to test the UI search functionality."""
|
||||
# Create the interface
|
||||
interface = GradioInterface()
|
||||
|
||||
# Test queries
|
||||
test_queries = [
|
||||
"What are the latest advancements in quantum computing?",
|
||||
"Compare transformer and RNN architectures for NLP tasks",
|
||||
"Explain the environmental impact of electric vehicles"
|
||||
]
|
||||
|
||||
# Test each query
|
||||
for query in test_queries:
|
||||
print(f"\n\n{'=' * 80}")
|
||||
print(f"Testing query: {query}")
|
||||
print(f"{'=' * 80}\n")
|
||||
|
||||
# Process the query
|
||||
markdown_results, results_file = interface.process_query(query, num_results=5)
|
||||
|
||||
# Print the results
|
||||
print(f"\nResults file: {results_file}")
|
||||
print(f"\nMarkdown results preview:")
|
||||
print(f"{markdown_results[:500]}...\n")
|
||||
|
||||
# Check if results file exists and has content
|
||||
if results_file and os.path.exists(results_file):
|
||||
with open(results_file, 'r') as f:
|
||||
results = json.load(f)
|
||||
print(f"Number of results: {len(results)}")
|
||||
|
||||
if len(results) > 0:
|
||||
print(f"First result title: {results[0].get('title', 'No title')}")
|
||||
print(f"First result URL: {results[0].get('url', 'No URL')}")
|
||||
else:
|
||||
print("No results file or empty results.")
|
||||
|
||||
print("\nTest completed.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
|
@ -42,29 +42,57 @@ class GradioInterface:
|
|||
"""
|
||||
try:
|
||||
# Process the query
|
||||
print(f"Processing query: {query}")
|
||||
processed_query = self.query_processor.process_query(query)
|
||||
print(f"Processed query: {processed_query}")
|
||||
|
||||
# Add search engines if not specified
|
||||
if 'search_engines' not in processed_query:
|
||||
available_engines = self.search_executor.get_available_search_engines()
|
||||
processed_query['search_engines'] = available_engines
|
||||
print(f"Using search engines: {available_engines}")
|
||||
|
||||
# Execute the search
|
||||
search_results = self.search_executor.execute_search(processed_query)
|
||||
print(f"Executing search...")
|
||||
search_results = self.search_executor.execute_search(
|
||||
structured_query=processed_query,
|
||||
num_results=num_results
|
||||
)
|
||||
print(f"Search results: {search_results}")
|
||||
|
||||
# Process the results
|
||||
print(f"Processing results...")
|
||||
processed_results = self.result_collector.process_results(
|
||||
search_results, dedup=True, max_results=num_results
|
||||
)
|
||||
print(f"Processed {len(processed_results)} results")
|
||||
|
||||
# Save results to file
|
||||
timestamp = int(time.time())
|
||||
results_file = self.results_dir / f"results_{timestamp}.json"
|
||||
with open(results_file, "w") as f:
|
||||
json.dump(processed_results, f, indent=2)
|
||||
|
||||
# Ensure the results are not empty before saving
|
||||
if processed_results:
|
||||
with open(results_file, "w") as f:
|
||||
json.dump(processed_results, f, indent=2)
|
||||
print(f"Results saved to {results_file}")
|
||||
file_path = str(results_file)
|
||||
else:
|
||||
error_message = "No results found. Please try a different query or check API keys."
|
||||
print(error_message)
|
||||
file_path = None
|
||||
return f"## No Results Found\n\n{error_message}", file_path
|
||||
|
||||
# Format results for display
|
||||
markdown_results = self._format_results_as_markdown(processed_results)
|
||||
|
||||
return markdown_results, str(results_file)
|
||||
return markdown_results, file_path
|
||||
|
||||
except Exception as e:
|
||||
error_message = f"Error processing query: {str(e)}"
|
||||
print(f"ERROR: {error_message}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return f"## Error\n\n{error_message}", None
|
||||
|
||||
def _format_results_as_markdown(self, results):
|
||||
|
|
Loading…
Reference in New Issue