Spaces:
No application file
No application file
| #!/usr/bin/env python3 | |
| """ | |
| Simple test script for the FastAPI TextGen API. | |
| Run this script to test the API endpoints locally. | |
| """ | |
| import requests | |
| import json | |
| import sys | |
| def test_health_endpoint(base_url): | |
| """Test the health check endpoint.""" | |
| print("🔍 Testing health endpoint...") | |
| try: | |
| response = requests.get(f"{base_url}/health") | |
| if response.status_code == 200: | |
| data = response.json() | |
| print(f"✅ Health check passed: {data['message']}") | |
| print(f"🔑 OpenAI configured: {data['openai_configured']}") | |
| return True | |
| else: | |
| print(f"❌ Health check failed with status: {response.status_code}") | |
| return False | |
| except requests.exceptions.RequestException as e: | |
| print(f"❌ Health check failed: {e}") | |
| return False | |
| def test_generate_endpoint(base_url, question): | |
| """Test the generate endpoint with a question.""" | |
| print(f"\n💭 Testing question: '{question}'") | |
| try: | |
| response = requests.post( | |
| f"{base_url}/api/generate", | |
| json={"question": question}, | |
| headers={"Content-Type": "application/json"} | |
| ) | |
| if response.status_code == 200: | |
| data = response.json() | |
| print("✅ Response received:") | |
| print(f"📝 Answer: {data['text'][:200]}{'...' if len(data['text']) > 200 else ''}") | |
| return True | |
| else: | |
| print(f"❌ Request failed with status: {response.status_code}") | |
| print(f"📄 Response: {response.text}") | |
| return False | |
| except requests.exceptions.RequestException as e: | |
| print(f"❌ Request failed: {e}") | |
| return False | |
| def main(): | |
| """Main test function.""" | |
| base_url = "http://localhost:8000" | |
| print("🚀 FastAPI TextGen API Test Suite") | |
| print("=" * 50) | |
| # Test health endpoint | |
| if not test_health_endpoint(base_url): | |
| print("\n❌ Health check failed. Make sure the server is running.") | |
| print("💡 Start the server with: python dev_run.py") | |
| sys.exit(1) | |
| # Test questions | |
| test_questions = [ | |
| "What is artificial intelligence?", | |
| "Explain Python programming in one sentence", | |
| "What are the benefits of renewable energy?", | |
| ] | |
| print(f"\n🧪 Testing {len(test_questions)} questions...") | |
| success_count = 0 | |
| for question in test_questions: | |
| if test_generate_endpoint(base_url, question): | |
| success_count += 1 | |
| print(f"\n📊 Test Results: {success_count}/{len(test_questions)} tests passed") | |
| if success_count == len(test_questions): | |
| print("🎉 All tests passed! Your API is working correctly.") | |
| else: | |
| print("⚠️ Some tests failed. Check your OpenAI API key and configuration.") | |
| if __name__ == "__main__": | |
| main() |