Spaces:
Sleeping
Sleeping
File size: 1,860 Bytes
8f81868 | 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 | """
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() |