Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -245,20 +245,44 @@ async def ask_website(url: str, question: str, model: str = "llama-3-70b"):
|
|
| 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(q: str, model: str = "flash"):
|
| 250 |
"""Get answers from Gemini models."""
|
| 251 |
try:
|
| 252 |
-
|
| 253 |
-
|
|
|
|
| 254 |
else:
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
data = response.json()
|
| 259 |
-
return JSONResponse(content=jsonable_encoder({data['content']})) # Use JSONResponse to return a JSON
|
| 260 |
-
except requests.exceptions.RequestException as e:
|
| 261 |
-
raise HTTPException(status_code=500, detail=f"Gemini API request failed: {e}")
|
| 262 |
|
| 263 |
|
| 264 |
|
|
|
|
| 245 |
except Exception as e:
|
| 246 |
raise HTTPException(status_code=500, detail=f"Error during question answering: {e}")
|
| 247 |
|
| 248 |
+
import requests
|
| 249 |
+
|
| 250 |
+
def get_gemini_content(question, model="flash"):
|
| 251 |
+
"""
|
| 252 |
+
Sends a request to the Gemini API and returns the content of the response.
|
| 253 |
+
|
| 254 |
+
Args:
|
| 255 |
+
question: The question to ask the Gemini API.
|
| 256 |
+
model: The Gemini model to use (flash or pro). Defaults to "flash".
|
| 257 |
+
|
| 258 |
+
Returns:
|
| 259 |
+
The content string from the API response, or None if the request fails.
|
| 260 |
+
"""
|
| 261 |
+
|
| 262 |
+
if model == "pro":
|
| 263 |
+
url = f"https://gemini-pro.developer-house.workers.dev/?question={question}"
|
| 264 |
+
else:
|
| 265 |
+
url = f"https://gemini-flash.developer-house.workers.dev/?question={question}"
|
| 266 |
+
response = requests.get(url)
|
| 267 |
+
|
| 268 |
+
if response.status_code == 200:
|
| 269 |
+
data = response.json()
|
| 270 |
+
return data['content']
|
| 271 |
+
else:
|
| 272 |
+
print(f"API request failed with status code: {response.status_code}")
|
| 273 |
+
return None
|
| 274 |
+
|
| 275 |
@app.get("/api/gemini")
|
| 276 |
async def gemini(q: str, model: str = "flash"):
|
| 277 |
"""Get answers from Gemini models."""
|
| 278 |
try:
|
| 279 |
+
content = get_gemini_content(q, model)
|
| 280 |
+
if content:
|
| 281 |
+
return JSONResponse(content=jsonable_encoder({"answer": content}))
|
| 282 |
else:
|
| 283 |
+
raise HTTPException(status_code=500, detail="Gemini API request failed.")
|
| 284 |
+
except Exception as e:
|
| 285 |
+
raise HTTPException(status_code=500, detail=f"Error during Gemini request: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
|
| 287 |
|
| 288 |
|