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