KingNish commited on
Commit
5d5b83b
·
verified ·
1 Parent(s): 08caa7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -1
app.py CHANGED
@@ -244,7 +244,92 @@ async def ask_website(url: str, question: str, model: str = "llama-3-70b"):
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(
250
  q: str,
 
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/gemini")
249
+ async def gemini(
250
+ q: str,
251
+ model: str = "flash"
252
+ ):
253
+ """Perform a text search."""
254
+ try:
255
+ if model=="pro":
256
+ url = f"https://gemini-pro.developer-house.workers.dev/?question={q}"
257
+ else:
258
+ url = f"https://gemini-flash.developer-house.workers.dev/?question={q}"
259
+ response = requests.get(url)
260
+ if response.status_code == 200:
261
+ data = response.json()
262
+ return data['content']
263
+ else:
264
+ print(f"API request failed with status code: {response.status_code}")
265
+ return None
266
+
267
+ @app.get("/api/web_gemini")
268
+ async def web_gemini(
269
+ q: str,
270
+ model: str = "flash"
271
+ ):
272
+ """Perform a text search."""
273
+ try:
274
+ with WEBS() as webs:
275
+ search_results = webs.text(keywords=q, max_results=3)
276
+ # Extract text from each result's link
277
+ extracted_results = []
278
+ for result in search_results:
279
+ if 'href' in result:
280
+ link = result['href']
281
+ try:
282
+ response = requests.get(link, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0"})
283
+ response.raise_for_status()
284
+ visible_text = extract_text_from_webpage(response.text)
285
+ if len(visible_text) > 10000:
286
+ visible_text = visible_text[:10000] + "..."
287
+ extracted_results.append({"link": link, "text": visible_text})
288
+ except requests.exceptions.RequestException as e:
289
+ print(f"Error fetching or processing {link}: {e}")
290
+ extracted_results.append({"link": link, "text": None})
291
+ else:
292
+ extracted_results.append({"link": None, "text": None})
293
+ content=jsonable_encoder({extracted_results})
294
+ if model=="pro":
295
+ url = f"https://gemini-pro.developer-house.workers.dev/?question= Based on the following text from Google Search, answer this question in Paragraph: [QUESTION] {question} [TEXT] {content}"
296
+ else:
297
+ url = f"https://gemini-flash.developer-house.workers.dev/?question= Based on the following text from Google Search, answer this question in Paragraph: [QUESTION] {question} [TEXT] {content}"
298
+ response = requests.get(url)
299
+ if response.status_code == 200:
300
+ data = response.json()
301
+ return data['content']
302
+ else:
303
+ print(f"API request failed with status code: {response.status_code}")
304
+ return None
305
+
306
+ @app.get("/api/ask_website_gemini")
307
+ async def ask_website_gemini(
308
+ url: str,
309
+ q: str,
310
+ model: str = "flash"
311
+ ):
312
+ """Perform a text search."""
313
+ try:
314
+ # Extract text from the given URL
315
+ response = requests.get(url, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0"})
316
+ response.raise_for_status()
317
+ visible_text = extract_text_from_webpage(response.text)
318
+ if len(visible_text) > 30000: # Adjust max_chars based on your needs
319
+ visible_text = visible_text[:30000] + "..."
320
+ if model=="pro":
321
+ url = f"https://gemini-pro.developer-house.workers.dev/?question= Based on the following text, answer this question in Paragraph: [QUESTION] {question} [TEXT] {visible_text}"
322
+ else:
323
+ url = f"https://gemini-flash.developer-house.workers.dev/?question={q}"
324
+ response = requests.get(url)
325
+ if response.status_code == 200:
326
+ data = response.json()
327
+ return data['content']
328
+ else:
329
+ print(f"API request failed with status code: {response.status_code}")
330
+ return None
331
+
332
+
333
  @app.get("/api/maps")
334
  async def maps(
335
  q: str,