KingNish commited on
Commit
75fec26
·
verified ·
1 Parent(s): 5d5b83b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -35
app.py CHANGED
@@ -246,34 +246,26 @@ async def ask_website(url: str, question: str, model: str = "llama-3-70b"):
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:
@@ -290,18 +282,22 @@ async def web_gemini(
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(
@@ -322,14 +318,11 @@ async def ask_website_gemini(
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,
 
246
  raise HTTPException(status_code=500, detail=f"Error during question answering: {e}")
247
 
248
  @app.get("/api/gemini")
249
+ async def gemini(q: str, model: str = "flash"):
250
+ """Get answers from Gemini models."""
 
 
 
251
  try:
252
+ if model == "pro":
253
  url = f"https://gemini-pro.developer-house.workers.dev/?question={q}"
254
  else:
255
  url = f"https://gemini-flash.developer-house.workers.dev/?question={q}"
256
  response = requests.get(url)
257
+ response.raise_for_status() # Raise an error for bad status codes
258
+ data = response.json()
259
+ return data['content']
260
+ except requests.exceptions.RequestException as e:
261
+ raise HTTPException(status_code=500, detail=f"Gemini API request failed: {e}")
 
262
 
263
  @app.get("/api/web_gemini")
264
+ async def web_gemini(q: str, model: str = "flash"):
265
+ """Search the web and get answers from Gemini models."""
 
 
 
266
  try:
267
  with WEBS() as webs:
268
  search_results = webs.text(keywords=q, max_results=3)
 
269
  extracted_results = []
270
  for result in search_results:
271
  if 'href' in result:
 
282
  extracted_results.append({"link": link, "text": None})
283
  else:
284
  extracted_results.append({"link": None, "text": None})
285
+
286
+ content = jsonable_encoder({"extracted_results": extracted_results})
287
+
288
+ # Call Gemini API based on the selected model
289
+ if model == "pro":
290
+ url = f"https://gemini-pro.developer-house.workers.dev/?question=Based on the following text from Google Search, answer this question in Paragraph: [QUESTION] {q} [TEXT] {content}"
291
+ else:
292
+ url = f"https://gemini-flash.developer-house.workers.dev/?question=Based on the following text from Google Search, answer this question in Paragraph: [QUESTION] {q} [TEXT] {content}"
293
+
294
+ response = requests.get(url)
295
+ response.raise_for_status()
296
  data = response.json()
297
  return data['content']
298
+
299
+ except requests.exceptions.RequestException as e:
300
+ raise HTTPException(status_code=500, detail=f"Error during web search or Gemini request: {e}")
301
 
302
  @app.get("/api/ask_website_gemini")
303
  async def ask_website_gemini(
 
318
  else:
319
  url = f"https://gemini-flash.developer-house.workers.dev/?question={q}"
320
  response = requests.get(url)
321
+ data = response.json()
322
+ return data['content']
323
+ except requests.exceptions.RequestException as e:
324
+ raise HTTPException(status_code=500, detail=f"Gemini API request failed: {e}")
 
 
325
 
 
326
  @app.get("/api/maps")
327
  async def maps(
328
  q: str,