Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,176 +1,160 @@
|
|
1 |
-
from fastapi import FastAPI,
|
2 |
from fastapi.responses import JSONResponse
|
3 |
from webscout import WEBS, transcriber
|
4 |
|
5 |
-
|
6 |
-
from pydantic import BaseModel, Field
|
7 |
-
from typing import List, Optional
|
8 |
from fastapi.encoders import jsonable_encoder
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
12 |
-
class SearchQuery(BaseModel):
|
13 |
-
q: str = Field(..., description="The search query string")
|
14 |
-
max_results: int = Field(10, description="The maximum number of results to return", ge=1, le=100)
|
15 |
-
timelimit: Optional[str] = Field(None, description="The time limit for the search (e.g., 'd' for day, 'w' for week, 'm' for month, 'y' for year)")
|
16 |
-
safesearch: str = Field("moderate", description="The safe search level ('on', 'moderate', or 'off')")
|
17 |
-
region: str = Field("wt-wt", description="The region for the search (e.g., 'us-en', 'uk-en', 'ru-ru')")
|
18 |
-
backend: str = Field("api", description="The backend to use for search ('api', 'html', or 'lite')")
|
19 |
-
|
20 |
-
class ImageSearchQuery(BaseModel):
|
21 |
-
q: str = Field(..., description="The search query string")
|
22 |
-
max_results: int = Field(10, description="The maximum number of results to return", ge=1, le=100)
|
23 |
-
safesearch: str = Field("moderate", description="The safe search level ('on', 'moderate', or 'off')")
|
24 |
-
region: str = Field("wt-wt", description="The region for the search (e.g., 'us-en', 'uk-en', 'ru-ru')")
|
25 |
-
timelimit: Optional[str] = Field(None, description="The time limit for the search ('Day', 'Week', 'Month', or 'Year')")
|
26 |
-
size: Optional[str] = Field(None, description="The size of the images ('Small', 'Medium', 'Large', or 'Wallpaper')")
|
27 |
-
color: Optional[str] = Field(None, description="The color of the images ('color', 'Monochrome', 'Red', etc.)")
|
28 |
-
type_image: Optional[str] = Field(None, description="The type of images ('photo', 'clipart', 'gif', etc.)")
|
29 |
-
layout: Optional[str] = Field(None, description="The layout of the images ('Square', 'Tall', or 'Wide')")
|
30 |
-
license_image: Optional[str] = Field(None, description="The license of the images ('any', 'Public', 'Share', etc.)")
|
31 |
-
|
32 |
-
class VideoSearchQuery(BaseModel):
|
33 |
-
q: str = Field(..., description="The search query string")
|
34 |
-
max_results: int = Field(10, description="The maximum number of results to return", ge=1, le=100)
|
35 |
-
safesearch: str = Field("moderate", description="The safe search level ('on', 'moderate', or 'off')")
|
36 |
-
region: str = Field("wt-wt", description="The region for the search (e.g., 'us-en', 'uk-en', 'ru-ru')")
|
37 |
-
timelimit: Optional[str] = Field(None, description="The time limit for the search (e.g., 'd' for day, 'w' for week, 'm' for month)")
|
38 |
-
resolution: Optional[str] = Field(None, description="The resolution of the videos ('high' or 'standard')")
|
39 |
-
duration: Optional[str] = Field(None, description="The duration of the videos ('short', 'medium', or 'long')")
|
40 |
-
license_videos: Optional[str] = Field(None, description="The license of the videos ('creativeCommon' or 'youtube')")
|
41 |
-
|
42 |
-
class NewsSearchQuery(BaseModel):
|
43 |
-
q: str = Field(..., description="The search query string")
|
44 |
-
max_results: int = Field(10, description="The maximum number of results to return", ge=1, le=100)
|
45 |
-
safesearch: str = Field("moderate", description="The safe search level ('on', 'moderate', or 'off')")
|
46 |
-
region: str = Field("wt-wt", description="The region for the search (e.g., 'us-en', 'uk-en', 'ru-ru')")
|
47 |
-
timelimit: Optional[str] = Field(None, description="The time limit for the search (e.g., 'd' for day, 'w' for week, 'm' for month, 'y' for year)")
|
48 |
-
|
49 |
-
class AnswersSearchQuery(BaseModel):
|
50 |
-
q: str = Field(..., description="The search query string")
|
51 |
-
|
52 |
-
class SuggestionsSearchQuery(BaseModel):
|
53 |
-
q: str = Field(..., description="The search query string")
|
54 |
-
region: str = Field("wt-wt", description="The region for suggestions (e.g., 'us-en', 'uk-en', 'ru-ru')")
|
55 |
-
|
56 |
-
class MapsSearchQuery(BaseModel):
|
57 |
-
q: str = Field(..., description="The search query string")
|
58 |
-
place: Optional[str] = Field(None, description="Simplified search - if set, other location parameters are ignored")
|
59 |
-
street: Optional[str] = Field(None, description="Street address")
|
60 |
-
city: Optional[str] = Field(None, description="City")
|
61 |
-
county: Optional[str] = Field(None, description="County")
|
62 |
-
state: Optional[str] = Field(None, description="State")
|
63 |
-
country: Optional[str] = Field(None, description="Country")
|
64 |
-
postalcode: Optional[str] = Field(None, description="Postal code")
|
65 |
-
latitude: Optional[str] = Field(None, description="Latitude (if used, other location parameters are ignored)")
|
66 |
-
longitude: Optional[str] = Field(None, description="Longitude (if used, other location parameters are ignored)")
|
67 |
-
radius: int = Field(0, description="Expand the search radius in kilometers")
|
68 |
-
max_results: int = Field(10, description="The maximum number of results to return", ge=1, le=100)
|
69 |
-
|
70 |
-
class TranslateSearchQuery(BaseModel):
|
71 |
-
q: str = Field(..., description="The text to translate")
|
72 |
-
from_: Optional[str] = Field(None, description="The source language (defaults to automatic detection)")
|
73 |
-
to: str = Field("en", description="The target language (defaults to English)")
|
74 |
-
|
75 |
-
class TranscriptQuery(BaseModel):
|
76 |
-
video_id: str = Field(..., description="The YouTube video ID")
|
77 |
-
languages: str = Field("en", description="Comma-separated list of language codes (e.g., 'en,es')")
|
78 |
-
preserve_formatting: bool = Field(False, description="Whether to preserve text formatting")
|
79 |
-
|
80 |
@app.get("/")
|
81 |
async def root():
|
82 |
-
return {"message": "
|
83 |
|
84 |
@app.get("/health")
|
85 |
async def health_check():
|
86 |
return {"status": "OK"}
|
87 |
|
88 |
@app.get("/api/search")
|
89 |
-
async def search(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
"""Perform a text search."""
|
91 |
try:
|
92 |
with WEBS() as webs:
|
93 |
-
results = webs.text(keywords=
|
94 |
return JSONResponse(content=jsonable_encoder(results))
|
95 |
except Exception as e:
|
96 |
raise HTTPException(status_code=500, detail=f"Error during search: {e}")
|
97 |
|
98 |
@app.get("/api/images")
|
99 |
-
async def images(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
"""Perform an image search."""
|
101 |
try:
|
102 |
with WEBS() as webs:
|
103 |
-
results = webs.images(keywords=
|
104 |
return JSONResponse(content=jsonable_encoder(results))
|
105 |
except Exception as e:
|
106 |
raise HTTPException(status_code=500, detail=f"Error during image search: {e}")
|
107 |
|
108 |
@app.get("/api/videos")
|
109 |
-
async def videos(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
"""Perform a video search."""
|
111 |
try:
|
112 |
with WEBS() as webs:
|
113 |
-
results = webs.videos(keywords=
|
114 |
return JSONResponse(content=jsonable_encoder(results))
|
115 |
except Exception as e:
|
116 |
raise HTTPException(status_code=500, detail=f"Error during video search: {e}")
|
117 |
|
118 |
@app.get("/api/news")
|
119 |
-
async def news(
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
"""Perform a news search."""
|
121 |
try:
|
122 |
with WEBS() as webs:
|
123 |
-
results = webs.news(keywords=
|
124 |
return JSONResponse(content=jsonable_encoder(results))
|
125 |
except Exception as e:
|
126 |
raise HTTPException(status_code=500, detail=f"Error during news search: {e}")
|
127 |
|
128 |
@app.get("/api/answers")
|
129 |
-
async def answers(
|
130 |
"""Get instant answers for a query."""
|
131 |
try:
|
132 |
with WEBS() as webs:
|
133 |
-
results = webs.answers(keywords=
|
134 |
return JSONResponse(content=jsonable_encoder(results))
|
135 |
except Exception as e:
|
136 |
raise HTTPException(status_code=500, detail=f"Error getting instant answers: {e}")
|
137 |
|
138 |
@app.get("/api/suggestions")
|
139 |
-
async def suggestions(
|
140 |
"""Get search suggestions for a query."""
|
141 |
try:
|
142 |
with WEBS() as webs:
|
143 |
-
results = webs.suggestions(keywords=
|
144 |
return JSONResponse(content=jsonable_encoder(results))
|
145 |
except Exception as e:
|
146 |
raise HTTPException(status_code=500, detail=f"Error getting search suggestions: {e}")
|
147 |
|
148 |
@app.get("/api/maps")
|
149 |
-
async def maps(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
"""Perform a maps search."""
|
151 |
try:
|
152 |
with WEBS() as webs:
|
153 |
-
results = webs.maps(keywords=
|
154 |
return JSONResponse(content=jsonable_encoder(results))
|
155 |
except Exception as e:
|
156 |
raise HTTPException(status_code=500, detail=f"Error during maps search: {e}")
|
157 |
|
158 |
@app.get("/api/translate")
|
159 |
-
async def translate(
|
|
|
|
|
|
|
|
|
160 |
"""Translate text."""
|
161 |
try:
|
162 |
with WEBS() as webs:
|
163 |
-
results = webs.translate(keywords=
|
164 |
return JSONResponse(content=jsonable_encoder(results))
|
165 |
except Exception as e:
|
166 |
raise HTTPException(status_code=500, detail=f"Error during translation: {e}")
|
167 |
|
168 |
@app.get("/api/youtube/transcript")
|
169 |
-
async def youtube_transcript(
|
|
|
|
|
|
|
|
|
170 |
"""Get the transcript of a YouTube video."""
|
171 |
try:
|
172 |
-
|
173 |
-
transcript = transcriber.get_transcript(
|
174 |
return JSONResponse(content=jsonable_encoder(transcript))
|
175 |
except Exception as e:
|
176 |
raise HTTPException(status_code=500, detail=f"Error getting YouTube transcript: {e}")
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
from fastapi.responses import JSONResponse
|
3 |
from webscout import WEBS, transcriber
|
4 |
|
5 |
+
from typing import Optional
|
|
|
|
|
6 |
from fastapi.encoders import jsonable_encoder
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
@app.get("/")
|
11 |
async def root():
|
12 |
+
return {"message": "API documentation can be found at /docs"}
|
13 |
|
14 |
@app.get("/health")
|
15 |
async def health_check():
|
16 |
return {"status": "OK"}
|
17 |
|
18 |
@app.get("/api/search")
|
19 |
+
async def search(
|
20 |
+
q: str,
|
21 |
+
max_results: int = 10,
|
22 |
+
timelimit: Optional[str] = None,
|
23 |
+
safesearch: str = "moderate",
|
24 |
+
region: str = "wt-wt",
|
25 |
+
backend: str = "api"
|
26 |
+
):
|
27 |
"""Perform a text search."""
|
28 |
try:
|
29 |
with WEBS() as webs:
|
30 |
+
results = webs.text(keywords=q, region=region, safesearch=safesearch, timelimit=timelimit, backend=backend, max_results=max_results)
|
31 |
return JSONResponse(content=jsonable_encoder(results))
|
32 |
except Exception as e:
|
33 |
raise HTTPException(status_code=500, detail=f"Error during search: {e}")
|
34 |
|
35 |
@app.get("/api/images")
|
36 |
+
async def images(
|
37 |
+
q: str,
|
38 |
+
max_results: int = 10,
|
39 |
+
safesearch: str = "moderate",
|
40 |
+
region: str = "wt-wt",
|
41 |
+
timelimit: Optional[str] = None,
|
42 |
+
size: Optional[str] = None,
|
43 |
+
color: Optional[str] = None,
|
44 |
+
type_image: Optional[str] = None,
|
45 |
+
layout: Optional[str] = None,
|
46 |
+
license_image: Optional[str] = None
|
47 |
+
):
|
48 |
"""Perform an image search."""
|
49 |
try:
|
50 |
with WEBS() as webs:
|
51 |
+
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)
|
52 |
return JSONResponse(content=jsonable_encoder(results))
|
53 |
except Exception as e:
|
54 |
raise HTTPException(status_code=500, detail=f"Error during image search: {e}")
|
55 |
|
56 |
@app.get("/api/videos")
|
57 |
+
async def videos(
|
58 |
+
q: str,
|
59 |
+
max_results: int = 10,
|
60 |
+
safesearch: str = "moderate",
|
61 |
+
region: str = "wt-wt",
|
62 |
+
timelimit: Optional[str] = None,
|
63 |
+
resolution: Optional[str] = None,
|
64 |
+
duration: Optional[str] = None,
|
65 |
+
license_videos: Optional[str] = None
|
66 |
+
):
|
67 |
"""Perform a video search."""
|
68 |
try:
|
69 |
with WEBS() as webs:
|
70 |
+
results = webs.videos(keywords=q, region=region, safesearch=safesearch, timelimit=timelimit, resolution=resolution, duration=duration, license_videos=license_videos, max_results=max_results)
|
71 |
return JSONResponse(content=jsonable_encoder(results))
|
72 |
except Exception as e:
|
73 |
raise HTTPException(status_code=500, detail=f"Error during video search: {e}")
|
74 |
|
75 |
@app.get("/api/news")
|
76 |
+
async def news(
|
77 |
+
q: str,
|
78 |
+
max_results: int = 10,
|
79 |
+
safesearch: str = "moderate",
|
80 |
+
region: str = "wt-wt",
|
81 |
+
timelimit: Optional[str] = None
|
82 |
+
):
|
83 |
"""Perform a news search."""
|
84 |
try:
|
85 |
with WEBS() as webs:
|
86 |
+
results = webs.news(keywords=q, region=region, safesearch=safesearch, timelimit=timelimit, max_results=max_results)
|
87 |
return JSONResponse(content=jsonable_encoder(results))
|
88 |
except Exception as e:
|
89 |
raise HTTPException(status_code=500, detail=f"Error during news search: {e}")
|
90 |
|
91 |
@app.get("/api/answers")
|
92 |
+
async def answers(q: str):
|
93 |
"""Get instant answers for a query."""
|
94 |
try:
|
95 |
with WEBS() as webs:
|
96 |
+
results = webs.answers(keywords=q)
|
97 |
return JSONResponse(content=jsonable_encoder(results))
|
98 |
except Exception as e:
|
99 |
raise HTTPException(status_code=500, detail=f"Error getting instant answers: {e}")
|
100 |
|
101 |
@app.get("/api/suggestions")
|
102 |
+
async def suggestions(q: str, region: str = "wt-wt"):
|
103 |
"""Get search suggestions for a query."""
|
104 |
try:
|
105 |
with WEBS() as webs:
|
106 |
+
results = webs.suggestions(keywords=q, region=region)
|
107 |
return JSONResponse(content=jsonable_encoder(results))
|
108 |
except Exception as e:
|
109 |
raise HTTPException(status_code=500, detail=f"Error getting search suggestions: {e}")
|
110 |
|
111 |
@app.get("/api/maps")
|
112 |
+
async def maps(
|
113 |
+
q: str,
|
114 |
+
place: Optional[str] = None,
|
115 |
+
street: Optional[str] = None,
|
116 |
+
city: Optional[str] = None,
|
117 |
+
county: Optional[str] = None,
|
118 |
+
state: Optional[str] = None,
|
119 |
+
country: Optional[str] = None,
|
120 |
+
postalcode: Optional[str] = None,
|
121 |
+
latitude: Optional[str] = None,
|
122 |
+
longitude: Optional[str] = None,
|
123 |
+
radius: int = 0,
|
124 |
+
max_results: int = 10
|
125 |
+
):
|
126 |
"""Perform a maps search."""
|
127 |
try:
|
128 |
with WEBS() as webs:
|
129 |
+
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)
|
130 |
return JSONResponse(content=jsonable_encoder(results))
|
131 |
except Exception as e:
|
132 |
raise HTTPException(status_code=500, detail=f"Error during maps search: {e}")
|
133 |
|
134 |
@app.get("/api/translate")
|
135 |
+
async def translate(
|
136 |
+
q: str,
|
137 |
+
from_: Optional[str] = None,
|
138 |
+
to: str = "en"
|
139 |
+
):
|
140 |
"""Translate text."""
|
141 |
try:
|
142 |
with WEBS() as webs:
|
143 |
+
results = webs.translate(keywords=q, from_=from_, to=to)
|
144 |
return JSONResponse(content=jsonable_encoder(results))
|
145 |
except Exception as e:
|
146 |
raise HTTPException(status_code=500, detail=f"Error during translation: {e}")
|
147 |
|
148 |
@app.get("/api/youtube/transcript")
|
149 |
+
async def youtube_transcript(
|
150 |
+
video_id: str,
|
151 |
+
languages: str = "en",
|
152 |
+
preserve_formatting: bool = False
|
153 |
+
):
|
154 |
"""Get the transcript of a YouTube video."""
|
155 |
try:
|
156 |
+
languages_list = languages.split(",")
|
157 |
+
transcript = transcriber.get_transcript(video_id, languages=languages_list, preserve_formatting=preserve_formatting)
|
158 |
return JSONResponse(content=jsonable_encoder(transcript))
|
159 |
except Exception as e:
|
160 |
raise HTTPException(status_code=500, detail=f"Error getting YouTube transcript: {e}")
|