pvanand commited on
Commit
9e02811
·
verified ·
1 Parent(s): 1d91463

Update static/js/apps-hub/research-pro.js

Browse files
Files changed (1) hide show
  1. static/js/apps-hub/research-pro.js +281 -281
static/js/apps-hub/research-pro.js CHANGED
@@ -1,282 +1,282 @@
1
- async function generateReport() {
2
- const description = document.getElementById('description').value || document.getElementById('description-textarea').value;
3
- document.getElementById('initial-view').style.display = 'none';
4
- document.getElementById('report-view').style.display = 'block';
5
- document.getElementById('description-textarea').value = description;
6
- const reportContainer = document.getElementById('report-container');
7
- const sourcesContainer = document.getElementById('sources-container');
8
- const downloadBtn = document.getElementById('downloadBtn');
9
- reportContainer.innerHTML = 'Generating report...';
10
- sourcesContainer.innerHTML = '';
11
- downloadBtn.style.display = 'none';
12
-
13
- try {
14
- const response = await fetch('https://pvanand-search-generate-prod.hf.space/generate_report', {
15
- method: 'POST',
16
- headers: {
17
- 'Content-Type': 'application/json',
18
- 'Accept': 'text/plain'
19
- },
20
- body: JSON.stringify({
21
- description: description,
22
- user_id: "",
23
- user_name: "multi-agent-research",
24
- internet: true,
25
- output_format: "report_table",
26
- data_format: "Structured data",
27
- generate_charts: true,
28
- output_as_md: true
29
- })
30
- });
31
-
32
- if (!response.ok) {
33
- throw new Error(`HTTP error! status: ${response.status}`);
34
- }
35
-
36
- const reader = response.body.getReader();
37
- const decoder = new TextDecoder();
38
- let markdown = '';
39
- let metadata = '';
40
- let isReadingMetadata = false;
41
-
42
- while (true) {
43
- const { value, done } = await reader.read();
44
- if (done) break;
45
-
46
- const chunk = decoder.decode(value, { stream: true });
47
-
48
- if (chunk.includes('<report-metadata>')) {
49
- isReadingMetadata = true;
50
- metadata = '';
51
- }
52
-
53
- if (isReadingMetadata) {
54
- metadata += chunk;
55
- if (chunk.includes('</report-metadata>')) {
56
- isReadingMetadata = false;
57
- processMetadata(metadata);
58
- }
59
- } else {
60
- markdown += chunk;
61
- renderMarkdown(markdown);
62
- }
63
- }
64
-
65
- // Show the download button after the report is generated
66
- downloadBtn.style.display = 'inline-block';
67
- } catch (error) {
68
- reportContainer.innerHTML = `Error generating report: ${error.message}`;
69
- }
70
- }
71
-
72
- function renderMarkdown(markdown) {
73
- const reportContainer = document.getElementById('report-container');
74
- const previousHeight = reportContainer.scrollHeight;
75
- const reportContent = markdown.match(/<report>([\s\S]*)<\/report>/);
76
-
77
- if (reportContent) {
78
- reportContainer.innerHTML = marked.parse(reportContent[1]);
79
- } else {
80
- reportContainer.innerHTML = marked.parse(markdown);
81
- }
82
-
83
- const scripts = reportContainer.getElementsByTagName('script');
84
- Array.from(scripts).forEach(script => {
85
- const newScript = document.createElement('script');
86
- newScript.textContent = script.textContent;
87
- script.parentNode.replaceChild(newScript, script);
88
- });
89
-
90
- // Make Plotly charts responsive
91
- const plots = reportContainer.querySelectorAll('.js-plotly-plot');
92
- plots.forEach(plot => {
93
- Plotly.Plots.resize(plot);
94
- });
95
-
96
- // Scroll to new content
97
- scrollToNewContent(previousHeight);
98
- }
99
-
100
- function processMetadata(metadata) {
101
- const sourcesContainer = document.getElementById('sources-container');
102
- const metadataMatch = metadata.match(/all-text-with-urls: (.+)/);
103
-
104
- if (metadataMatch) {
105
- const metadataObj = JSON.parse(metadataMatch[1]);
106
-
107
- sourcesContainer.innerHTML = '<h2>Sources</h2>';
108
- metadataObj.forEach(([content, url]) => {
109
- if (content.trim() !== "") {
110
- const sourceItem = document.createElement('div');
111
- sourceItem.className = 'source-item';
112
- const snippet = content.length > 400 ? content.substring(0, 400) + '...' : content;
113
- sourceItem.innerHTML = `
114
- <a href="${url}" target="_blank" class="source-url">${url}</a>
115
- <div class="source-content">
116
- <div class="source-snippet">${marked.parse(snippet)}</div>
117
- <div class="source-full">${marked.parse(content)}</div>
118
- <div class="expand-indicator"></div>
119
- </div>
120
- `;
121
- sourcesContainer.appendChild(sourceItem);
122
-
123
- const sourceUrl = sourceItem.querySelector('.source-url');
124
- const sourceContent = sourceItem.querySelector('.source-content');
125
- const snippetDiv = sourceItem.querySelector('.source-snippet');
126
- const fullDiv = sourceItem.querySelector('.source-full');
127
-
128
- sourceContent.addEventListener('click', function(e) {
129
- if (!sourceItem.classList.contains('expanded')) {
130
- sourceItem.classList.add('expanded');
131
- snippetDiv.style.display = 'none';
132
- fullDiv.style.display = 'block';
133
- } else if (e.clientY > sourceContent.getBoundingClientRect().bottom - 30) {
134
- sourceItem.classList.remove('expanded');
135
- snippetDiv.style.display = 'block';
136
- fullDiv.style.display = 'none';
137
- }
138
- });
139
-
140
- sourceUrl.addEventListener('click', function(e) {
141
- e.stopPropagation();
142
- });
143
- }
144
- });
145
- } else {
146
- sourcesContainer.innerHTML = '<h2>Sources</h2><p>No source information available.</p>';
147
- }
148
- }
149
-
150
- function scrollToNewContent(previousHeight) {
151
- const reportContainer = document.getElementById('report-container');
152
- const newHeight = reportContainer.scrollHeight;
153
- const scrollDifference = newHeight - previousHeight;
154
-
155
- if (scrollDifference > 0) {
156
- const viewportHeight = window.innerHeight;
157
- const currentScrollPosition = window.pageYOffset;
158
- const contentBottom = reportContainer.offsetTop + newHeight;
159
-
160
- // Start scrolling when new content is within 200px of viewport bottom
161
- if (contentBottom > currentScrollPosition + viewportHeight - 200) {
162
- window.scrollBy({
163
- top: scrollDifference,
164
- behavior: 'smooth'
165
- });
166
- }
167
- }
168
- }
169
-
170
- function scrollToTop() {
171
- window.scrollTo({
172
- top: 0,
173
- behavior: 'smooth'
174
- });
175
- }
176
-
177
- async function downloadHTML() {
178
- try {
179
- // Fetch the CSS
180
- const response = await fetch('styles.css');
181
- let css = await response.text();
182
-
183
- // Add styles for expanded sources and body max-width
184
- css += `
185
- body.report-body {
186
- max-width: 804px;
187
- margin: 0 auto;
188
- }
189
- .source-item {
190
- margin-bottom: 20px;
191
- }
192
- .source-content {
193
- margin-top: 10px;
194
- }
195
- .source-snippet, .expand-indicator {
196
- display: none;
197
- }
198
- .source-full {
199
- display: block;
200
- }
201
- `;
202
-
203
- // Clone the document
204
- const htmlContent = document.implementation.createHTMLDocument('Report');
205
-
206
- // Set up the basic structure
207
- htmlContent.documentElement.lang = 'en';
208
- const head = htmlContent.head;
209
- const body = htmlContent.body;
210
- body.className = 'report-body';
211
-
212
- // Add meta tags
213
- const meta = htmlContent.createElement('meta');
214
- meta.charset = 'UTF-8';
215
- head.appendChild(meta);
216
-
217
- const viewport = htmlContent.createElement('meta');
218
- viewport.name = 'viewport';
219
- viewport.content = 'width=device-width, initial-scale=1.0';
220
- head.appendChild(viewport);
221
-
222
- // Add title
223
- const title = htmlContent.createElement('title');
224
- title.textContent = 'Generated Report';
225
- head.appendChild(title);
226
-
227
- // Add style
228
- const style = htmlContent.createElement('style');
229
- style.textContent = css;
230
- head.appendChild(style);
231
-
232
- // Add necessary scripts
233
- const markedScript = htmlContent.createElement('script');
234
- markedScript.src = 'https://cdn.jsdelivr.net/npm/marked/marked.min.js';
235
- head.appendChild(markedScript);
236
-
237
- const plotlyScript = htmlContent.createElement('script');
238
- plotlyScript.src = 'https://cdn.plot.ly/plotly-latest.min.js';
239
- head.appendChild(plotlyScript);
240
-
241
- // Copy the report content
242
- const reportContainer = document.getElementById('report-container');
243
- body.innerHTML = reportContainer.innerHTML;
244
-
245
- // Copy and modify the sources content
246
- const sourcesContainer = document.getElementById('sources-container');
247
- const sourcesDiv = htmlContent.createElement('div');
248
- sourcesDiv.innerHTML = sourcesContainer.innerHTML;
249
-
250
- // Expand all sources
251
- const sourceItems = sourcesDiv.querySelectorAll('.source-item');
252
- sourceItems.forEach(item => {
253
- item.classList.add('expanded');
254
- const snippetDiv = item.querySelector('.source-snippet');
255
- const fullDiv = item.querySelector('.source-full');
256
- if (snippetDiv) snippetDiv.style.display = 'none';
257
- if (fullDiv) fullDiv.style.display = 'block';
258
- });
259
-
260
- body.appendChild(sourcesDiv);
261
-
262
- // Create blob and download
263
- const blob = new Blob([htmlContent.documentElement.outerHTML], { type: 'text/html' });
264
- const url = URL.createObjectURL(blob);
265
- const a = document.createElement('a');
266
- a.href = url;
267
- a.download = 'report.html';
268
- document.body.appendChild(a);
269
- a.click();
270
- document.body.removeChild(a);
271
- URL.revokeObjectURL(url);
272
- } catch (error) {
273
- console.error('Error downloading HTML:', error);
274
- }
275
- }
276
- // Make Plotly charts responsive on window resize
277
- window.addEventListener('resize', function() {
278
- const plots = document.querySelectorAll('.js-plotly-plot');
279
- plots.forEach(plot => {
280
- Plotly.Plots.resize(plot);
281
- });
282
  });
 
