Spaces:
Running
Running
Create curl-python-file.py
Browse files- curl-python-file.py +134 -0
curl-python-file.py
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Working Python code with correct FileData format for Gradio
|
| 2 |
+
|
| 3 |
+
from gradio_client import Client, handle_file
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
try:
|
| 7 |
+
print("π Connecting to Baby Cry Classifier...")
|
| 8 |
+
client = Client("https://jitender1278-babycry.hf.space")
|
| 9 |
+
|
| 10 |
+
print("π₯ Testing with correct FileData format...")
|
| 11 |
+
|
| 12 |
+
# Method 1: Using handle_file() with URL
|
| 13 |
+
try:
|
| 14 |
+
print("π§ͺ Method 1: Using handle_file() with URL...")
|
| 15 |
+
|
| 16 |
+
# handle_file() creates proper FileData object
|
| 17 |
+
audio_file = handle_file("https://raw.githubusercontent.com/jiten-kmar/python-projects/main/baby-crying-32232.mp3")
|
| 18 |
+
|
| 19 |
+
result = client.predict(
|
| 20 |
+
audio_file,
|
| 21 |
+
fn_index=0 # Try the web interface first
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
print("β
Method 1 with fn_index=0 worked!")
|
| 25 |
+
print("Result type:", type(result))
|
| 26 |
+
print("Result:")
|
| 27 |
+
if isinstance(result, (list, tuple)):
|
| 28 |
+
for i, item in enumerate(result):
|
| 29 |
+
print(f" Output {i}:")
|
| 30 |
+
print(f" {item}")
|
| 31 |
+
else:
|
| 32 |
+
print(json.dumps(result, indent=2))
|
| 33 |
+
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"β Method 1 fn_index=0 failed: {e}")
|
| 36 |
+
|
| 37 |
+
# Try fn_index=1
|
| 38 |
+
try:
|
| 39 |
+
print("π§ͺ Method 1b: Using handle_file() with fn_index=1...")
|
| 40 |
+
audio_file = handle_file("https://raw.githubusercontent.com/jiten-kmar/python-projects/main/baby-crying-32232.mp3")
|
| 41 |
+
|
| 42 |
+
result = client.predict(
|
| 43 |
+
audio_file,
|
| 44 |
+
fn_index=1
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
print("β
Method 1 with fn_index=1 worked!")
|
| 48 |
+
print("Result type:", type(result))
|
| 49 |
+
print("Result:")
|
| 50 |
+
print(json.dumps(result, indent=2))
|
| 51 |
+
|
| 52 |
+
except Exception as e2:
|
| 53 |
+
print(f"β Method 1 fn_index=1 also failed: {e2}")
|
| 54 |
+
|
| 55 |
+
# Method 2: Try with API name
|
| 56 |
+
try:
|
| 57 |
+
print("\nπ§ͺ Method 2: Using api_name...")
|
| 58 |
+
audio_file = handle_file("https://raw.githubusercontent.com/jiten-kmar/python-projects/main/baby-crying-32232.mp3")
|
| 59 |
+
|
| 60 |
+
result = client.predict(
|
| 61 |
+
audio_file,
|
| 62 |
+
api_name="/web_interface_predict"
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
print("β
Method 2 worked!")
|
| 66 |
+
print("Result type:", type(result))
|
| 67 |
+
print("Result:")
|
| 68 |
+
if isinstance(result, (list, tuple)):
|
| 69 |
+
for i, item in enumerate(result):
|
| 70 |
+
print(f" Output {i}:")
|
| 71 |
+
print(f" {item}")
|
| 72 |
+
else:
|
| 73 |
+
print(json.dumps(result, indent=2))
|
| 74 |
+
|
| 75 |
+
except Exception as e:
|
| 76 |
+
print(f"β Method 2 failed: {e}")
|
| 77 |
+
|
| 78 |
+
# Method 3: Download file locally first, then upload
|
| 79 |
+
try:
|
| 80 |
+
print("\nπ§ͺ Method 3: Download and use local file...")
|
| 81 |
+
import requests
|
| 82 |
+
import tempfile
|
| 83 |
+
import os
|
| 84 |
+
|
| 85 |
+
# Download the file
|
| 86 |
+
response = requests.get("https://raw.githubusercontent.com/jiten-kmar/python-projects/main/baby-crying-32232.mp3")
|
| 87 |
+
|
| 88 |
+
# Save to temporary file
|
| 89 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as tmp_file:
|
| 90 |
+
tmp_file.write(response.content)
|
| 91 |
+
local_file_path = tmp_file.name
|
| 92 |
+
|
| 93 |
+
print(f"π Downloaded to: {local_file_path}")
|
| 94 |
+
|
| 95 |
+
# Use local file with handle_file
|
| 96 |
+
audio_file = handle_file(local_file_path)
|
| 97 |
+
|
| 98 |
+
result = client.predict(
|
| 99 |
+
audio_file,
|
| 100 |
+
fn_index=0
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
print("β
Method 3 worked!")
|
| 104 |
+
print("Result type:", type(result))
|
| 105 |
+
print("Result:")
|
| 106 |
+
if isinstance(result, (list, tuple)):
|
| 107 |
+
for i, item in enumerate(result):
|
| 108 |
+
print(f" Output {i}:")
|
| 109 |
+
print(f" {item}")
|
| 110 |
+
else:
|
| 111 |
+
print(json.dumps(result, indent=2))
|
| 112 |
+
|
| 113 |
+
# Clean up
|
| 114 |
+
os.unlink(local_file_path)
|
| 115 |
+
|
| 116 |
+
except Exception as e:
|
| 117 |
+
print(f"β Method 3 failed: {e}")
|
| 118 |
+
|
| 119 |
+
except Exception as main_error:
|
| 120 |
+
print(f"β Failed to connect to the Space: {main_error}")
|
| 121 |
+
|
| 122 |
+
print("\n" + "="*60)
|
| 123 |
+
print("π― KEY LEARNING:")
|
| 124 |
+
print("Gradio expects FileData objects, not string URLs!")
|
| 125 |
+
print("Use: handle_file(url_or_path) to create proper FileData")
|
| 126 |
+
print("\nβ
Working Python pattern:")
|
| 127 |
+
print("""
|
| 128 |
+
from gradio_client import Client, handle_file
|
| 129 |
+
|
| 130 |
+
client = Client("https://jitender1278-babycry.hf.space")
|
| 131 |
+
audio_file = handle_file("your_audio_url_or_path")
|
| 132 |
+
result = client.predict(audio_file, fn_index=WORKING_INDEX)
|
| 133 |
+
print(result)
|
| 134 |
+
""")
|