| 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}") | |