Spaces:
Sleeping
Sleeping
| """ | |
| Test script to verify FastAPI app works locally | |
| """ | |
| import subprocess | |
| import time | |
| import requests | |
| import threading | |
| import sys | |
| def start_server(): | |
| """Start the FastAPI server in background""" | |
| try: | |
| subprocess.run([ | |
| sys.executable, "-m", "uvicorn", | |
| "app:app", "--host", "127.0.0.1", "--port", "8000" | |
| ], check=False) | |
| except Exception as e: | |
| print(f"Server failed to start: {e}") | |
| def test_endpoints(): | |
| """Test the API endpoints""" | |
| base_url = "http://127.0.0.1:8000" | |
| # Wait a moment for server to start | |
| time.sleep(3) | |
| try: | |
| # Test health endpoint | |
| print("๐น Testing health endpoint...") | |
| health_resp = requests.get(base_url, timeout=5) | |
| print(f"Health Status: {health_resp.status_code}") | |
| if health_resp.status_code == 200: | |
| print(f"Health Response: {health_resp.json()}") | |
| else: | |
| print(f"Health Error: {health_resp.text}") | |
| # Test prediction endpoint | |
| print("\n๐น Testing prediction endpoint...") | |
| pred_resp = requests.post( | |
| base_url, | |
| json={"text": "Congratulations! You've won a free cruise!"}, | |
| headers={"Content-Type": "application/json"}, | |
| timeout=10 | |
| ) | |
| print(f"Prediction Status: {pred_resp.status_code}") | |
| if pred_resp.status_code == 200: | |
| print(f"Prediction Response: {pred_resp.json()}") | |
| else: | |
| print(f"Prediction Error: {pred_resp.text}") | |
| except Exception as e: | |
| print(f"Test failed: {e}") | |
| if __name__ == "__main__": | |
| print("Starting FastAPI server...") | |
| # Start server in a separate thread | |
| server_thread = threading.Thread(target=start_server, daemon=True) | |
| server_thread.start() | |
| # Run tests | |
| test_endpoints() |