File size: 7,472 Bytes
01a84b4
 
 
20929ce
01a84b4
 
 
 
 
20929ce
61c663e
 
 
 
a272826
 
 
 
 
 
 
 
 
01a84b4
20929ce
a272826
20929ce
01a84b4
a272826
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01a84b4
 
a272826
01a84b4
20929ce
 
 
 
a272826
 
 
 
 
 
 
 
 
61c663e
 
a272826
 
20929ce
01a84b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61c663e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01a84b4
 
61c663e
01a84b4
 
61c663e
 
 
 
 
 
 
01a84b4
 
 
 
 
61c663e
01a84b4
 
 
 
 
 
61c663e
 
 
 
 
 
 
 
01a84b4
 
 
 
 
61c663e
01a84b4
 
 
 
 
 
61c663e
 
 
 
 
 
 
 
 
01a84b4
 
 
 
20929ce
a272826
 
20929ce
61c663e
 
 
 
a272826
61c663e
a272826
61c663e
01a84b4
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/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()