import gradio as gr from search_engine import search def format_results(results): """Format search results in a user-friendly way""" if 'error' in results: return f"❌ Error: {results['error']}" output = [] # Add insights section if 'insights' in results and results['insights']: output.append("# 💡 Key Insights\n") output.append(results['insights']) output.append("\n") # Add key points section if 'key_points' in results and results['key_points']: output.append("# 🎯 Key Points\n") for i, point in enumerate(results['key_points'], 1): output.append(f"{i}. {point}\n") output.append("\n") # Add detailed results section if 'results' in results and results['results']: output.append("# 📄 Detailed Results\n") for i, result in enumerate(results['results'], 1): output.append(f"## {i}. [{result['title']}]({result['url']})\n") if 'description' in result and result['description']: output.append(f"*{result['description']}*\n") if 'summary' in result and result['summary']: output.append(f"{result['summary']}\n") if 'key_points' in result and result['key_points']: output.append("\nHighlights:\n") for point in result['key_points']: output.append(f"- {point}\n") output.append("\n") # Add follow-up questions section if 'follow_up_questions' in results and results['follow_up_questions']: output.append("# ❓ Related Questions\n") for question in results['follow_up_questions']: output.append(f"- {question}\n") return "\n".join(output) def search_and_format(query): """Search and format results""" try: results = search(query) return format_results(results) except Exception as e: return f"❌ Error: {str(e)}" # Create the Gradio interface interface = gr.Interface( fn=search_and_format, inputs=gr.Textbox( label="Enter your search query", placeholder="What would you like to learn about?", lines=2 ), outputs=gr.Markdown( label="Search Results", show_label=True ), title="🔍 AI-Powered Web Search", description=""" This search engine uses AI to: - Find relevant web pages - Extract key information - Generate insights and summaries - Suggest follow-up questions """, examples=[ ["What is quantum computing?"], ["Latest developments in artificial intelligence"], ["How does blockchain technology work?"], ["Explain machine learning in simple terms"], ], theme=gr.themes.Soft() ) # Launch the app if __name__ == "__main__": interface.launch()