CoolFace
Apppublic

sneha0428/web-research-agent

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py33 linesDownload Raw Back to root
1import gradio as gr2from googleapiclient.discovery import build3 4# Google Custom Search credentials5api_key = "AIzaSyCnrkc1Ri6WWji_zVZDainxBbRZfv1TYck"  # Replace with your API key6cse_id = "106709730045649a5"    # Replace with your Custom Search Engine ID7 8# Function to perform search using Google Custom Search API9def google_search(query):10    try:11        service = build("customsearch", "v1", developerKey=api_key)  # Setup Google Custom Search API12        res = service.cse().list(q=query, cx=cse_id).execute()  # Perform search13 14        # Check if results are returned15        if "items" in res:16            results = [{"title": item["title"], "snippet": item["snippet"], "url": item["link"]} for item in res["items"][:3]]17            return "\n\n".join([f"**{r['title']}**\n{r['snippet']}\n๐Ÿ”— [Link]({r['url']})" for r in results])18        else:19            return "No results found or blocked by Google."20    except Exception as e:21        return f"Error: {str(e)}"22 23# Gradio Web Interface24demo = gr.Interface(25    fn=google_search,  # The function that will be called when the user submits a query26    inputs=gr.Textbox(lines=2, placeholder="Enter your research query..."),  # Input box for queries27    outputs=gr.Markdown(), # Text output will display the search results28    title="Web Research Agent",  # Title of the app29    description="Ask any research question. The agent will search and summarize the answer.",  # Description of the app30)31 32demo.launch()33