babycry / curl-python-file.py
Jitender1278's picture
Create curl-python-file.py
1b33af7 verified
# 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)
""")