import requests import numpy as np import pandas as pd import json # Your deployed Hugging Face Space URL SPACE_URL = "https://mystic-cbk-ecg-fm-api.hf.space" def load_ecg_data(csv_file): """Load ECG data from CSV file""" print(f"๐Ÿ“ Loading ECG data from: {csv_file}") # Read the CSV file df = pd.read_csv(csv_file) print(f"๐Ÿ“Š ECG Data Shape: {df.shape}") print(f"๐Ÿ“Š Leads: {list(df.columns)}") print(f"๐Ÿ“Š Samples per lead: {len(df)}") # Convert to the format expected by the API # Each lead should be a list of float values ecg_data = [] for lead in df.columns: ecg_data.append(df[lead].astype(float).tolist()) print(f"โœ… ECG data loaded successfully!") print(f"๐Ÿ“Š Data format: {len(ecg_data)} leads ร— {len(ecg_data[0])} samples") return ecg_data def test_api_with_real_ecg(): """Test the deployed ECG-FM API with real ECG data""" print("๐Ÿงช Testing ECG-FM API with Real ECG Data") print("=" * 60) print(f"๐ŸŒ API URL: {SPACE_URL}") print() # Load the real ECG data try: ecg_data = load_ecg_data("ecg_d2c4fdc0-01ee-4210-a027-54a527329cd8.csv") except Exception as e: print(f"โŒ Error loading ECG data: {e}") return print() # Test 1: Health check print("1๏ธโƒฃ Testing health endpoint...") try: r = requests.get(f"{SPACE_URL}/healthz", timeout=30) print(f" Status: {r.status_code}") if r.status_code == 200: response = r.json() print(f" Response: {response}") print(f" Mode: {response.get('mode', 'unknown')}") else: print(f" Error: {r.text}") except Exception as e: print(f" โŒ Error: {e}") print() # Test 2: Root endpoint print("2๏ธโƒฃ Testing root endpoint...") try: r = requests.get(f"{SPACE_URL}/", timeout=30) print(f" Status: {r.status_code}") if r.status_code == 200: response = r.json() print(f" Response: {response}") else: print(f" Error: {r.text}") except Exception as e: print(f" โŒ Error: {e}") print() # Test 3: Predict endpoint with REAL ECG data print("3๏ธโƒฃ Testing predict endpoint with REAL ECG data...") # Prepare the payload with real ECG data payload = { "signal": ecg_data, "fs": 500 # 500 Hz sampling rate } try: print(" ๐Ÿ“ค Sending REAL ECG data...") print(f" ๐Ÿ“Š Input: 12 leads ร— {len(ecg_data[0])} samples") print(f" ๐Ÿ“Š Sampling rate: 500 Hz") print(f" ๐Ÿ“Š Duration: {len(ecg_data[0])/500:.1f} seconds") print(" โณ Waiting for inference...") r = requests.post(f"{SPACE_URL}/predict", json=payload, timeout=180) # 3 min timeout print(f" Status: {r.status_code}") if r.status_code == 200: result = r.json() print(f" โœ… Success!") print(f" ๐Ÿ“Š Mode: {result.get('mode', 'unknown')}") print(f" ๐Ÿ“Š Output shape: {result.get('output_shape', 'unknown')}") print(f" ๐Ÿ“Š Input shape: {result.get('input_shape', 'unknown')}") print(f" ๐Ÿ“Š Output length: {len(result.get('output', []))}") if result.get('mode') == 'fallback': print(f" ๐Ÿ’ก Note: {result.get('note', 'Running in fallback mode')}") else: print(f" ๐ŸŽฏ Full ECG-FM model inference completed!") else: print(f" โŒ Error: {r.text}") except requests.exceptions.Timeout: print(" โฐ Timeout - Inference taking longer than expected") except Exception as e: print(f" โŒ Error: {e}") print() print("=" * 60) print("๐Ÿ Real ECG Testing Complete!") print() print("๐Ÿ“Š Results Summary:") print(" - If you see 200 status codes: API is working with real data!") print(" - If you see fallback mode: API works but model not fully loaded") print(" - If you see full mode: Complete ECG-FM inference working!") print() print("๐Ÿ”— Your API is live at:") print(f" {SPACE_URL}") print() print("๐Ÿ“ฑ Test manually:") print(f" Health: {SPACE_URL}/healthz") print(f" Root: {SPACE_URL}/") if __name__ == "__main__": test_api_with_real_ecg()