File size: 7,852 Bytes
d179c44
892aeb4
 
8b464c8
 
892aeb4
 
 
 
 
 
 
 
 
8b464c8
 
 
668a2f1
892aeb4
 
 
 
 
8b464c8
 
 
 
892aeb4
 
8b464c8
 
892aeb4
 
 
 
 
 
8b464c8
892aeb4
8b464c8
 
 
 
 
892aeb4
8b464c8
 
 
892aeb4
 
 
 
 
8b464c8
892aeb4
 
8b464c8
 
892aeb4
 
8b464c8
 
 
 
 
 
892aeb4
8b464c8
 
892aeb4
 
 
 
 
8b464c8
892aeb4
 
 
 
8b464c8
892aeb4
8b464c8
 
 
 
 
892aeb4
8b464c8
 
 
892aeb4
8b464c8
 
 
 
 
 
892aeb4
 
8b464c8
 
892aeb4
 
8b464c8
 
 
 
892aeb4
 
8b464c8
 
892aeb4
 
 
 
 
 
8b464c8
892aeb4
8b464c8
 
 
 
 
892aeb4
8b464c8
 
 
892aeb4
 
 
 
 
 
 
8b464c8
892aeb4
 
8b464c8
 
892aeb4
 
8b464c8
 
 
 
 
 
 
892aeb4
8b464c8
 
892aeb4
 
 
 
 
 
 
 
8b464c8
892aeb4
8b464c8
892aeb4
8b464c8
 
 
892aeb4
8b464c8
 
 
892aeb4
 
 
 
 
8b464c8
892aeb4
 
8b464c8
 
892aeb4
 
 
 
 
 
 
 
 
 
 
 
d179c44
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*document.addEventListener('DOMContentLoaded', function() {
    // Tab functionality
    const tabButtons = document.querySelectorAll('.tab-btn');
    const tabContents = document.querySelectorAll('.tab-content');
    
    tabButtons.forEach(button => {
        button.addEventListener('click', () => {
            // Remove active class from all buttons and contents
            tabButtons.forEach(btn => btn.classList.remove('active'));
            tabContents.forEach(content => content.classList.remove('active'));
            
            // Add active class to clicked button and corresponding content
            button.classList.add('active');
            const tabId = button.getAttribute('data-tab');
            document.getElementById(tabId).classList.add('active');
        });
    });

    // API base URL - replace with your Hugging Face Space URL
    const API_BASE_URL = "https://your-username-your-space-name.hf.space"; 
    // For local testing: "http://localhost:7860"

    // Summarize functionality
    document.getElementById('summarize-btn').addEventListener('click', async function() {
        const fileInput = document.getElementById('summarize-file');
        const resultArea = document.getElementById('summarize-result');
        
        if (!fileInput.files[0]) {
            resultArea.textContent = "Please select a file first";
            return;
        }

        resultArea.textContent = "Processing document...";

        const formData = new FormData();
        formData.append('file', fileInput.files[0]);

        try {
            const response = await fetch(`${API_BASE_URL}/ask`, {
                method: 'POST',
                body: formData
            });
            
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }
            
            const data = await response.json();
            resultArea.innerHTML = `
                <h3>Summary:</h3>
                <p>${data.answer}</p>
                <p class="confidence">Confidence: ${(data.confidence * 100).toFixed(1)}%</p>
            `;
        } catch (error) {
            resultArea.textContent = `Error: ${error.message}`;
            console.error('Summarization error:', error);
        }
    });

    // Question Answering functionality
    document.getElementById('ask-btn').addEventListener('click', async function() {
        const fileInput = document.getElementById('ask-file');
        const questionInput = document.getElementById('ask-question');
        const resultArea = document.getElementById('ask-result');
        
        if (!questionInput.value.trim()) {
            resultArea.textContent = "Please enter a question";
            return;
        }

        resultArea.textContent = "Processing your question...";

        const formData = new FormData();
        formData.append('question', questionInput.value);
        
        if (fileInput.files[0]) {
            formData.append('file', fileInput.files[0]);
        }

        try {
            const response = await fetch(`${API_BASE_URL}/ask`, {
                method: 'POST',
                body: formData
            });
            
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }
            
            const data = await response.json();
            resultArea.innerHTML = `
                <h3>Answer:</h3>
                <p>${data.answer}</p>
                <p class="confidence">Confidence: ${(data.confidence * 100).toFixed(1)}%</p>
                ${data.context_used ? `<details><summary>Context used</summary><p>${data.context_used}</p></details>` : ''}
            `;
        } catch (error) {
            resultArea.textContent = `Error: ${error.message}`;
            console.error('Question answering error:', error);
        }
    });

    // Image Captioning functionality
    document.getElementById('caption-btn').addEventListener('click', async function() {
        const fileInput = document.getElementById('caption-file');
        const resultArea = document.getElementById('caption-result');
        
        if (!fileInput.files[0]) {
            resultArea.textContent = "Please select an image first";
            return;
        }

        resultArea.textContent = "Generating caption...";

        const formData = new FormData();
        formData.append('file', fileInput.files[0]);

        try {
            const response = await fetch(`${API_BASE_URL}/api/caption`, {
                method: 'POST',
                body: formData
            });
            
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }
            
            const data = await response.json();
            resultArea.innerHTML = `
                <h3>Caption:</h3>
                <p>${data.caption}</p>
                <div class="image-preview">
                    <img src="${URL.createObjectURL(fileInput.files[0])}" alt="Uploaded image">
                </div>
            `;
        } catch (error) {
            resultArea.textContent = `Error: ${error.message}`;
            console.error('Captioning error:', error);
        }
    });

    // Translation functionality
    document.getElementById('translate-btn').addEventListener('click', async function() {
        const textInput = document.getElementById('translate-text');
        const sourceLang = document.getElementById('source-lang').value;
        const targetLang = document.getElementById('target-lang').value;
        const resultArea = document.getElementById('translate-result');
        
        if (!textInput.value.trim()) {
            resultArea.textContent = "Please enter text to translate";
            return;
        }

        resultArea.textContent = "Translating...";

        const formData = new FormData();
        formData.append('text', textInput.value);
        formData.append('target_lang', targetLang);
        formData.append('src_lang', sourceLang);

        try {
            const response = await fetch(`${API_BASE_URL}/translate`, {
                method: 'POST',
                body: formData
            });
            
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }
            
            const data = await response.json();
            resultArea.innerHTML = `
                <h3>Translation:</h3>
                <p>${data.translated_text}</p>
                <p class="language-info">Translated from ${document.getElementById('source-lang').options[document.getElementById('source-lang').selectedIndex].text} to ${document.getElementById('target-lang').options[document.getElementById('target-lang').selectedIndex].text}</p>
            `;
        } catch (error) {
            resultArea.textContent = `Error: ${error.message}`;
            console.error('Translation error:', error);
        }
    });

    // File input styling
    document.querySelectorAll('input[type="file"]').forEach(input => {
        input.addEventListener('change', function() {
            const label = this.nextElementSibling;
            if (this.files[0]) {
                label.textContent = this.files[0].name;
            } else {
                label.textContent = label.getAttribute('data-default') || 'Choose a file';
            }
        });
    });
});*/

// Function to show the selected section and hide the others
function showSection(sectionId) {
    const sections = document.querySelectorAll('.tool-section');
    sections.forEach(section => {
      section.style.display = 'none';
    });
  
    const activeSection = document.getElementById(sectionId);
    if (activeSection) {
      activeSection.style.display = 'block';
    }
  }