KingNish commited on
Commit
f50a979
1 Parent(s): 431deae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -0
app.py CHANGED
@@ -216,6 +216,34 @@ async def website_summarizer(url: str):
216
  raise HTTPException(status_code=500, detail=f"Error fetching or processing URL: {e}")
217
  except Exception as e:
218
  raise HTTPException(status_code=500, detail=f"Error during summarization: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219
 
220
  @app.get("/api/maps")
221
  async def maps(
 
216
  raise HTTPException(status_code=500, detail=f"Error fetching or processing URL: {e}")
217
  except Exception as e:
218
  raise HTTPException(status_code=500, detail=f"Error during summarization: {e}")
219
+
220
+ @app.get("/api/ask_website")
221
+ async def ask_website(url: str, question: str):
222
+ """
223
+ Asks a question about the content of a given website.
224
+ """
225
+ try:
226
+ # Extract text from the given URL
227
+ response = requests.get(url, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0"})
228
+ response.raise_for_status()
229
+ visible_text = extract_text_from_webpage(response.text)
230
+ if len(visible_text) > 7500: # Adjust max_chars based on your needs
231
+ visible_text = visible_text[:7500] + "..."
232
+
233
+ # Construct a prompt for the chat model
234
+ prompt = f"Based on the following text, answer this question in Detail: [QUESTION] {question}\n\n[TEXT] {visible_text}"
235
+
236
+ # Use chat model to get the answer
237
+ with WEBS() as webs:
238
+ answer_result = webs.chat(keywords=prompt, model="gpt-3.5")
239
+
240
+ # Return the answer result
241
+ return JSONResponse(content=jsonable_encoder({answer_result}))
242
+
243
+ except requests.exceptions.RequestException as e:
244
+ raise HTTPException(status_code=500, detail=f"Error fetching or processing URL: {e}")
245
+ except Exception as e:
246
+ raise HTTPException(status_code=500, detail=f"Error during question answering: {e}")
247
 
248
  @app.get("/api/maps")
249
  async def maps(