Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import urllib.parse
|
| 4 |
+
import requests
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from typing import Literal, List, Dict
|
| 7 |
+
|
| 8 |
+
JINA_API_BASE = "https://s.jina.ai" # Search endpoint
|
| 9 |
+
# For reading page content, you can also use r.jina.ai like: https://r.jina.ai/https://example.com
|
| 10 |
+
|
| 11 |
+
def _headers():
|
| 12 |
+
api_key = os.getenv("JINA_API_KEY")
|
| 13 |
+
headers = {
|
| 14 |
+
"Accept": "application/json",
|
| 15 |
+
}
|
| 16 |
+
if api_key:
|
| 17 |
+
headers["Authorization"] = f"Bearer {api_key}"
|
| 18 |
+
return headers
|
| 19 |
+
|
| 20 |
+
def search(input_query: str, max_results: int = 5) -> List[Dict[Literal["snippet", "title", "link"], str]]:
|
| 21 |
+
"""
|
| 22 |
+
Perform a web search using Jina AI (s.jina.ai).
|
| 23 |
+
|
| 24 |
+
Args:
|
| 25 |
+
input_query: The query to search for.
|
| 26 |
+
max_results: The maximum number of results to return. Defaults to 5.
|
| 27 |
+
|
| 28 |
+
Returns:
|
| 29 |
+
A list of dictionaries with "snippet", "title", and "link".
|
| 30 |
+
"""
|
| 31 |
+
# Jina Search accepts the query as part of the path; use JSON response for structured data
|
| 32 |
+
# URL-encode the query to be safe in the path segment
|
| 33 |
+
encoded = urllib.parse.quote(input_query, safe="")
|
| 34 |
+
url = f"{JINA_API_BASE}/{encoded}"
|
| 35 |
+
|
| 36 |
+
# Request JSON output; Jina returns the top results with metadata when Accept: application/json is set
|
| 37 |
+
resp = requests.get(url, headers=_headers(), timeout=30)
|
| 38 |
+
resp.raise_for_status()
|
| 39 |
+
|
| 40 |
+
data = resp.json()
|
| 41 |
+
# The JSON shape from s.jina.ai includes results with title, url, and snippet-like content
|
| 42 |
+
# We map to a compact schema: title, link, snippet
|
| 43 |
+
items = []
|
| 44 |
+
# Common keys seen include "results" or "data" depending on mode; prefer "results" if present
|
| 45 |
+
results = []
|
| 46 |
+
if isinstance(data, dict):
|
| 47 |
+
if "results" in data and isinstance(data["results"], list):
|
| 48 |
+
results = data["results"]
|
| 49 |
+
elif "data" in data and isinstance(data["data"], list):
|
| 50 |
+
results = data["data"]
|
| 51 |
+
else:
|
| 52 |
+
# Fallback: if it's already a list
|
| 53 |
+
results = data if isinstance(data, list) else []
|
| 54 |
+
elif isinstance(data, list):
|
| 55 |
+
results = data
|
| 56 |
+
|
| 57 |
+
for r in results[:max_results]:
|
| 58 |
+
title = r.get("title") or r.get("headline") or ""
|
| 59 |
+
link = r.get("url") or r.get("link") or ""
|
| 60 |
+
# Prefer a concise snippet if present; otherwise fallback to any text/description
|
| 61 |
+
snippet = r.get("snippet") or r.get("description") or r.get("content") or ""
|
| 62 |
+
# Ensure strings
|
| 63 |
+
title = str(title) if title is not None else ""
|
| 64 |
+
link = str(link) if link is not None else ""
|
| 65 |
+
snippet = str(snippet) if snippet is not None else ""
|
| 66 |
+
if link or title or snippet:
|
| 67 |
+
items.append({"title": title, "link": link, "snippet": snippet})
|
| 68 |
+
|
| 69 |
+
return items
|
| 70 |
+
|
| 71 |
+
demo = gr.Interface(
|
| 72 |
+
fn=search,
|
| 73 |
+
inputs=[
|
| 74 |
+
gr.Textbox(value="Ahly SC of Egypt matches.", label="Search query"),
|
| 75 |
+
gr.Slider(minimum=1, maximum=20, value=5, step=1, label="Max results"),
|
| 76 |
+
],
|
| 77 |
+
outputs=gr.JSON(label="Search results"),
|
| 78 |
+
title="Web Searcher using Jina AI",
|
| 79 |
+
description="Search the web using Jina AI Search Foundation API (s.jina.ai).",
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
demo.launch(mcp_server=True)
|