File size: 3,781 Bytes
eda093f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html>
<head>
    <title>FastAPI Search Client</title>
    <style>

        body { font-family: Arial, sans-serif; max-width: 800px; margin: 20px auto; }

        .input-group { margin-bottom: 15px; }

        textarea { width: 300px; padding: 8px; resize: vertical; }

        #results { margin-top: 20px; }

        .result { margin-bottom: 15px; padding: 10px; border: 1px solid #ddd; }

    </style>
</head>
<body>
    <h2>FastAPI Search Client</h2>
    
    <div class="input-group">
        <label>API Key:</label><br>
        <textarea id="apiKey" placeholder="Enter your API_SECRET_KEY"></textarea>
    </div>
    
    <div class="input-group">
        <input type="text" id="query" placeholder="Enter search query and press Enter..." 

               style="width: 300px; padding: 8px;">
    </div>
    
    <div id="results"></div>

    <script>

        const API_URL = 'https://huggingface.co/spaces/Almaatla/search'; // Replace with your FastAPI endpoint



        document.getElementById('query').addEventListener('keypress', async (e) => {

            if (e.key === 'Enter') {

                const apiKey = document.getElementById('apiKey').value.trim();

                const query = e.target.value.trim();

                

                if (!apiKey || !query) {

                    alert('Please enter both API key and search query');

                    return;

                }



                try {

                    const response = await fetch(API_URL, {

                        method: 'POST',

                        headers: {

                            'Content-Type': 'application/json',

                            'X-API-Key': apiKey

                        },

                        body: JSON.stringify({

                            query: query,

                            engine: 'google',

                            num_results: 10

                        })

                    });



                    if (!response.ok) {

                        throw new Error(`HTTP error! status: ${response.status}`);

                    }



                    const data = await response.json();

                    displayResults(data);

                } catch (error) {

                    console.error('Error:', error);

                    document.getElementById('results').innerHTML = `

                        <div class="error">

                            Error: ${error.message}

                        </div>

                    `;

                }

            }

        });



        function displayResults(data) {

            const results = document.getElementById('results');

            let html = '';



            if (data.organic_results?.length > 0) {

                html += '<h3>Search Results:</h3>';

                data.organic_results.forEach(result => {

                    html += `

                        <div class="result">

                            <a href="${result.link}" target="_blank">${result.title}</a>

                            <p>${result.snippet}</p>

                            <small>${result.displayed_link}</small>

                        </div>

                    `;

                });

            }



            if (data.related_searches?.length > 0) {

                html += '<h3>Related Searches:</h3>';

                data.related_searches.forEach(search => {

                    html += `

                        <div class="result">

                            ${search.query}

                        </div>

                    `;

                });

            }



            results.innerHTML = html || '<p>No results found</p>';

        }

    </script>
</body>
</html>