|
import gradio as gr |
|
import pandas as pd |
|
from langchain_community.tools import DuckDuckGoSearchResults |
|
|
|
from utils.helpers import map_results |
|
|
|
|
|
async def duckduckgo_search(query: str, max_results: int = 5) -> pd.DataFrame: |
|
""" |
|
Given a search query, returns the search results from DuckDuckGo. |
|
|
|
Args: |
|
query (str): The search query. |
|
max_results (int, optional): The number of maximum results to return. Defaults to 5. |
|
|
|
Returns: |
|
str: The search results from DuckDuckGo Search Engine. |
|
""" |
|
ddg = DuckDuckGoSearchResults( |
|
output_format="list", |
|
) |
|
ddg.max_results = max_results |
|
results = await ddg.ainvoke(query) |
|
mapping = { |
|
"title": "title", |
|
"link": "link", |
|
"snippet": "body", |
|
} |
|
results = await map_results(results, mapping) |
|
return results |
|
|
|
|
|
duckduckgo_interface = gr.Interface( |
|
fn=duckduckgo_search, |
|
inputs=[ |
|
gr.Textbox(label="Search Query"), |
|
gr.Slider(minimum=1, maximum=10, step=1, value=5, label="Number of Results"), |
|
], |
|
outputs=gr.Dataframe( |
|
label="Search Results", |
|
headers=["title", "body", "link"], |
|
show_fullscreen_button=True, |
|
show_row_numbers=True, |
|
show_copy_button=True, |
|
wrap=True, |
|
), |
|
title="DuckDuckGo Search", |
|
description="Search the web using DuckDuckGo Search Engine.", |
|
) |
|
|