File size: 9,392 Bytes
d4e9189
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DeepSeek AI Server</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://js.puter.com/v2/"></script>
    <style>
        body {
            font-family: 'Inter', sans-serif;
            background-color: #f3f4f6;
        }
        #output {
            white-space: pre-wrap;
            word-wrap: break-word;
        }
        #apiResponse {
            font-family: monospace;
            white-space: pre;
            overflow-x: auto;
        }
    </style>
</head>
<body class="min-h-screen flex flex-col items-center justify-center p-4">
    <div class="w-full max-w-4xl bg-white shadow-lg rounded-lg p-6">
        <h1 class="text-2xl font-bold text-gray-800 mb-4 text-center">DeepSeek AI Server</h1>
        
        <!-- Web Interface Tab -->
        <div class="mb-6" id="webInterface">
            <div class="mb-4">
                <input 
                    type="text" 
                    id="query" 
                    placeholder="Enter your query (e.g., Explain dark matter)" 
                    class="w-full p-3 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
                >
                <button 
                    id="submit" 
                    class="mt-2 w-full bg-blue-600 text-white p-3 rounded-lg hover:bg-blue-700 transition"
                >
                    Get AI Response
                </button>
            </div>
            <div id="output" class="bg-gray-100 p-4 rounded-lg text-gray-800 min-h-32"></div>
        </div>
        
        <!-- API Tester Tab -->
        <div class="mb-6 hidden" id="apiTester">
            <div class="mb-4">
                <h2 class="text-lg font-semibold mb-2">Test API Endpoint</h2>
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">API Request</label>
                        <textarea 
                            id="apiRequest" 
                            class="w-full p-3 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 h-32"
                            placeholder='{"query": "Your question here", "model": "gpt-4o"}'
                        ></textarea>
                        <button 
                            id="apiSubmit" 
                            class="mt-2 w-full bg-green-600 text-white p-3 rounded-lg hover:bg-green-700 transition"
                        >
                            Send API Request
                        </button>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">API Response</label>
                        <div id="apiResponse" class="w-full p-3 border rounded-lg bg-gray-100 h-32 overflow-auto"></div>
                    </div>
                </div>
            </div>
            
            <div class="bg-gray-100 p-4 rounded-lg">
                <h2 class="text-lg font-semibold mb-2">API Documentation</h2>
                <div class="space-y-3">
                    <div>
                        <h3 class="font-medium">Endpoint</h3>
                        <code class="bg-gray-200 p-1 rounded text-sm">POST /api/ask</code>
                    </div>
                    <div>
                        <h3 class="font-medium">Request Body</h3>
                        <pre class="bg-gray-200 p-2 rounded text-sm overflow-x-auto">
{
  "query": "string",  // Required
  "model": "string"   // Optional (default: "gpt-4o")
}</pre>
                    </div>
                    <div>
                        <h3 class="font-medium">Example Usage</h3>
                        <pre class="bg-gray-200 p-2 rounded text-sm overflow-x-auto">
fetch('/api/ask', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    query: "Explain quantum computing",
    model: "gpt-4o"
  })
})
.then(response => response.text())
.then(data => console.log(data));</pre>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Tab Navigation -->
        <div class="flex border-b">
            <button 
                class="px-4 py-2 font-medium text-blue-600 border-b-2 border-blue-600"
                onclick="showTab('webInterface')"
            >
                Web Interface
            </button>
            <button 
                class="px-4 py-2 font-medium text-gray-500 hover:text-gray-700"
                onclick="showTab('apiTester')"
            >
                API Tester
            </button>
        </div>
    </div>

    <script>
        // Tab navigation
        function showTab(tabId) {
            document.getElementById('webInterface').classList.add('hidden');
            document.getElementById('apiTester').classList.add('hidden');
            document.getElementById(tabId).classList.remove('hidden');
            
            // Update active tab styling
            const tabs = document.querySelectorAll('.flex.border-b button');
            tabs.forEach(tab => {
                tab.classList.remove('text-blue-600', 'border-b-2', 'border-blue-600');
                tab.classList.add('text-gray-500', 'hover:text-gray-700');
            });
            
            event.currentTarget.classList.add('text-blue-600', 'border-b-2', 'border-blue-600');
            event.currentTarget.classList.remove('text-gray-500', 'hover:text-gray-700');
        }

        // Web Interface Functionality
        async function streamResponse(query) {
            const outputDiv = document.getElementById('output');
            outputDiv.innerHTML = ''; // Clear previous output
            const submitButton = document.getElementById('submit');
            submitButton.disabled = true; // Disable button during processing

            try {
                outputDiv.innerHTML += '<h2 class="text-lg font-semibold text-blue-600">AI Response:</h2>';
                
                // Using Puter SDK directly (client-side)
                const chatResponse = await puter.ai.chat(
                    query, 
                    {
                        model: 'gpt-4o',
                        stream: true
                    }
                );
                
                for await (const part of chatResponse) {
                    if (part?.text) {
                        outputDiv.innerHTML += part.text;
                        outputDiv.scrollTop = outputDiv.scrollHeight; // Auto-scroll to bottom
                    }
                }

            } catch (error) {
                outputDiv.innerHTML += `<p class="text-red-600">Error: ${error.message}</p>`;
            } finally {
                submitButton.disabled = false; // Re-enable button
            }
        }

        // API Tester Functionality
        async function testApi() {
            const apiRequest = document.getElementById('apiRequest').value.trim();
            const apiResponseDiv = document.getElementById('apiResponse');
            const apiSubmitButton = document.getElementById('apiSubmit');
            
            apiResponseDiv.textContent = '';
            apiSubmitButton.disabled = true;
            
            try {
                if (!apiRequest) {
                    throw new Error('Please enter a valid JSON request');
                }
                
                const requestObj = JSON.parse(apiRequest);
                if (!requestObj.query) {
                    throw new Error('Request must include a "query" field');
                }
                
                // Using Puter SDK directly (client-side)
                const response = await puter.ai.chat(
                    requestObj.query, 
                    {
                        model: requestObj.model || 'gpt-4o',
                        stream: false
                    }
                );
                
                apiResponseDiv.textContent = JSON.stringify(response, null, 2);
            } catch (error) {
                apiResponseDiv.textContent = `Error: ${error.message}`;
            } finally {
                apiSubmitButton.disabled = false;
            }
        }

        // Event listeners
        document.getElementById('submit').addEventListener('click', () => {
            const query = document.getElementById('query').value.trim();
            if (query) {
                streamResponse(query);
            } else {
                document.getElementById('output').innerHTML = '<p class="text-red-600">Please enter a query.</p>';
            }
        });

        document.getElementById('query').addEventListener('keypress', (e) => {
            if (e.key === 'Enter') {
                document.getElementById('submit').click();
            }
        });

        document.getElementById('apiSubmit').addEventListener('click', testApi);

        document.getElementById('apiRequest').addEventListener('keypress', (e) => {
            if (e.key === 'Enter' && e.ctrlKey) {
                testApi();
            }
        });

        // Initialize with web interface shown
        showTab('webInterface');
    </script>
</body>
</html>