| Server IP : 85.155.190.233 / Your IP : 216.73.216.103 Web Server : nginx/1.24.0 System : Linux antigravity-cli 6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64 User : wp-moonbloom ( 1001) PHP Version : 8.3.6 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /opt/Gemini_Swarm_Core/scripts/ |
Upload File : |
import os
import urllib.request
import urllib.error
import json
import google.auth
import google.auth.transport.requests
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/root/gcp-key.json"
def test():
try:
print("Obtaining Google credentials...")
credentials, project = google.auth.default(
scopes=["https://www.googleapis.com/auth/cloud-platform"]
)
auth_req = google.auth.transport.requests.Request()
credentials.refresh(auth_req)
print(f"Token obtained successfully! Project: {project}")
# Call Gemini API
url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent"
headers = {
"Authorization": f"Bearer {credentials.token}",
"Content-Type": "application/json",
"x-goog-user-project": "gen-lang-client-0207686288"
}
data = {
"contents": [{"parts": [{"text": "Hello, Gemini! Answer with a short sentence."}]}]
}
print("Calling Gemini API...")
req = urllib.request.Request(url, data=json.dumps(data).encode('utf-8'), headers=headers, method="POST")
with urllib.request.urlopen(req) as response:
res = json.loads(response.read().decode('utf-8'))
text = res["candidates"][0]["content"]["parts"][0]["text"]
print(f"Response from Gemini:\n{text}")
except urllib.error.HTTPError as e:
print(f"HTTP Error calling Gemini: {e}")
try:
print(f"Error body: {e.read().decode('utf-8')}")
except Exception as read_err:
print(f"Could not read error body: {read_err}")
except Exception as e:
print(f"Error calling Gemini: {e}")
if __name__ == "__main__":
test()