Spaces:
Running
Running
| # Working Python code with correct FileData format for Gradio | |
| from gradio_client import Client, handle_file | |
| import json | |
| try: | |
| print("π Connecting to Baby Cry Classifier...") | |
| client = Client("https://jitender1278-babycry.hf.space") | |
| print("π₯ Testing with correct FileData format...") | |
| # Method 1: Using handle_file() with URL | |
| try: | |
| print("π§ͺ Method 1: Using handle_file() with URL...") | |
| # handle_file() creates proper FileData object | |
| audio_file = handle_file("https://raw.githubusercontent.com/jiten-kmar/python-projects/main/baby-crying-32232.mp3") | |
| result = client.predict( | |
| audio_file, | |
| fn_index=0 # Try the web interface first | |
| ) | |
| print("β Method 1 with fn_index=0 worked!") | |
| print("Result type:", type(result)) | |
| print("Result:") | |
| if isinstance(result, (list, tuple)): | |
| for i, item in enumerate(result): | |
| print(f" Output {i}:") | |
| print(f" {item}") | |
| else: | |
| print(json.dumps(result, indent=2)) | |
| except Exception as e: | |
| print(f"β Method 1 fn_index=0 failed: {e}") | |
| # Try fn_index=1 | |
| try: | |
| print("π§ͺ Method 1b: Using handle_file() with fn_index=1...") | |
| audio_file = handle_file("https://raw.githubusercontent.com/jiten-kmar/python-projects/main/baby-crying-32232.mp3") | |
| result = client.predict( | |
| audio_file, | |
| fn_index=1 | |
| ) | |
| print("β Method 1 with fn_index=1 worked!") | |
| print("Result type:", type(result)) | |
| print("Result:") | |
| print(json.dumps(result, indent=2)) | |
| except Exception as e2: | |
| print(f"β Method 1 fn_index=1 also failed: {e2}") | |
| # Method 2: Try with API name | |
| try: | |
| print("\nπ§ͺ Method 2: Using api_name...") | |
| audio_file = handle_file("https://raw.githubusercontent.com/jiten-kmar/python-projects/main/baby-crying-32232.mp3") | |
| result = client.predict( | |
| audio_file, | |
| api_name="/web_interface_predict" | |
| ) | |
| print("β Method 2 worked!") | |
| print("Result type:", type(result)) | |
| print("Result:") | |
| if isinstance(result, (list, tuple)): | |
| for i, item in enumerate(result): | |
| print(f" Output {i}:") | |
| print(f" {item}") | |
| else: | |
| print(json.dumps(result, indent=2)) | |
| except Exception as e: | |
| print(f"β Method 2 failed: {e}") | |
| # Method 3: Download file locally first, then upload | |
| try: | |
| print("\nπ§ͺ Method 3: Download and use local file...") | |
| import requests | |
| import tempfile | |
| import os | |
| # Download the file | |
| response = requests.get("https://raw.githubusercontent.com/jiten-kmar/python-projects/main/baby-crying-32232.mp3") | |
| # Save to temporary file | |
| with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as tmp_file: | |
| tmp_file.write(response.content) | |
| local_file_path = tmp_file.name | |
| print(f"π Downloaded to: {local_file_path}") | |
| # Use local file with handle_file | |
| audio_file = handle_file(local_file_path) | |
| result = client.predict( | |
| audio_file, | |
| fn_index=0 | |
| ) | |
| print("β Method 3 worked!") | |
| print("Result type:", type(result)) | |
| print("Result:") | |
| if isinstance(result, (list, tuple)): | |
| for i, item in enumerate(result): | |
| print(f" Output {i}:") | |
| print(f" {item}") | |
| else: | |
| print(json.dumps(result, indent=2)) | |
| # Clean up | |
| os.unlink(local_file_path) | |
| except Exception as e: | |
| print(f"β Method 3 failed: {e}") | |
| except Exception as main_error: | |
| print(f"β Failed to connect to the Space: {main_error}") | |
| print("\n" + "="*60) | |
| print("π― KEY LEARNING:") | |
| print("Gradio expects FileData objects, not string URLs!") | |
| print("Use: handle_file(url_or_path) to create proper FileData") | |
| print("\nβ Working Python pattern:") | |
| print(""" | |
| from gradio_client import Client, handle_file | |
| client = Client("https://jitender1278-babycry.hf.space") | |
| audio_file = handle_file("your_audio_url_or_path") | |
| result = client.predict(audio_file, fn_index=WORKING_INDEX) | |
| print(result) | |
| """) |