File size: 4,529 Bytes
1b33af7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# 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)
""")