File size: 3,573 Bytes
69bb670
b5aa17b
69bb670
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d4d60e
 
 
 
69bb670
 
 
 
 
 
 
 
 
 
 
0d4d60e
69bb670
28048a3
 
 
69bb670
28048a3
 
 
 
69bb670
0d4d60e
 
 
 
69bb670
 
 
28048a3
 
69bb670
 
 
0d4d60e
 
 
 
28048a3
 
 
 
 
0d4d60e
 
69bb670
0d4d60e
 
28048a3
69bb670
 
 
 
 
0d4d60e
 
69bb670
 
 
 
 
 
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
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>OpenBuddy Text Generation</title>
    <link rel="stylesheet" href="style.css" />
</head>
<body>
    <div class="container">
        <h1>OpenBuddy Text Generation</h1>
        <form id="generateForm">
            <div class="form-group">
                <label for="promptInput">Enter your prompt:</label>
                <textarea id="promptInput" rows="5" cols="50" required></textarea>
            </div>
            <button type="submit">Generate Text</button>
        </form>
        <div id="resultContainer" style="display: none;">
            <h2>Generated Text:</h2>
            <p id="generatedText"></p>
        </div>
        <div id="debugContainer">
            <h2>Debug Information:</h2>
            <pre id="debugOutput"></pre>
        </div>
    </div>

    <script>
        document.getElementById('generateForm').addEventListener('submit', function(e) {
            e.preventDefault();
            const promptInput = document.getElementById('promptInput');
            const prompt = promptInput.value.trim();
            if (prompt !== '') {
                generateText(prompt);
            }
        });

        function generateText(prompt) {
            const apiUrl = 'https://api-inference.huggingface.co/models/OpenBuddy/openbuddy-llama3-8b-v21.1-8k';
            const apiKey = '{{API_KEY}}'; // La chiave API verrà sostituita dalla variabile d'ambiente

            const requestData = {
                inputs: prompt,
                parameters: {
                    max_new_tokens: 100
                }
            };

            // Display request data in the debug output
            document.getElementById('debugOutput').textContent = 'Request Data:\n' + JSON.stringify(requestData, null, 2);

            fetch(apiUrl, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': 'Bearer ' + apiKey
                },
                body: JSON.stringify(requestData)
            })
            .then(response => {
                // Display response status and headers in the debug output
                document.getElementById('debugOutput').textContent += '\n\nResponse Status: ' + response.status;
                document.getElementById('debugOutput').textContent += '\nResponse Headers:\n' + JSON.stringify(Object.fromEntries(response.headers), null, 2);
                
                if (!response.ok) {
                    throw new Error('Request failed with status: ' + response.status);
                }
                
                return response.json();
            })
            .then(data => {
                // Display response data in the debug output
                document.getElementById('debugOutput').textContent += '\n\nResponse Data:\n' + JSON.stringify(data, null, 2);
                const generatedText = data[0].generated_text;
                document.getElementById('generatedText').textContent = generatedText;
                document.getElementById('resultContainer').style.display = 'block';
            })
            .catch(error => {
                console.error('Error:', error);
                // Display error in the debug output
                document.getElementById('debugOutput').textContent += '\n\nError:\n' + error.message;
                alert('An error occurred while generating text.');
            });
        }
    </script>
</body>
</html>