Spaces:
Runtime error
Runtime error
from fastapi import FastAPI, HTTPException | |
from fastapi.responses import JSONResponse | |
from webscout import WEBS, transcriber | |
from typing import Optional | |
from fastapi.encoders import jsonable_encoder | |
app = FastAPI() | |
async def root(): | |
return {"message": "API documentation can be found at /docs"} | |
async def health_check(): | |
return {"status": "OK"} | |
async def search( | |
q: str, | |
max_results: int = 10, | |
timelimit: Optional[str] = None, | |
safesearch: str = "moderate", | |
region: str = "wt-wt", | |
backend: str = "api" | |
): | |
"""Perform a text search.""" | |
try: | |
with WEBS() as webs: | |
results = webs.text(keywords=q, region=region, safesearch=safesearch, timelimit=timelimit, backend=backend, max_results=max_results) | |
return JSONResponse(content=jsonable_encoder(results)) | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=f"Error during search: {e}") | |
async def images( | |
q: str, | |
max_results: int = 10, | |
safesearch: str = "moderate", | |
region: str = "wt-wt", | |
timelimit: Optional[str] = None, | |
size: Optional[str] = None, | |
color: Optional[str] = None, | |
type_image: Optional[str] = None, | |
layout: Optional[str] = None, | |
license_image: Optional[str] = None | |
): | |
"""Perform an image search.""" | |
try: | |
with WEBS() as webs: | |
results = webs.images(keywords=q, region=region, safesearch=safesearch, timelimit=timelimit, size=size, color=color, type_image=type_image, layout=layout, license_image=license_image, max_results=max_results) | |
return JSONResponse(content=jsonable_encoder(results)) | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=f"Error during image search: {e}") | |
async def videos( | |
q: str, | |
max_results: int = 10, | |
safesearch: str = "moderate", | |
region: str = "wt-wt", | |
timelimit: Optional[str] = None, | |
resolution: Optional[str] = None, | |
duration: Optional[str] = None, | |
license_videos: Optional[str] = None | |
): | |
"""Perform a video search.""" | |
try: | |
with WEBS() as webs: | |
results = webs.videos(keywords=q, region=region, safesearch=safesearch, timelimit=timelimit, resolution=resolution, duration=duration, license_videos=license_videos, max_results=max_results) | |
return JSONResponse(content=jsonable_encoder(results)) | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=f"Error during video search: {e}") | |
async def news( | |
q: str, | |
max_results: int = 10, | |
safesearch: str = "moderate", | |
region: str = "wt-wt", | |
timelimit: Optional[str] = None | |
): | |
"""Perform a news search.""" | |
try: | |
with WEBS() as webs: | |
results = webs.news(keywords=q, region=region, safesearch=safesearch, timelimit=timelimit, max_results=max_results) | |
return JSONResponse(content=jsonable_encoder(results)) | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=f"Error during news search: {e}") | |
async def answers(q: str): | |
"""Get instant answers for a query.""" | |
try: | |
with WEBS() as webs: | |
results = webs.answers(keywords=q) | |
return JSONResponse(content=jsonable_encoder(results)) | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=f"Error getting instant answers: {e}") | |
async def suggestions(q: str, region: str = "wt-wt"): | |
"""Get search suggestions for a query.""" | |
try: | |
with WEBS() as webs: | |
results = webs.suggestions(keywords=q, region=region) | |
return JSONResponse(content=jsonable_encoder(results)) | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=f"Error getting search suggestions: {e}") | |
async def maps( | |
q: str, | |
place: Optional[str] = None, | |
street: Optional[str] = None, | |
city: Optional[str] = None, | |
county: Optional[str] = None, | |
state: Optional[str] = None, | |
country: Optional[str] = None, | |
postalcode: Optional[str] = None, | |
latitude: Optional[str] = None, | |
longitude: Optional[str] = None, | |
radius: int = 0, | |
max_results: int = 10 | |
): | |
"""Perform a maps search.""" | |
try: | |
with WEBS() as webs: | |
results = webs.maps(keywords=q, place=place, street=street, city=city, county=county, state=state, country=country, postalcode=postalcode, latitude=latitude, longitude=longitude, radius=radius, max_results=max_results) | |
return JSONResponse(content=jsonable_encoder(results)) | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=f"Error during maps search: {e}") | |
async def translate( | |
q: str, | |
from_: Optional[str] = None, | |
to: str = "en" | |
): | |
"""Translate text.""" | |
try: | |
with WEBS() as webs: | |
results = webs.translate(keywords=q, from_=from_, to=to) | |
return JSONResponse(content=jsonable_encoder(results)) | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=f"Error during translation: {e}") | |
async def youtube_transcript( | |
video_id: str, | |
languages: str = "en", | |
preserve_formatting: bool = False | |
): | |
"""Get the transcript of a YouTube video.""" | |
try: | |
languages_list = languages.split(",") | |
transcript = transcriber.get_transcript(video_id, languages=languages_list, preserve_formatting=preserve_formatting) | |
return JSONResponse(content=jsonable_encoder(transcript)) | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=f"Error getting YouTube transcript: {e}") | |
import requests | |
def get_weather_json(location: str): | |
url = f"https://wttr.in/{location}?format=j1" | |
response = requests.get(url) | |
if response.status_code == 200: | |
return response.json() | |
else: | |
return {"error": f"Unable to fetch weather data. Status code: {response.status_code}"} | |
def get_ascii_weather(location: str): | |
url = f"https://wttr.in/{location}" | |
response = requests.get(url, headers={'User-Agent': 'curl'}) | |
if response.status_code == 200: | |
return response.text | |
else: | |
return {"error": f"Unable to fetch weather data. Status code: {response.status_code}"} | |
# Run the API server if this script is executed | |
if __name__ == "__main__": | |
import uvicorn | |
uvicorn.run(app, host="0.0.0.0", port=8080) | |