donecase / test_gradio_setup.py
mr.saris kiattithapanayong
update the code that demoed on saturday 22 nov
3d142aa
"""
Simple test script to verify the Gradio setup
Run this before launching the full app to check configuration
"""
import os
import sys
from dotenv import load_dotenv
print("=" * 60)
print("πŸ§ͺ Gradio Setup Verification")
print("=" * 60)
# Test 1: Check environment file
print("\n1️⃣ Checking environment file...")
env_path = os.path.join(os.path.dirname(__file__), "rag_agent", ".env")
if os.path.exists(env_path):
print(f" βœ… Found .env file at: {env_path}")
load_dotenv(env_path)
else:
print(f" ❌ .env file not found at: {env_path}")
sys.exit(1)
# Test 2: Check environment variables
print("\n2️⃣ Checking environment variables...")
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT")
LOCATION = os.environ.get("GOOGLE_CLOUD_LOCATION")
if PROJECT_ID:
print(f" βœ… GOOGLE_CLOUD_PROJECT: {PROJECT_ID}")
else:
print(" ❌ GOOGLE_CLOUD_PROJECT not set")
sys.exit(1)
if LOCATION:
print(f" βœ… GOOGLE_CLOUD_LOCATION: {LOCATION}")
else:
print(" ⚠️ GOOGLE_CLOUD_LOCATION not set, defaulting to 'us-central1'")
LOCATION = "us-central1"
# Test 3: Check Python packages
print("\n3️⃣ Checking required packages...")
required_packages = {
"gradio": "Gradio UI framework",
"vertexai": "Vertex AI SDK",
"google.cloud.aiplatform_v1beta1": "AI Platform Client",
"dotenv": "Environment loader"
}
all_packages_ok = True
for package, description in required_packages.items():
try:
__import__(package.replace("-", "_"))
print(f" βœ… {package}: {description}")
except ImportError:
print(f" ❌ {package}: NOT INSTALLED - {description}")
all_packages_ok = False
if not all_packages_ok:
print("\n ⚠️ Missing packages. Install with:")
print(" pip install -r requirements.txt")
sys.exit(1)
# Test 4: Check Google Cloud authentication
print("\n4️⃣ Checking Google Cloud authentication...")
try:
from google.auth import default
credentials, project = default()
print(f" βœ… Authenticated with project: {project}")
except Exception as e:
print(f" ❌ Authentication error: {e}")
print(" Run: gcloud auth application-default login")
sys.exit(1)
# Test 5: Try to list agents
print("\n5️⃣ Testing Agent Engine connection...")
try:
from google.cloud import aiplatform_v1beta1 as aiplatform
client = aiplatform.AgentEnginesServiceClient(
client_options={"api_endpoint": f"{LOCATION}-aiplatform.googleapis.com"}
)
parent = f"projects/{PROJECT_ID}/locations/{LOCATION}"
request = aiplatform.ListAgentEnginesRequest(parent=parent)
agents = list(client.list_agent_engines(request=request))
if agents:
print(f" βœ… Successfully connected! Found {len(agents)} agent(s):")
for agent in agents:
display_name = agent.display_name or agent.name.split("/")[-1]
print(f" β€’ {display_name}")
else:
print(" ⚠️ Connected, but no agents found")
print(" Deploy an agent with: make deploy")
except Exception as e:
print(f" ❌ Connection error: {e}")
print(" Check your project ID and location")
sys.exit(1)
# All tests passed
print("\n" + "=" * 60)
print("βœ… All checks passed! Ready to launch Gradio app")
print("=" * 60)
print("\nRun the app with:")
print(" python gradio_app_v2.py")
print("\nOr:")
print(" python run_gradio.py")
print()