File size: 1,056 Bytes
59b55bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import gradio as gr
import requests
import os

SERPAPI_KEY = os.getenv("SERPAPI_KEY")  # Secret API key from Hugging Face Space settings

def search_web(query):
    url = "https://serpapi.com/search"
    params = {
        "q": query,
        "api_key": SERPAPI_KEY,
        "engine": "google"
    }
    response = requests.get(url, params=params)
    
    if response.status_code != 200:
        return "Error: Could not retrieve results."
    
    results = response.json()
    output = ""
    for result in results.get("organic_results", [])[:5]:
        title = result.get("title")
        link = result.get("link")
        snippet = result.get("snippet", "")
        output += f"๐Ÿ”น {title}\n{snippet}\n๐Ÿ”— {link}\n\n"
    
    return output if output else "No results found."

demo = gr.Interface(
    fn=search_web,
    inputs=gr.Textbox(label="Search Query"),
    outputs=gr.Textbox(label="Top Results"),
    title="๐ŸŒ Internet Search App",
    description="Enter any search query and get live results using SerpAPI + Google."
)

demo.launch()