File size: 4,541 Bytes
f6072cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Gradio API Client</title>
    <style>
        body { font-family: system-ui, sans-serif; line-height: 1.6; max-width: 800px; margin: 2rem auto; }
        .container { display: flex; flex-direction: column; gap: 1.5rem; border: 1px solid #ccc; padding: 1.5rem; border-radius: 8px; }
        .tab-content { display: none; }
        .tab-content.active { display: block; }
        .tabs button { padding: 10px; border: 1px solid #ccc; background: #f0f0f0; cursor: pointer; }
        .tabs button.active { background: #fff; border-bottom: 1px solid #fff; }
        h1, h3 { margin-top: 0; }
        input, button, textarea { font-size: 1rem; padding: 0.5rem; }
        pre { background: #f4f4f4; padding: 1rem; border-radius: 5px; white-space: pre-wrap; word-wrap: break-word; }
    </style>
</head>
<body>

    <h1>Gradio API Test Client</h1>
    <div class="container">
        <div>
            <label for="api-url-base"><b>Base API URL (Your Space's Direct URL):</b></label>
            <input type="text" id="api-url-base" placeholder="https://your-space-name.hf.space" style="width: 100%;">
        </div>
        
        <div class="tabs">
            <button class="tab-button active" onclick="showTab('mcp')">Test /run/mcp_decode (Base64)</button>
        </div>

        <div id="mcp" class="tab-content active">
            <h3>Test the MCP (Base64) Endpoint</h3>
            <p>This method is simple and robust for programmatic use.</p>
            <div>
                <label for="image-upload-mcp">Upload Image to convert to Base64:</label>
                <input type="file" id="image-upload-mcp" accept="image/png">
            </div>
            <textarea id="base64-output" rows="4" readonly placeholder="Base64 will appear here..."></textarea>
            <button id="submit-mcp">Send Base64 to /run/mcp_decode</button>
            <h3>MCP Result:</h3>
            <pre id="result-mcp">...</pre>
        </div>
    </div>

    <script>
        const apiUrlBaseInput = document.getElementById('api-url-base');
        const mcpUploadInput = document.getElementById('image-upload-mcp');
        const b64Output = document.getElementById('base64-output');
        const mcpSubmitBtn = document.getElementById('submit-mcp');
        const mcpResultPre = document.getElementById('result-mcp');

        // Convert file to Base64 for MCP endpoint
        mcpUploadInput.addEventListener('change', () => {
            const file = mcpUploadInput.files[0];
            if (!file) return;
            const reader = new FileReader();
            reader.onload = (e) => {
                // The result includes the data URL prefix, which we must remove.
                const b64 = e.target.result.split(',')[1];
                b64Output.value = b64;
            };
            reader.readAsDataURL(file);
        });

        // Handle MCP submission
        mcpSubmitBtn.addEventListener('click', async () => {
            const baseUrl = apiUrlBaseInput.value.trim();
            const b64String = b64Output.value.trim();

            if (!baseUrl || !b64String) {
                mcpResultPre.textContent = 'Error: Please provide both a base URL and an image.';
                return;
            }

            mcpResultPre.textContent = 'Sending request...';
            mcpSubmitBtn.disabled = true;

            const apiUrl = `${baseUrl}/run/mcp_decode`;
            
            // Gradio API expects a specific JSON structure: {"data": [inputs...]}
            const payload = {
                data: [ b64String ]
            };

            try {
                const response = await fetch(apiUrl, {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(payload)
                });

                const result = await response.json();

                if (result.error) {
                    throw new Error(result.error);
                }
                
                // The actual data is inside result.data[0]
                mcpResultPre.textContent = `βœ… Success!\n\n${JSON.stringify(result.data[0], null, 2)}`;
            } catch (error) {
                mcpResultPre.textContent = `❌ API Request Failed:\n\n${error.message}`;
            } finally {
                mcpSubmitBtn.disabled = false;
            }
        });
    </script>
</body>
</html>