Spaces:
Paused
Paused
Download backend/api/research.py from Luca448/APP-Backend: direct link, hf CLI and curl.
- Browser
- Download file 1.78 kB
-
https://huggingface.co/spaces/Luca448/APP-Backend/resolve/main/backend/api/research.py
- Command line
-
hf download hf://spaces/Luca448/APP-Backend/backend/api/research.py
-
curl -L -o research.py https://huggingface.co/spaces/Luca448/APP-Backend/resolve/main/backend/api/research.py
1.78 kB
| from fastapi import APIRouter, HTTPException, Query | |
| from pydantic import BaseModel | |
| import requests | |
| from bs4 import BeautifulSoup | |
| from duckduckgo_search import DDGS | |
| router = APIRouter() | |
| class SearchResponse(BaseModel): | |
| results: list | |
| error: str = None | |
| def perform_search(q: str = Query(..., description="The search query")): | |
| """ | |
| Performs a web search using DuckDuckGo. | |
| """ | |
| try: | |
| with DDGS() as ddgs: | |
| results = list(ddgs.text(q, max_results=10)) | |
| return SearchResponse(results=results) | |
| except Exception as e: | |
| return SearchResponse(results=[], error=str(e)) | |
| def perform_scrape(url: str = Query(..., description="The URL to scrape")): | |
| """ | |
| Scrapes the text content of a given URL using Jina Reader (or fallback). | |
| """ | |
| try: | |
| # We can use jina.ai for extremely clean markdown extraction | |
| jina_url = f"https://r.jina.ai/{url}" | |
| response = requests.get(jina_url, timeout=15) | |
| response.raise_for_status() | |
| return {"text": response.text} | |
| except Exception as e: | |
| return {"error": str(e)} | |
| def perform_shopping(q: str = Query(..., description="The shopping search query")): | |
| """ | |
| Performs a shopping search. Since DDG doesn't have a native shopping API in the python package, | |
| we can use a fallback or simply search with "kaufen" appended. | |
| (This can be expanded later). | |
| """ | |
| try: | |
| with DDGS() as ddgs: | |
| # Simple text search for now, could parse images/prices if needed | |
| results = list(ddgs.text(q + " kaufen", max_results=5)) | |
| return {"results": results} | |
| except Exception as e: | |
| return {"error": str(e)} | |