Spaces:
Sleeping
Sleeping
Download test.py from snvskiit/content_processor: direct link, hf CLI and curl.
- Browser
- Download file 7.47 kB
-
https://huggingface.co/spaces/snvskiit/content_processor/resolve/main/test.py
- Command line
-
hf download hf://spaces/snvskiit/content_processor/test.py
-
curl -L -o test.py https://huggingface.co/spaces/snvskiit/content_processor/resolve/main/test.py
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() | |