satishsnv
fixed label
a272826
Raw History Blame Contribute Delete
7.47 kB
#!/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()