WebSearch / app.py
Nishanth88's picture
Update 1 app.py
6eb6008 verified
raw
history blame
4.2 kB
import gradio as gr
from huggingface_hub import InferenceClient
from smolagents import CodeAgent, DuckDuckGoSearchTool, FinalAnswerTool, VisitWebpageTool, HfApiModel
# Initialize HuggingFace client
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
# Create the smolagents agent (without UserInputTool since we'll get input from Gradio)
agent = CodeAgent(
tools=[DuckDuckGoSearchTool(), VisitWebpageTool(), FinalAnswerTool()],
model=HfApiModel(),
max_steps=8,
verbosity_level=1
)
# Function to perform web research with a provided query
def research_with_query(query):
result = agent.run(f"""
Think step by step:
1. The user has asked about: "{query}"
2. Use the DuckDuckGoSearchTool to search the web for information about this query.
3. From the search results, identify 1-2 relevant webpage URLs that might contain detailed information.
4. Use the VisitWebpageTool to visit each identified webpage and extract its content.
5. Combine the information from the search results and webpage visits.
6. Create a comprehensive bullet point summary of all collected information.
7. Each bullet point should start with "• " and be on a new line.
8. Use the FinalAnswerTool to present your bullet-point summary as the final answer.
Make sure your bullet points are clear, well-organized, and directly relevant to the user's query.
Include the most important and factual information from your research.
""")
return result
def respond(
message,
history: list[tuple[str, str]],
system_message,
max_tokens,
temperature,
top_p,
use_web_search,
):
# Check if web search is enabled and message starts with a research request
if use_web_search and message.strip().lower().startswith(("search:", "research:", "find info:")):
query = message.split(":", 1)[1].strip()
yield "Searching the web for information about your query. This may take a moment..."
try:
# Perform the web search and get bullet point summary
research_results = research_with_query(query)
# Return the research results
yield f"Here's what I found about '{query}':\n\n{research_results}"
except Exception as e:
yield f"Sorry, I encountered an error while searching the web: {str(e)}"
else:
# Regular chat completion for normal messages
messages = [{"role": "system", "content": system_message}]
for val in history:
if val[0]:
messages.append({"role": "user", "content": val[0]})
if val[1]:
messages.append({"role": "assistant", "content": val[1]})
messages.append({"role": "user", "content": message})
response = ""
for message in client.chat_completion(
messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
token = message.choices[0].delta.content
response += token
yield response
# Create the Gradio interface
demo = gr.ChatInterface(
respond,
additional_inputs=[
gr.Textbox(
value="You are a helpful assistant. When users ask you to search for information with 'search:', 'research:', or 'find info:', you will search the web for them.",
label="System message"
),
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.95,
step=0.05,
label="Top-p (nucleus sampling)",
),
gr.Checkbox(value=True, label="Enable web search (use 'search:', 'research:', or 'find info:' to search)")
],
examples=[
["search: latest developments in quantum computing"],
["research: climate change impacts in 2023"],
["find info: benefits of meditation"],
["Hello! How are you today?"]
],
)
if __name__ == "__main__":
demo.launch()