#!/usr/bin/env python3 """ Test script to verify the new API endpoint returns the exact JSON format """ import requests import json import time def test_api_endpoints(): """Test all API endpoints including the new question-specific ones.""" # Base URL - change this to your actual API URL base_url = "http://localhost:8000" # For local testing # base_url = "https://your-huggingface-space-url" # For production # Test data sample_essay = """ Climate change is one of the most pressing issues facing our world today. The scientific consensus is clear: human activities, particularly the burning of fossil fuels, are causing global temperatures to rise at an unprecedented rate. The consequences of climate change are far-reaching and severe. Rising sea levels threaten coastal communities, extreme weather events are becoming more frequent and intense, and ecosystems around the world are being disrupted. These changes affect not only the environment but also human health, food security, and economic stability. To address climate change, we need a comprehensive approach that includes reducing greenhouse gas emissions, transitioning to renewable energy sources, and implementing policies that promote sustainability. Governments, businesses, and individuals all have a role to play in this effort. While the challenge is daunting, there are reasons for optimism. Renewable energy technologies are becoming more affordable and efficient, and many countries are committing to ambitious climate goals. By working together, we can create a more sustainable future for generations to come. """ sample_question = "Discuss the causes and effects of climate change, and suggest solutions to address this global challenge." print("šŸš€ Testing CSS Essay Grader API Endpoints") print("=" * 50) # Test 1: Health Check print("\n1. Testing Health Check...") try: response = requests.get(f"{base_url}/health") if response.status_code == 200: print("āœ… Health check passed") print(f" Status: {response.json().get('status', 'unknown')}") else: print(f"āŒ Health check failed: {response.status_code}") except Exception as e: print(f"āŒ Health check error: {str(e)}") # Test 2: Original Essay Analysis (without question) print("\n2. Testing Original Essay Analysis (without question)...") try: response = requests.post( f"{base_url}/api/essay-analysis", data={"essay_text": sample_essay}, headers={"Content-Type": "application/x-www-form-urlencoded"} ) if response.status_code == 200: result = response.json() print("āœ… Original essay analysis successful") print(f" Overall Score: {result.get('overallEssayEvaluationScore', 'N/A')}") print(f" Original Word Count: {result.get('originalEssayWordCount', 'N/A')}") print(f" Rewritten Word Count: {result.get('reWrittenEssayWordCount', 'N/A')}") print(f" Analysis Type: {result.get('analysisType', 'N/A')}") # Save response to file with open("api_response_original.json", "w") as f: json.dump(result, f, indent=2) print(" šŸ“„ Response saved to api_response_original.json") else: print(f"āŒ Original essay analysis failed: {response.status_code}") print(f" Error: {response.text}") except Exception as e: print(f"āŒ Original essay analysis error: {str(e)}") # Test 2b: Original Essay Analysis (with question) print("\n2b. Testing Original Essay Analysis (with question)...") try: response = requests.post( f"{base_url}/api/essay-analysis", data={ "essay_text": sample_essay, "question": sample_question }, headers={"Content-Type": "application/x-www-form-urlencoded"} ) if response.status_code == 200: result = response.json() print("āœ… Original essay analysis with question successful") print(f" Overall Score: {result.get('overallEssayEvaluationScore', 'N/A')}") print(f" Question: {result.get('question', 'N/A')}") print(f" Original Word Count: {result.get('originalEssayWordCount', 'N/A')}") print(f" Rewritten Word Count: {result.get('reWrittenEssayWordCount', 'N/A')}") print(f" Analysis Type: {result.get('analysisType', 'N/A')}") # Check for question-specific feedback question_feedback = result.get('questionSpecificFeedback', {}) if question_feedback: print(f" Question Relevance Score: {question_feedback.get('question_relevance_score', 'N/A')}") print(f" Question Coverage: {question_feedback.get('question_coverage', 'N/A')[:100]}...") # Save response to file with open("api_response_original_with_question.json", "w") as f: json.dump(result, f, indent=2) print(" šŸ“„ Response saved to api_response_original_with_question.json") else: print(f"āŒ Original essay analysis with question failed: {response.status_code}") print(f" Error: {response.text}") except Exception as e: print(f"āŒ Original essay analysis with question error: {str(e)}") # Test 3: Original Feedback (without question) print("\n3. Testing Original Feedback (without question)...") try: response = requests.post( f"{base_url}/api/feedback", data={"essay_text": sample_essay}, headers={"Content-Type": "application/x-www-form-urlencoded"} ) if response.status_code == 200: result = response.json() print("āœ… Original feedback successful") print(f" Feedback Type: {result.get('feedback_type', 'N/A')}") print(f" Question: {result.get('question', 'N/A')}") feedback = result.get('feedback', {}) if feedback: print(f" Overall Score: {feedback.get('overall_score', 'N/A')}") sections = feedback.get('sections', []) print(f" Number of Feedback Sections: {len(sections)}") # Save response to file with open("api_response_feedback_original.json", "w") as f: json.dump(result, f, indent=2) print(" šŸ“„ Response saved to api_response_feedback_original.json") else: print(f"āŒ Original feedback failed: {response.status_code}") print(f" Error: {response.text}") except Exception as e: print(f"āŒ Original feedback error: {str(e)}") # Test 3b: Original Feedback (with question) print("\n3b. Testing Original Feedback (with question)...") try: response = requests.post( f"{base_url}/api/feedback", data={ "essay_text": sample_essay, "question": sample_question }, headers={"Content-Type": "application/x-www-form-urlencoded"} ) if response.status_code == 200: result = response.json() print("āœ… Original feedback with question successful") print(f" Feedback Type: {result.get('feedback_type', 'N/A')}") print(f" Question: {result.get('question', 'N/A')}") feedback = result.get('feedback', {}) if feedback: print(f" Overall Score: {feedback.get('overall_score', 'N/A')}") sections = feedback.get('sections', []) print(f" Number of Feedback Sections: {len(sections)}") # Show question-specific feedback if available question_feedback = feedback.get('question_specific_feedback', {}) if question_feedback: print(f" Question Relevance Score: {question_feedback.get('question_relevance_score', 'N/A')}") # Save response to file with open("api_response_feedback_original_with_question.json", "w") as f: json.dump(result, f, indent=2) print(" šŸ“„ Response saved to api_response_feedback_original_with_question.json") else: print(f"āŒ Original feedback with question failed: {response.status_code}") print(f" Error: {response.text}") except Exception as e: print(f"āŒ Original feedback with question error: {str(e)}") # Test 4: NEW - Essay Analysis with Question (dedicated endpoint) print("\n4. Testing NEW Essay Analysis with Question (dedicated endpoint)...") try: response = requests.post( f"{base_url}/api/essay-analysis-with-question", data={ "essay_text": sample_essay, "question": sample_question }, headers={"Content-Type": "application/x-www-form-urlencoded"} ) if response.status_code == 200: result = response.json() print("āœ… Essay analysis with question successful") print(f" Overall Score: {result.get('overallEssayEvaluationScore', 'N/A')}") print(f" Question: {result.get('question', 'N/A')}") print(f" Original Word Count: {result.get('originalEssayWordCount', 'N/A')}") print(f" Rewritten Word Count: {result.get('reWrittenEssayWordCount', 'N/A')}") # Check for question-specific feedback question_feedback = result.get('questionSpecificFeedback', {}) if question_feedback: print(f" Question Relevance Score: {question_feedback.get('question_relevance_score', 'N/A')}") print(f" Question Coverage: {question_feedback.get('question_coverage', 'N/A')[:100]}...") # Save response to file with open("api_response_with_question.json", "w") as f: json.dump(result, f, indent=2) print(" šŸ“„ Response saved to api_response_with_question.json") else: print(f"āŒ Essay analysis with question failed: {response.status_code}") print(f" Error: {response.text}") except Exception as e: print(f"āŒ Essay analysis with question error: {str(e)}") # Test 5: NEW - Feedback with Question (dedicated endpoint) print("\n5. Testing NEW Feedback with Question (dedicated endpoint)...") try: response = requests.post( f"{base_url}/api/feedback-with-question", data={ "essay_text": sample_essay, "question": sample_question }, headers={"Content-Type": "application/x-www-form-urlencoded"} ) if response.status_code == 200: result = response.json() print("āœ… Feedback with question successful") print(f" Question: {result.get('question', 'N/A')}") feedback = result.get('feedback', {}) if feedback: print(f" Overall Score: {feedback.get('overall_score', 'N/A')}") sections = feedback.get('sections', []) print(f" Number of Feedback Sections: {len(sections)}") # Show question-specific feedback if available question_feedback = feedback.get('question_specific_feedback', {}) if question_feedback: print(f" Question Relevance Score: {question_feedback.get('question_relevance_score', 'N/A')}") # Save response to file with open("api_response_feedback_with_question.json", "w") as f: json.dump(result, f, indent=2) print(" šŸ“„ Response saved to api_response_feedback_with_question.json") else: print(f"āŒ Feedback with question failed: {response.status_code}") print(f" Error: {response.text}") except Exception as e: print(f"āŒ Feedback with question error: {str(e)}") print("\n" + "=" * 50) print("šŸŽ‰ API Testing Complete!") print("\nšŸ“‹ Summary:") print("- Original endpoints now support optional question parameters") print("- NEW dedicated question-specific endpoints added successfully") print("- All responses saved to JSON files for inspection") print("\nšŸ”— Key Features:") print("1. /api/essay-analysis - Works with or without question parameter") print("2. /api/feedback - Works with or without question parameter") print("3. /api/essay-analysis-with-question - Dedicated question endpoint") print("4. /api/feedback-with-question - Dedicated question endpoint") print("\nšŸ”— Next Steps:") print("1. Review the generated JSON files to verify response format") print("2. Compare question-specific vs general feedback") print("3. Test with different questions and essay topics") print("4. Integrate the new endpoints into your application") if __name__ == "__main__": test_api_endpoints()