basitccp's picture
Create app.py
59b55bf verified
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()