Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -157,7 +157,8 @@ async def web_search_and_extract(
|
|
| 157 |
safesearch: str = "moderate",
|
| 158 |
region: str = "wt-wt",
|
| 159 |
backend: str = "api",
|
| 160 |
-
max_chars: int = 6000
|
|
|
|
| 161 |
):
|
| 162 |
"""
|
| 163 |
Searches using WEBS, extracts text from the top results, and returns both.
|
|
@@ -185,11 +186,38 @@ async def web_search_and_extract(
|
|
| 185 |
extracted_results.append({"link": link, "text": None})
|
| 186 |
else:
|
| 187 |
extracted_results.append({"link": None, "text": None})
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
| 190 |
except Exception as e:
|
| 191 |
raise HTTPException(status_code=500, detail=f"Error during search and extraction: {e}")
|
| 192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
@app.get("/api/maps")
|
| 194 |
async def maps(
|
| 195 |
q: str,
|
|
@@ -240,6 +268,7 @@ async def youtube_transcript(
|
|
| 240 |
return JSONResponse(content=jsonable_encoder(transcript))
|
| 241 |
except Exception as e:
|
| 242 |
raise HTTPException(status_code=500, detail=f"Error getting YouTube transcript: {e}")
|
|
|
|
| 243 |
import requests
|
| 244 |
@app.get("/weather/json/{location}")
|
| 245 |
def get_weather_json(location: str):
|
|
|
|
| 157 |
safesearch: str = "moderate",
|
| 158 |
region: str = "wt-wt",
|
| 159 |
backend: str = "api",
|
| 160 |
+
max_chars: int = 6000
|
| 161 |
+
extract_only: bool = False
|
| 162 |
):
|
| 163 |
"""
|
| 164 |
Searches using WEBS, extracts text from the top results, and returns both.
|
|
|
|
| 186 |
extracted_results.append({"link": link, "text": None})
|
| 187 |
else:
|
| 188 |
extracted_results.append({"link": None, "text": None})
|
| 189 |
+
if extract_only:
|
| 190 |
+
return JSONResponse(content=jsonable_encoder({extracted_results}))
|
| 191 |
+
else:
|
| 192 |
+
return JSONResponse(content=jsonable_encoder({"search_results": search_results, "extracted_results": extracted_results}))
|
| 193 |
+
return
|
| 194 |
except Exception as e:
|
| 195 |
raise HTTPException(status_code=500, detail=f"Error during search and extraction: {e}")
|
| 196 |
|
| 197 |
+
@app.get("/api/website_summarizer")
|
| 198 |
+
async def website_summarizer(url: str):
|
| 199 |
+
"""Summarizes the content of a given URL using a chat model."""
|
| 200 |
+
try:
|
| 201 |
+
# Extract text from the given URL
|
| 202 |
+
response = requests.get(url, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0"})
|
| 203 |
+
response.raise_for_status()
|
| 204 |
+
visible_text = extract_text_from_webpage(response.text)
|
| 205 |
+
if len(visible_text) > 7500: # Adjust max_chars based on your needs
|
| 206 |
+
visible_text = visible_text[:7500] + "..."
|
| 207 |
+
|
| 208 |
+
# Use chat model to summarize the extracted text
|
| 209 |
+
with WEBS() as webs:
|
| 210 |
+
summary_prompt = f"Summarize this in detail in Paragraph: {visible_text}"
|
| 211 |
+
summary_result = webs.chat(keywords=summary_prompt, model="gpt-3.5")
|
| 212 |
+
|
| 213 |
+
# Return the summary result
|
| 214 |
+
return JSONResponse(content=jsonable_encoder({summary_result}))
|
| 215 |
+
|
| 216 |
+
except requests.exceptions.RequestException as e:
|
| 217 |
+
raise HTTPException(status_code=500, detail=f"Error fetching or processing URL: {e}")
|
| 218 |
+
except Exception as e:
|
| 219 |
+
raise HTTPException(status_code=500, detail=f"Error during summarization: {e}")
|
| 220 |
+
|
| 221 |
@app.get("/api/maps")
|
| 222 |
async def maps(
|
| 223 |
q: str,
|
|
|
|
| 268 |
return JSONResponse(content=jsonable_encoder(transcript))
|
| 269 |
except Exception as e:
|
| 270 |
raise HTTPException(status_code=500, detail=f"Error getting YouTube transcript: {e}")
|
| 271 |
+
|
| 272 |
import requests
|
| 273 |
@app.get("/weather/json/{location}")
|
| 274 |
def get_weather_json(location: str):
|