-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrefine.js
More file actions
211 lines (167 loc) · 6.94 KB
/
refine.js
File metadata and controls
211 lines (167 loc) · 6.94 KB
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
// Import GoogleGenerativeAI module
import { GoogleGenerativeAI } from 'https://esm.run/@google/generative-ai';
// Function to fetch the contents of a file
async function getFileContents(filename) {
const response = await fetch(filename);
const text = await response.text();
return text;
}
function showLoader() {
var loader = document.createElement('div');
loader.className = 'loader';
document.body.appendChild(loader);
}
function hideLoader() {
var loader = document.querySelector('.loader');
if (loader) {
loader.remove();
}
}
// Update the content of the HTML element with the formatted generated text
function updateCopy() {
const copyElement = document.getElementById('copyText');
// Gradually increase opacity
let opacity = 0;
const fadeInInterval = setInterval(function () {
opacity += 0.05; // Increase opacity gradually
copyElement.style.opacity = opacity;
if (opacity >= 1) {
clearInterval(fadeInInterval); // Stop interval when opacity reaches 1
}
}, 10); // Adjust the interval time (milliseconds) for smoother animation
}
updateCopy();
// Initialize GoogleGenerativeAI object with your API key
async function initializeGenerativeAI() {
// Fetch API key from api_key.txt
const key = await getFileContents('api_key.txt');
const API_KEY = key.trim(); // Remove leading/trailing whitespace
// Return the initialized GoogleGenerativeAI object
return new GoogleGenerativeAI(API_KEY);
}
// Retrieve the stored HTML content from sessionStorage
const storedHTML = sessionStorage.getItem('generatedTextHTML');
// Check if there is stored HTML content
// Retrieve the stored HTML content from sessionStorage after page load
window.onload = function () {
const storedHTML = sessionStorage.getItem('generatedTextHTML');
if (storedHTML) {
document.getElementById('generatedText').innerHTML = storedHTML;
} else {
console.log('No stored HTML content found.');
}
};
var loader = document.querySelector('.loader');
// Check if the loader element exists
if (loader) {
// Set the opacity to 1 to make the loader fully visible
hideLoader();
} else {
console.error('Loader element not found.');
}
// Function to generate text and update HTML content
async function run() {
let outputText = document.getElementById('generatedText').textContent;
let changes = document.getElementById('inputText').value;
// Retrieve the outputText element
// Get the generatedTextElement
const generatedTextElement = document.getElementById('generatedText');
// Clear the generatedTextElement
generatedTextElement.innerHTML = ''; // Clear the content
// Check if the loader element exists
if (loader) {
// Set the opacity to 1 to make the loader fully visible
showLoader();
} else {
console.error('Loader element not found.');
}
// Initialize GoogleGenerativeAI object with the API key
const genAI = await initializeGenerativeAI();
// For text-only input, use the gemini-pro model
const model = genAI.getGenerativeModel({ model: 'gemini-pro' });
// Construct the prompt by concatenating outputText with additional prompt text
let prompt =
"'DO NOT INCLUDE BACKTICKS IN THE RESPONSE. PROVIDE ONLY CODE IN YOUR RESPONSE, NO OTHER TEXT. - make the following changes to the code below:" +
changes +
'\n\n' +
outputText +
'DO NOT INCLUDE BACKTICKS OR LANGUAGE NAME IN THE RESPONSE. BE SURE TO INCLUDE NECESSARY LIBRARIES. ALWAYS GIVE BACK ENTIRE CODE WITH CHANGES, NOT JUST THE CHANGES';
console.log('prompt1: ', prompt);
let result = await model.generateContent(prompt); // Specify max_tokens here
let response = await result.response;
let text = response.text();
function removeLinesStartingWithPrefix(text, prefix) {
// Create a regular expression to match lines starting with the prefix
var regex = new RegExp('^' + prefix + '.*$', 'gm');
// Replace lines starting with the prefix with an empty string
var result = text.replace(regex, '');
return result;
}
text = removeLinesStartingWithPrefix(text, '`');
let iconElement = document.getElementById('copyID');
iconElement.classList.remove('copyIconClicked');
iconElement.classList.add('copyIcon');
generatedTextElement.textContent = text;
// Check if the loader element exists
if (loader) {
// Set the opacity to 1 to make the loader fully visible
hideLoader();
} else {
console.error('Loader element not found.');
}
// Create a function to save text content as a file
function saveTextAsFile(text, filename) {
// Create a Blob containing the text content
const blob = new Blob([text], { type: 'text/plain' });
// Create a temporary anchor element
const anchorElement = document.createElement('a');
// Set the download attribute to specify the filename
anchorElement.download = filename;
// Set the href attribute to the Blob object
anchorElement.href = window.URL.createObjectURL(blob);
// Trigger a click event on the anchor element
anchorElement.click();
// Clean up: remove the anchor element and revoke the URL object
window.URL.revokeObjectURL(anchorElement.href);
anchorElement.remove();
}
// Get the download link element
const downloadLink = document.getElementById('downloadLink');
// Attach event listener to the download link
downloadLink.addEventListener('click', (event) => {
event.preventDefault(); // Prevent the default behavior of the <a> tag
// Get the file name from session storage
const uploadedFileName = sessionStorage.getItem('uploadedFileName');
// Construct the file name for download
const fileName = `${uploadedFileName}.cpp`;
// Get the text content from the generatedText element without <div> tags
const generatedTextElement = document.getElementById('generatedText');
let textContent = generatedTextElement.innerText; // Use innerText to get text content
textContent = textContent.replace(/</g, '<').replace(/>/g, '>'); // Revert HTML entities
// Construct the URL for download
const downloadURL = `data:text/plain;charset=utf-8,${encodeURIComponent(
textContent
)}`;
// Create a temporary anchor element
const anchorElement = document.createElement('a');
// Set the download attribute to specify the filename
anchorElement.download = fileName;
// Set the href attribute to the download URL
anchorElement.href = downloadURL;
// Trigger a click event on the anchor element
anchorElement.click();
// Clean up: remove the anchor element
anchorElement.remove();
});
// Save the code to send to refine if needed
function passText() {
const generatedText = document.getElementById('generatedText').textContent;
// Store text in sessionStorage
sessionStorage.setItem('generatedText', generatedText);
}
passText();
}
// Add an event listener to the submit button
document.getElementById('submitBtn').addEventListener('click', function () {
run(); // Call the run() function when the button is clicked
});