Spaces:
Sleeping
Sleeping
| import os | |
| import requests | |
| # Test the REST API call with corrected endpoint | |
| GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY") or os.getenv("GENAI_API_KEY") or "AIzaSyDtPD6bnRX8zkqie4Ha08AIVZflQk--fwY" | |
| print(f"API Key present: {bool(GOOGLE_API_KEY)}") | |
| # Try v1 API with gemini-pro | |
| url = f"https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent?key={GOOGLE_API_KEY}" | |
| payload = { | |
| "contents": [{ | |
| "parts": [{"text": "Say hello in one word"}] | |
| }] | |
| } | |
| print(f"\nCalling Google Gemini API (v1)...") | |
| try: | |
| response = requests.post(url, json=payload, timeout=30) | |
| print(f"Status code: {response.status_code}") | |
| if response.status_code != 200: | |
| print(f"Error response: {response.text}") | |
| else: | |
| data = response.json() | |
| print(f"Success!") | |
| # Extract text | |
| if "candidates" in data and len(data["candidates"]) > 0: | |
| candidate = data["candidates"][0] | |
| if "content" in candidate and "parts" in candidate["content"]: | |
| parts = candidate["content"]["parts"] | |
| if len(parts) > 0 and "text" in parts[0]: | |
| print(f"Extracted text: {parts[0]['text']}") | |
| except Exception as e: | |
| print(f"Exception: {e}") | |
| import traceback | |
| traceback.print_exc() | |