Spaces:
Sleeping
Sleeping
File size: 13,447 Bytes
459923e |
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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
#!/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() |