File size: 1,124 Bytes
4408617 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import requests
import json
import os
API_KEY = "AIzaSyCXd43s3-sCSUJPkkXa1-LzXCMzFc9_xMI"
URL = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={API_KEY}"
headers = {
"Content-Type": "application/json"
}
data = {
"contents": [
{
"parts": [
{
"text": "Explain how AI works in a few words"
}
]
}
]
}
try:
print(f"Sending request to {URL.split('?')[0]}...")
response = requests.post(URL, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
result = response.json()
print("\nResponse:")
print(json.dumps(result, indent=2))
# Extract text
try:
text = result['candidates'][0]['content']['parts'][0]['text']
print(f"\nGenerated Text:\n{text}")
except:
print("Could not extract text from response.")
else:
print(f"Error: {response.text}")
except Exception as e:
print(f"Exception: {e}")
|