Update app.py
Browse files
app.py
CHANGED
|
@@ -11,6 +11,7 @@ import random
|
|
| 11 |
import textract
|
| 12 |
import boto3
|
| 13 |
from botocore.exceptions import NoCredentialsError
|
|
|
|
| 14 |
|
| 15 |
app = FastAPI()
|
| 16 |
|
|
@@ -181,4 +182,82 @@ async def extract_text(file: UploadFile, api_key: str = Depends(verify_api_key))
|
|
| 181 |
if not result['success']:
|
| 182 |
raise HTTPException(status_code=500, detail=result['error'])
|
| 183 |
return {"extracted_text": result['extracted_text']}
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
import textract
|
| 12 |
import boto3
|
| 13 |
from botocore.exceptions import NoCredentialsError
|
| 14 |
+
from duckduckgo_search import DDGS
|
| 15 |
|
| 16 |
app = FastAPI()
|
| 17 |
|
|
|
|
| 182 |
if not result['success']:
|
| 183 |
raise HTTPException(status_code=500, detail=result['error'])
|
| 184 |
return {"extracted_text": result['extracted_text']}
|
| 185 |
+
|
| 186 |
+
def web_search(query: str, num_results: int = 5) -> dict:
|
| 187 |
+
"""
|
| 188 |
+
Perform a web search using DuckDuckGo and return the results.
|
| 189 |
+
Args:
|
| 190 |
+
query (str): The search query.
|
| 191 |
+
num_results (int, optional): The number of results to return. Defaults to 5.
|
| 192 |
+
Returns:
|
| 193 |
+
dict: A dictionary containing the success status and either the search results or an error message.
|
| 194 |
+
"""
|
| 195 |
+
try:
|
| 196 |
+
with DDGS() as ddgs:
|
| 197 |
+
results = list(ddgs.text(query, max_results=num_results))
|
| 198 |
+
formatted_results = [
|
| 199 |
+
{
|
| 200 |
+
'title': result['title'],
|
| 201 |
+
'body': result['body'],
|
| 202 |
+
'href': result['href']
|
| 203 |
+
}
|
| 204 |
+
for result in results
|
| 205 |
+
]
|
| 206 |
+
return {
|
| 207 |
+
'success': True,
|
| 208 |
+
'results': formatted_results
|
| 209 |
+
}
|
| 210 |
+
except Exception as e:
|
| 211 |
+
logger.error(f"Error performing web search: {e}")
|
| 212 |
+
return {
|
| 213 |
+
'success': False,
|
| 214 |
+
'error': f"Error performing web search: {str(e)}"
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
def image_search(query: str, num_results: int = 5) -> dict:
|
| 218 |
+
"""
|
| 219 |
+
Perform an image search using DuckDuckGo and return the results.
|
| 220 |
+
Args:
|
| 221 |
+
query (str): The search query.
|
| 222 |
+
num_results (int, optional): The number of results to return. Defaults to 5.
|
| 223 |
+
Returns:
|
| 224 |
+
dict: A dictionary containing the success status and either the image search results or an error message.
|
| 225 |
+
"""
|
| 226 |
+
try:
|
| 227 |
+
with DDGS() as ddgs:
|
| 228 |
+
results = list(ddgs.images(query, max_results=num_results))
|
| 229 |
+
formatted_results = [
|
| 230 |
+
{
|
| 231 |
+
'title': result['title'],
|
| 232 |
+
'image_url': result['image'],
|
| 233 |
+
'thumbnail_url': result['thumbnail'],
|
| 234 |
+
'source_url': result['url'],
|
| 235 |
+
'width': result['width'],
|
| 236 |
+
'height': result['height']
|
| 237 |
+
}
|
| 238 |
+
for result in results
|
| 239 |
+
]
|
| 240 |
+
return {
|
| 241 |
+
'success': True,
|
| 242 |
+
'results': formatted_results
|
| 243 |
+
}
|
| 244 |
+
except Exception as e:
|
| 245 |
+
logger.error(f"Error performing image search: {e}")
|
| 246 |
+
return {
|
| 247 |
+
'success': False,
|
| 248 |
+
'error': f"Error performing image search: {str(e)}"
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
@app.get("/web-search/")
|
| 252 |
+
async def web_search_endpoint(query: str, num_results: int = 5, api_key: str = Depends(verify_api_key)):
|
| 253 |
+
result = web_search(query, num_results)
|
| 254 |
+
if not result['success']:
|
| 255 |
+
raise HTTPException(status_code=500, detail=result['error'])
|
| 256 |
+
return result
|
| 257 |
+
|
| 258 |
+
@app.get("/image-search/")
|
| 259 |
+
async def image_search_endpoint(query: str, num_results: int = 5, api_key: str = Depends(verify_api_key)):
|
| 260 |
+
result = image_search(query, num_results)
|
| 261 |
+
if not result['success']:
|
| 262 |
+
raise HTTPException(status_code=500, detail=result['error'])
|
| 263 |
+
return result
|