1
+ async function generateReport() {
2
+ const description = document.getElementById('description').value || document.getElementById('description-textarea').value;
3
+ document.getElementById('initial-view').style.display = 'none';
4
+ document.getElementById('report-view').style.display = 'block';
5
+ document.getElementById('description-textarea').value = description;
6
+ const reportContainer = document.getElementById('report-container');
7
+ const sourcesContainer = document.getElementById('sources-container');
8
+ const downloadBtn = document.getElementById('downloadBtn');
9
+ reportContainer.innerHTML = 'Generating report...';
10
+ sourcesContainer.innerHTML = '';
11
+ downloadBtn.style.display = 'none';
12
+
13
+ try {
14
+ const response = await fetch('https://iresearcher-api.elevatics.cloud/generate_report', {
15
+ method: 'POST',
16
+ headers: {
17
+ 'Content-Type': 'application/json',
18
+ 'Accept': 'text/plain'
19
+ },
20
+ body: JSON.stringify({
21
+ description: description,
22
+ user_id: "",
23
+ user_name: "multi-agent-research",
24
+ internet: true,
25
+ output_format: "report_table",
26
+ data_format: "Structured data",
27
+ generate_charts: true,
28
+ output_as_md: true
29
+ })
30
+ });
31
+
32
+ if (!response.ok) {
33
+ throw new Error(`HTTP error! status: ${response.status}`);
34
+ }
35
+
36
+ const reader = response.body.getReader();
37
+ const decoder = new TextDecoder();
38
+ let markdown = '';
39
+ let metadata = '';
40
+ let isReadingMetadata = false;
41
+
42
+ while (true) {
43
+ const { value, done } = await reader.read();
44
+ if (done) break;
45
+
46
+ const chunk = decoder.decode(value, { stream: true });
47
+
48
+ if (chunk.includes('<report-metadata>')) {
49
+ isReadingMetadata = true;
50
+ metadata = '';
51
+ }
52
+
53
+ if (isReadingMetadata) {
54
+ metadata += chunk;
55
+ if (chunk.includes('</report-metadata>')) {
56
+ isReadingMetadata = false;
57
+ processMetadata(metadata);
58
+ }
59
+ } else {
60
+ markdown += chunk;
61
+ renderMarkdown(markdown);
62
+ }
63
+ }
64
+
65
+ // Show the download button after the report is generated
66
+ downloadBtn.style.display = 'inline-block';
67
+ } catch (error) {
68
+ reportContainer.innerHTML = `Error generating report: ${error.message}`;
69
+ }
70
+ }
71
+
72
+ function renderMarkdown(markdown) {
73
+ const reportContainer = document.getElementById('report-container');
74
+ const previousHeight = reportContainer.scrollHeight;
75
+ const reportContent = markdown.match(/<report>([\s\S]*)<\/report>/);
76
+
77
+ if (reportContent) {
78
+ reportContainer.innerHTML = marked.parse(reportContent[1]);
79
+ } else {
80
+ reportContainer.innerHTML = marked.parse(markdown);
81
+ }
82
+
83
+ const scripts = reportContainer.getElementsByTagName('script');
84
+ Array.from(scripts).forEach(script => {
85
+ const newScript = document.createElement('script');
86
+ newScript.textContent = script.textContent;
87
+ script.parentNode.replaceChild(newScript, script);
88
+ });
89
+
90
+ // Make Plotly charts responsive
91
+ const plots = reportContainer.querySelectorAll('.js-plotly-plot');
92
+ plots.forEach(plot => {
93
+ Plotly.Plots.resize(plot);
94
+ });
95
+
96
+ // Scroll to new content
97
+ scrollToNewContent(previousHeight);
98
+ }
99
+
100
+ function processMetadata(metadata) {
101
+ const sourcesContainer = document.getElementById('sources-container');
102
+ const metadataMatch = metadata.match(/all-text-with-urls: (.+)/);
103
+
104
+ if (metadataMatch) {
105
+ const metadataObj = JSON.parse(metadataMatch[1]);
106
+
107
+ sourcesContainer.innerHTML = '<h2>Sources</h2>';
108
+ metadataObj.forEach(([content, url]) => {
109
+ if (content.trim() !== "") {
110
+ const sourceItem = document.createElement('div');
111
+ sourceItem.className = 'source-item';
112
+ const snippet = content.length > 400 ? content.substring(0, 400) + '...' : content;
113
+ sourceItem.innerHTML = `
114
+ <a href="${url}" target="_blank" class="source-url">${url}</a>
115
+ <div class="source-content">
116
+ <div class="source-snippet">${marked.parse(snippet)}</div>
117
+ <div class="source-full">${marked.parse(content)}</div>
118
+ <div class="expand-indicator"></div>
119
+ </div>
120
+ `;
121
+ sourcesContainer.appendChild(sourceItem);
122
+
123
+ const sourceUrl = sourceItem.querySelector('.source-url');
124
+ const sourceContent = sourceItem.querySelector('.source-content');
125
+ const snippetDiv = sourceItem.querySelector('.source-snippet');
126
+ const fullDiv = sourceItem.querySelector('.source-full');
127
+
128
+ sourceContent.addEventListener('click', function(e) {
129
+ if (!sourceItem.classList.contains('expanded')) {
130
+ sourceItem.classList.add('expanded');
131
+ snippetDiv.style.display = 'none';
132
+ fullDiv.style.display = 'block';
133
+ } else if (e.clientY > sourceContent.getBoundingClientRect().bottom - 30) {
134
+ sourceItem.classList.remove('expanded');
135
+ snippetDiv.style.display = 'block';
136
+ fullDiv.style.display = 'none';
137
+ }
138
+ });
139
+
140
+ sourceUrl.addEventListener('click', function(e) {
141
+ e.stopPropagation();
142
+ });
143
+ }
144
+ });
145
+ } else {
146
+ sourcesContainer.innerHTML = '<h2>Sources</h2><p>No source information available.</p>';
147
+ }
148
+ }
149
+
150
+ function scrollToNewContent(previousHeight) {
151
+ const reportContainer = document.getElementById('report-container');
152
+ const newHeight = reportContainer.scrollHeight;
153
+ const scrollDifference = newHeight - previousHeight;
154
+
155
+ if (scrollDifference > 0) {
156
+ const viewportHeight = window.innerHeight;
157
+ const currentScrollPosition = window.pageYOffset;
158
+ const contentBottom = reportContainer.offsetTop + newHeight;
159
+
160
+ // Start scrolling when new content is within 200px of viewport bottom
161
+ if (contentBottom > currentScrollPosition + viewportHeight - 200) {
162
+ window.scrollBy({
163
+ top: scrollDifference,
164
+ behavior: 'smooth'
165
+ });
166
+ }
167
+ }
168
+ }
169
+
170
+ function scrollToTop() {
171
+ window.scrollTo({
172
+ top: 0,
173
+ behavior: 'smooth'
174
+ });
175
+ }
176
+
177
+ async function downloadHTML() {
178
+ try {
179
+ // Fetch the CSS
180
+ const response = await fetch('styles.css');
181
+ let css = await response.text();
182
+
183
+ // Add styles for expanded sources and body max-width
184
+ css += `
185
+ body.report-body {
186
+ max-width: 804px;
187
+ margin: 0 auto;
188
+ }
189
+ .source-item {
190
+ margin-bottom: 20px;
191
+ }
192
+ .source-content {
193
+ margin-top: 10px;
194
+ }
195
+ .source-snippet, .expand-indicator {
196
+ display: none;
197
+ }
198
+ .source-full {
199
+ display: block;
200
+ }
201
+ `;
202
+
203
+ // Clone the document
204
+ const htmlContent = document.implementation.createHTMLDocument('Report');
205
+
206
+ // Set up the basic structure
207
+ htmlContent.documentElement.lang = 'en';
208
+ const head = htmlContent.head;
209
+ const body = htmlContent.body;
210
+ body.className = 'report-body';
211
+
212
+ // Add meta tags
213
+ const meta = htmlContent.createElement('meta');
214
+ meta.charset = 'UTF-8';
215
+ head.appendChild(meta);
216
+
217
+ const viewport = htmlContent.createElement('meta');
218
+ viewport.name = 'viewport';
219
+ viewport.content = 'width=device-width, initial-scale=1.0';
220
+ head.appendChild(viewport);
221
+
222
+ // Add title
223
+ const title = htmlContent.createElement('title');
224
+ title.textContent = 'Generated Report';
225
+ head.appendChild(title);
226
+
227
+ // Add style
228
+ const style = htmlContent.createElement('style');
229
+ style.textContent = css;
230
+ head.appendChild(style);
231
+
232
+ // Add necessary scripts
233
+ const markedScript = htmlContent.createElement('script');
234
+ markedScript.src = 'https://cdn.jsdelivr.net/npm/marked/marked.min.js';
235
+ head.appendChild(markedScript);
236
+
237
+ const plotlyScript = htmlContent.createElement('script');
238
+ plotlyScript.src = 'https://cdn.plot.ly/plotly-latest.min.js';
239
+ head.appendChild(plotlyScript);
240
+
241
+ // Copy the report content
242
+ const reportContainer = document.getElementById('report-container');
243
+ body.innerHTML = reportContainer.innerHTML;
244
+
245
+ // Copy and modify the sources content
246
+ const sourcesContainer = document.getElementById('sources-container');
247
+ const sourcesDiv = htmlContent.createElement('div');
248
+ sourcesDiv.innerHTML = sourcesContainer.innerHTML;
249
+
250
+ // Expand all sources
251
+ const sourceItems = sourcesDiv.querySelectorAll('.source-item');
252
+ sourceItems.forEach(item => {
253
+ item.classList.add('expanded');
254
+ const snippetDiv = item.querySelector('.source-snippet');
255
+ const fullDiv = item.querySelector('.source-full');
256
+ if (snippetDiv) snippetDiv.style.display = 'none';
257
+ if (fullDiv) fullDiv.style.display = 'block';
258
+ });
259
+
260
+ body.appendChild(sourcesDiv);
261
+
262
+ // Create blob and download
263
+ const blob = new Blob([htmlContent.documentElement.outerHTML], { type: 'text/html' });
264
+ const url = URL.createObjectURL(blob);
265
+ const a = document.createElement('a');
266
+ a.href = url;
267
+ a.download = 'report.html';
268
+ document.body.appendChild(a);
269
+ a.click();
270
+ document.body.removeChild(a);
271
+ URL.revokeObjectURL(url);
272
+ } catch (error) {
273
+ console.error('Error downloading HTML:', error);
274
+ }
275
+ }
276
+ // Make Plotly charts responsive on window resize
277
+ window.addEventListener('resize', function() {
278
+ const plots = document.querySelectorAll('.js-plotly-plot');
279
+ plots.forEach(plot => {
280
+ Plotly.Plots.resize(plot);
281
+ });
282
  });