Spaces:
Sleeping
Sleeping
import requests | |
import os | |
import httpx | |
import asyncio | |
GEMINI_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent" | |
async def complete_gemini_async(prompt, key, params={}): | |
data = { | |
"contents": [ | |
{ | |
"parts": [ | |
{ | |
"text":prompt | |
} | |
] | |
} | |
], | |
"generationConfig": { | |
"stopSequences": ["Title"], | |
"temperature": 1.0 if "temp" not in params else params["temp"], | |
"maxOutputTokens": 2048 if "max_length" not in params else params["max_length"], | |
"topP": 0.8 if "top_p" not in params else params["top_p"], | |
"topK": 10 if "top_k" not in params else params["top_k"] | |
} | |
} | |
params = {'key': key} | |
headers = {"Content-Type": "application/json"} | |
result = None | |
try: | |
async with httpx.AsyncClient() as client: | |
result = await client.post(GEMINI_URL, params=params, json=data, headers=headers, timeout=60) | |
result.raise_for_status() | |
result = result.json()["candidates"][0]["content"]["parts"][0]["text"] | |
return {"response":result} | |
except requests.RequestException as e: | |
if e.status_code == 429: | |
await asyncio.sleep(60) | |
#rint(f"Error making Gemini API request: {e}") | |
async with httpx.AsyncClient() as client: | |
result = await client.post(GEMINI_URL, params=params, json=data, headers=headers, timeout=60) | |
result.raise_for_status() | |
result = result.json()["candidates"][0]["content"]["parts"][0]["text"] | |
return {"response":result} |