""" Test script for stance analysis integration Tests the complete pipeline: 1. Summary generation 2. Embedding generation 3. Stance analysis """ import requests import json def test_batch_process_with_stance(): """Test /batch-process-articles endpoint with stance analysis""" # Test data: Korean political news articles test_articles = [ { "article_id": 1, "title": "정부 부동산 규제 완화", "content": "정부가 오늘 부동산 규제 완화 방안을 발표했다. 이번 조치로 주택 구매가 더 쉬워질 전망이다. " "전문가들은 이번 정책이 경제 활성화에 도움이 될 것으로 기대하고 있다." }, { "article_id": 2, "title": "야당 정부 정책 비판", "content": "야당은 오늘 정부의 정책에 대해 강하게 비판했다. 야당 대표는 이번 정책이 서민들에게 " "도움이 되지 않는다고 주장했다. 야당은 정부가 재검토해야 한다고 촉구했다." }, { "article_id": 3, "title": "국회 법안 심의", "content": "국회에서 오늘 법안 심의가 진행되었다. 여야 의원들이 참석한 가운데 다양한 의견이 " "제시되었다. 법안은 다음 주 본회의에 상정될 예정이다." } ] # API endpoint url = "http://localhost:7860/batch-process-articles" # Request payload payload = { "articles": test_articles, "max_summary_length": 300, "min_summary_length": 150 } print("Testing /batch-process-articles endpoint...") print(f"Sending {len(test_articles)} articles\n") try: # Send request response = requests.post(url, json=payload, timeout=120) response.raise_for_status() # Parse response result = response.json() print("=" * 80) print("RESPONSE SUMMARY") print("=" * 80) print(f"Total processed: {result['total_processed']}") print(f"Successful: {result['successful']}") print(f"Failed: {result['failed']}") print(f"Processing time: {result['processing_time_seconds']:.2f}s") print() # Display results for i, article_result in enumerate(result['results'], 1): print("=" * 80) print(f"ARTICLE {i}: {test_articles[i-1]['title']}") print("=" * 80) # Original content print(f"\nOriginal (first 100 chars):") print(f" {test_articles[i-1]['content'][:100]}...") # Summary if article_result['summary']: print(f"\nSummary:") print(f" {article_result['summary']}") else: print(f"\nSummary: FAILED - {article_result.get('error')}") # Embedding if article_result['embedding']: print(f"\nEmbedding:") print(f" Dimension: {len(article_result['embedding'])}") print(f" First 5 values: {article_result['embedding'][:5]}") else: print(f"\nEmbedding: Not generated") # Stance if article_result['stance']: stance = article_result['stance'] print(f"\nStance Analysis:") print(f" Label: {stance['stance_label'].upper()}") print(f" Score: {stance['stance_score']:.4f}") print(f" Probabilities:") print(f" - Support (옹호): {stance['prob_positive']:.4f}") print(f" - Neutral (중립): {stance['prob_neutral']:.4f}") print(f" - Oppose (비판): {stance['prob_negative']:.4f}") else: print(f"\nStance: Not analyzed") print() print("=" * 80) print("TEST COMPLETED SUCCESSFULLY") print("=" * 80) except requests.exceptions.RequestException as e: print(f"ERROR: Request failed") print(f" {e}") if hasattr(e.response, 'text'): print(f" Response: {e.response.text}") except Exception as e: print(f"ERROR: {e}") def test_health_check(): """Test /health endpoint to verify all models are loaded""" url = "http://localhost:7860/health" print("Testing /health endpoint...") try: response = requests.get(url, timeout=10) response.raise_for_status() result = response.json() print("\n" + "=" * 80) print("HEALTH CHECK") print("=" * 80) print(f"Status: {result['status']}") print(f"Device: {result['device']}") print(f"\nModels loaded:") print(f" - Summarization: {result['summarization_model']}") print(f" - Embedding: {result['embedding_model']}") print(f" - Stance: {result['stance_model']}") print("=" * 80) print() if result['stance_model'] is None: print("WARNING: Stance model not loaded!") return False return True except Exception as e: print(f"ERROR: Health check failed - {e}") return False if __name__ == "__main__": print("\n" + "=" * 80) print("STANCE ANALYSIS INTEGRATION TEST") print("=" * 80) print() # Step 1: Health check if test_health_check(): print("\n✓ Health check passed\n") # Step 2: Test batch processing with stance test_batch_process_with_stance() else: print("\n✗ Health check failed - skipping batch test") print("\nMake sure the API server is running:") print(" python app.py")