File size: 10,148 Bytes
01d5d83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"""
Test the Hugging Face V4 NDJSON endpoint with a real URL.
"""

import asyncio
import json

import httpx


async def test_hf_ndjson_endpoint():
    """Test HF V4 NDJSON endpoint with URL scraping."""
    
    # Hugging Face Space URL
    hf_space_url = "https://colin730-summarizerapp.hf.space"
    
    url = "https://www.nzherald.co.nz/nz/auckland/mt-wellington-homicide-jury-find-couple-not-guilty-of-murder-after-soldier-stormed-their-house-with-knife/B56S6KBHRVFCZMLDI56AZES6KY/"
    
    print("=" * 80)
    print("Hugging Face V4 NDJSON Endpoint Test")
    print("=" * 80)
    print(f"\nHF Space: {hf_space_url}")
    print(f"Endpoint: {hf_space_url}/api/v4/scrape-and-summarize/stream-ndjson")
    print(f"Article URL: {url[:80]}...")
    print(f"Style: executive\n")
    
    payload = {
        "url": url,
        "style": "executive",
        "max_tokens": 512,
        "include_metadata": True,
        "use_cache": True,
    }
    
    # Longer timeout for HF (first request can be slow if cold start)
    async with httpx.AsyncClient(timeout=600.0) as client:
        try:
            print("πŸ”„ Sending request to Hugging Face...")
            print("⏱️  Note: First request may take 30-60s if instance is cold\n")
            
            # Make streaming request
            async with client.stream(
                "POST",
                f"{hf_space_url}/api/v4/scrape-and-summarize/stream-ndjson",
                json=payload,
            ) as response:
                print(f"Status: {response.status_code}")
                
                if response.status_code != 200:
                    error_text = await response.aread()
                    error_str = error_text.decode()
                    print(f"\n❌ Error Response:")
                    print(error_str)
                    
                    # Check if it's a 404 (endpoint not found)
                    if response.status_code == 404:
                        print("\nπŸ’‘ The endpoint might not be deployed yet.")
                        print("   The HF Space may still be building (~5-10 minutes).")
                        print(f"   Check status at: https://huggingface.co/spaces/colin730/SummarizerApp")
                    return
                
                print("\n" + "=" * 80)
                print("STREAMING EVENTS")
                print("=" * 80)
                
                event_count = 0
                final_state = None
                total_tokens = 0
                metadata = None
                
                # Parse SSE stream
                async for line in response.aiter_lines():
                    if line.startswith("data: "):
                        try:
                            event = json.loads(line[6:])
                            
                            # Handle metadata event
                            if event.get("type") == "metadata":
                                metadata = event["data"]
                                print("\n--- Metadata Event ---")
                                print(json.dumps(metadata, indent=2))
                                print("\n" + "-" * 80)
                                continue
                            
                            event_count += 1
                            
                            # Check for error
                            if "error" in event:
                                print(f"\n❌ ERROR: {event['error']}")
                                
                                if "model not available" in event['error'].lower():
                                    print("\nπŸ’‘ This means:")
                                    print("   - The endpoint is working βœ…")
                                    print("   - Scraping is working βœ…")
                                    print("   - But the model isn't loaded on HF")
                                    print("   - This is expected if PyTorch/transformers aren't installed")
                                return
                            
                            # Extract event data
                            delta = event.get("delta")
                            state = event.get("state")
                            done = event.get("done", False)
                            tokens_used = event.get("tokens_used", 0)
                            latency_ms = event.get("latency_ms")
                            
                            total_tokens = tokens_used
                            
                            # Print event details (compact format)
                            if delta and "op" in delta:
                                op = delta.get("op")
                                if op == "set":
                                    field = delta.get("field")
                                    value = delta.get("value")
                                    value_str = str(value)[:60] + "..." if len(str(value)) > 60 else str(value)
                                    print(f"Event #{event_count}: Set {field} = {value_str}")
                                elif op == "append":
                                    field = delta.get("field")
                                    value = delta.get("value")
                                    value_str = str(value)[:60] + "..." if len(str(value)) > 60 else str(value)
                                    print(f"Event #{event_count}: Append to {field}: {value_str}")
                                elif op == "done":
                                    print(f"Event #{event_count}: βœ… Done signal received")
                            elif delta is None and done:
                                print(f"Event #{event_count}: 🏁 Final event (latency: {latency_ms}ms)")
                            
                            # Store final state
                            if state:
                                final_state = state
                        
                        except json.JSONDecodeError as e:
                            print(f"Failed to parse JSON: {e}")
                            print(f"Raw line: {line}")
                
                # Print final results
                print("\n" + "=" * 80)
                print("FINAL RESULTS")
                print("=" * 80)
                
                if metadata:
                    print(f"\n--- Scraping Info ---")
                    print(f"Input type: {metadata.get('input_type')}")
                    print(f"Article title: {metadata.get('title')}")
                    print(f"Site: {metadata.get('site_name')}")
                    print(f"Scrape method: {metadata.get('scrape_method')}")
                    print(f"Scrape latency: {metadata.get('scrape_latency_ms', 0):.2f}ms")
                    print(f"Text extracted: {metadata.get('extracted_text_length', 0)} chars")
                
                print(f"\nTotal events: {event_count}")
                print(f"Total tokens: {total_tokens}")
                
                if final_state:
                    print("\n--- Final Structured State ---")
                    print(json.dumps(final_state, indent=2, ensure_ascii=False))
                    
                    # Validate structure
                    print("\n--- Validation ---")
                    required_fields = ["title", "main_summary", "key_points", "category", "sentiment", "read_time_min"]
                    
                    all_valid = True
                    for field in required_fields:
                        value = final_state.get(field)
                        if field == "key_points":
                            if isinstance(value, list) and len(value) > 0:
                                print(f"βœ… {field}: {len(value)} items")
                            else:
                                print(f"⚠️  {field}: empty or not a list")
                                all_valid = False
                        else:
                            if value is not None:
                                value_str = str(value)[:50] + "..." if len(str(value)) > 50 else str(value)
                                print(f"βœ… {field}: {value_str}")
                            else:
                                print(f"⚠️  {field}: None")
                                all_valid = False
                    
                    # Check sentiment is valid
                    sentiment = final_state.get("sentiment")
                    valid_sentiments = ["positive", "negative", "neutral"]
                    if sentiment in valid_sentiments:
                        print(f"βœ… sentiment value is valid: {sentiment}")
                    else:
                        print(f"⚠️  sentiment value is invalid: {sentiment}")
                        all_valid = False
                    
                    print("\n" + "=" * 80)
                    if all_valid:
                        print("βœ… ALL VALIDATIONS PASSED - HF ENDPOINT WORKING!")
                    else:
                        print("⚠️  Some validations failed")
                    print("=" * 80)
                else:
                    print("\n⚠️  No final state received")
        
        except httpx.ConnectError:
            print(f"\n❌ Could not connect to {hf_space_url}")
            print("\nπŸ’‘ Possible reasons:")
            print("   1. HF Space is still building/deploying")
            print("   2. HF Space is sleeping (free tier)")
            print("   3. Network connectivity issue")
            print(f"\nπŸ”— Check space status: https://huggingface.co/spaces/colin730/SummarizerApp")
        except httpx.ReadTimeout:
            print("\n⏱️  Request timed out")
            print("   This might mean the HF Space is cold-starting")
            print("   Try again in a few moments")
        except Exception as e:
            print(f"\n❌ Error: {e}")
            import traceback
            traceback.print_exc()


if __name__ == "__main__":
    print("\nπŸš€ Testing Hugging Face V4 NDJSON Endpoint\n")
    asyncio.run(test_hf_ndjson_endpoint())