#!/usr/bin/env python3 """ Simple test script for the deployed API Update BASE_URL to your Hugging Face Space URL or local server """ import httpx import json import os import sys from dotenv import load_dotenv load_dotenv() # Load environment variables from .env file # Possible Hugging Face Space URLs to try - Common formats for HF Spaces POSSIBLE_URLS = [ "https://snvskiit-content-processor.hf.space", # Standard format: username-spacename "https://snvskiit-content_processor.hf.space", # With underscore "https://content-processor-snvskiit.hf.space", # Reverse order with dash "https://content_processor-snvskiit.hf.space", # Reverse order with underscore "https://huggingface.co/spaces/snvskiit/content-processor", # Direct HF URL with dash "https://huggingface.co/spaces/snvskiit/content_processor", # Direct HF URL with underscore ] # Update this to your actual Hugging Face Space URL or use "local" for localhost:7860 BASE_URL = "https://snvskiit-content-processor.hf.space" # Working endpoint! # BASE_URL = "http://localhost:7860" # Uncomment for local testing def find_working_hf_endpoint(): """Try different HF Space URL formats to find the working one""" print("๐Ÿ” Searching for working Hugging Face Space endpoint...") print("=" * 60) for i, url in enumerate(POSSIBLE_URLS, 1): print(f"\n{i}. Testing: {url}") try: with httpx.Client(timeout=10.0, verify=False) as client: response = client.get(f"{url}/health") if response.status_code == 200: print(f" โœ… SUCCESS! Found working endpoint") try: data = response.json() print(f" ๐Ÿ“Š Response: {data}") return url except: print(f" ๐Ÿ“ Response (non-JSON): {response.text[:100]}...") return url elif response.status_code == 404: print(f" โŒ 404 - Space not found at this URL") else: print(f" โš ๏ธ Status {response.status_code} - {response.text[:50]}...") except Exception as e: print(f" โŒ Connection failed: {str(e)[:50]}...") print(f"\n๐Ÿšซ No working endpoint found. Please check:") print(f" โ€ข Your Hugging Face username") print(f" โ€ข Your Space name") print(f" โ€ข That the Space is deployed and running") return None def test_api(): """Test the deployed API""" global BASE_URL # Handle local testing if len(sys.argv) > 1 and sys.argv[1] == "local": BASE_URL = "http://localhost:7860" print("๐Ÿ  Testing local server...") elif len(sys.argv) > 1 and sys.argv[1] == "find": # Find working endpoint mode working_url = find_working_hf_endpoint() if working_url: BASE_URL = working_url print(f"\n๐ŸŽฏ Using found endpoint: {BASE_URL}") else: print(f"\nโš ๏ธ Falling back to default: {BASE_URL}") print("\n" + "=" * 60) else: print("๐ŸŒ Testing Hugging Face Space deployment...") print("๐Ÿ” If 404 errors occur, try: python test.py find") print(" This will test all possible URL formats") # Get OpenAI API key from environment api_key = os.getenv("OPENAI_API_KEY") if not api_key: print("โŒ Please set OPENAI_API_KEY environment variable") print(" export OPENAI_API_KEY='your_actual_key'") return headers = { "Content-Type": "application/json", "x-api-key": api_key } print(f"๐Ÿงช Testing API at: {BASE_URL}") print("=" * 50) # Test root endpoint first try: with httpx.Client(timeout=30.0, verify=False) as client: response = client.get(f"{BASE_URL}/") print(f"โœ… Root Endpoint: {response.status_code}") if response.status_code == 200: try: print(f" {response.json()}") except: print(f" Response: {response.text[:200]}...") else: print(f" Error response: {response.text[:200]}...") except Exception as e: print(f"โŒ Root endpoint failed: {e}") # Test health endpoint try: with httpx.Client(timeout=30.0, verify=False) as client: response = client.get(f"{BASE_URL}/health") print(f"โœ… Health Check: {response.status_code}") if response.status_code == 200: try: print(f" {response.json()}") except: print(f" Response: {response.text[:200]}...") else: print(f" Error response: {response.text[:200]}...") except Exception as e: print(f"โŒ Health check failed: {e}") # Test address detection try: with httpx.Client(timeout=30.0, verify=False) as client: data = { "entity_urn": "test_001", "content": "Send package to 123 Main Street, New York, NY 10001" } response = client.post(f"{BASE_URL}/address-detection", json=data, headers=headers) print(f"โœ… Address Detection: {response.status_code}") if response.status_code == 200: try: result = response.json() print(f" Addresses: {result.get('result', 'N/A')}") except: print(f" Response: {response.text[:200]}...") else: print(f" Error response: {response.text[:200]}...") except Exception as e: print(f"โŒ Address detection failed: {e}") # Test summarization try: with httpx.Client(timeout=30.0, verify=False) as client: data = { "entity_urn": "test_002", "content": "I love this product! It's amazing and exceeded my expectations." } response = client.post(f"{BASE_URL}/summarize", json=data, headers=headers) print(f"โœ… Summarization: {response.status_code}") if response.status_code == 200: try: result = response.json() print(f" Summary: {result.get('result', 'N/A')}") print(f" Sentiment: {result.get('sentiment', 'N/A')}") except: print(f" Response: {response.text[:200]}...") else: print(f" Error response: {response.text[:200]}...") except Exception as e: print(f"โŒ Summarization failed: {e}") print(f"\n๐Ÿ“– View docs at: {BASE_URL}/docs") print(f"\n๐Ÿ’ก Usage:") print(f" python test.py # Test default Hugging Face deployment") print(f" python test.py find # Auto-discover working HF endpoint") print(f" python test.py local # Test local server") if "hf.space" in BASE_URL: print(f"\n๐Ÿ”ง Troubleshooting Hugging Face Space:") print(f" โ€ข Check if Space is active: {BASE_URL}") print(f" โ€ข Try: python test.py find") print(f" โ€ข Check Space logs for deployment errors") print(f" โ€ข Verify your username and space name are correct") if __name__ == "__main__": test_api()