|
import gradio as gr |
|
import requests |
|
import os |
|
|
|
SERPAPI_KEY = os.getenv("SERPAPI_KEY") |
|
|
|
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() |
|
|