-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_api.py
More file actions
78 lines (65 loc) · 2.8 KB
/
test_api.py
File metadata and controls
78 lines (65 loc) · 2.8 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
"""
Simple test script to validate the Gemini API connection and basic functionality.
"""
import os
from dotenv import load_dotenv
from google import genai
from google.genai import types
def test_api():
"""Test basic API functionality."""
print("🔍 Testing Google Gemini API connection...")
# Load environment variables
load_dotenv()
api_key = os.getenv('GOOGLE_API_KEY')
if not api_key:
print("❌ No API key found in .env file")
return False
try:
# Initialize client
client = genai.Client(api_key=api_key)
print("✅ Client initialized successfully")
# Test simple text generation
print("🧪 Testing text generation...")
response = client.models.generate_content(
model="gemini-2.0-flash-lite",
contents="Hello! Please respond with 'API test successful' if you can hear me."
)
if response and response.candidates:
text_content = response.candidates[0].content.parts[0].text
print(f"✅ Text response: {text_content}")
# Test image generation
print("🖼️ Testing image generation...")
image_response = client.models.generate_content(
model="gemini-2.0-flash-preview-image-generation",
contents="Generate a simple cartoon cat sitting on a sunny windowsill",
config=types.GenerateContentConfig(
response_modalities=["Text", "Image"]
)
)
if image_response and image_response.candidates:
parts = image_response.candidates[0].content.parts
print(f"✅ Image generation response has {len(parts)} parts")
for i, part in enumerate(parts):
if hasattr(part, 'text') and part.text:
print(f" Part {i}: Text content found")
elif hasattr(part, 'inline_data') and part.inline_data:
print(f" Part {i}: Image data found")
else:
print(f" Part {i}: Unknown part type")
return True
else:
print("❌ Image generation failed - no response")
return False
else:
print("❌ Text generation failed - no response")
return False
except Exception as e:
print(f"❌ API test failed: {e}")
return False
if __name__ == "__main__":
success = test_api()
if success:
print("\n🎉 All tests passed! The picture book generator should work now.")
else:
print("\n😞 Tests failed. Please check your API key and internet connection.")