Spaces:
Runtime error
Runtime error
LE Quoc Dat
commited on
Commit
·
bb56080
1
Parent(s):
38acb6a
prompt
Browse files- requirements.txt +1 -0
- static/js/prompts.js +18 -3
- templates/index.html +39 -12
requirements.txt
CHANGED
@@ -1,3 +1,4 @@
|
|
1 |
flask_cors
|
2 |
flask
|
3 |
anthropic
|
|
|
|
1 |
flask_cors
|
2 |
flask
|
3 |
anthropic
|
4 |
+
python-dotenv
|
static/js/prompts.js
CHANGED
@@ -14,7 +14,8 @@ Example output:
|
|
14 |
}
|
15 |
]
|
16 |
|
17 |
-
Now generate flashcards for this text
|
|
|
18 |
|
19 |
const EXPLAIN_PROMPT = `Explain the following text in simple terms, focusing on the main concepts and their relationships. Use clear and concise language, and break down complex ideas into easily understandable parts. If there are any technical terms, provide brief explanations for them. Return your explanation in a JSON object with an "explanation" key.
|
20 |
|
@@ -23,7 +24,8 @@ Example output:
|
|
23 |
"explanation": "Load balancing is a technique in parallel computing that ensures work is distributed evenly across different processing units. Think of it like distributing tasks among team members - when done well, everyone has a fair amount of work and the team is more efficient. There are two main approaches: dynamic balancing (adjusting work distribution as needed) and static balancing (planning the distribution ahead of time)."
|
24 |
}
|
25 |
|
26 |
-
Now explain this text
|
|
|
27 |
|
28 |
const LANGUAGE_PROMPT = `Return a JSON object with "word", "translation", "question", and "answer" keys for the given word in {targetLanguage}.
|
29 |
|
@@ -39,6 +41,19 @@ Example output:
|
|
39 |
"answer": "Declined to accept or comply with a request or proposal."
|
40 |
}
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
Now explain the word in the phrase below:
|
43 |
Word: "{word}"
|
44 |
-
Phrase: "{phrase}"
|
|
|
|
14 |
}
|
15 |
]
|
16 |
|
17 |
+
Now generate flashcards for this text:
|
18 |
+
Please output only the JSON array with no additional text or commentary.`;
|
19 |
|
20 |
const EXPLAIN_PROMPT = `Explain the following text in simple terms, focusing on the main concepts and their relationships. Use clear and concise language, and break down complex ideas into easily understandable parts. If there are any technical terms, provide brief explanations for them. Return your explanation in a JSON object with an "explanation" key.
|
21 |
|
|
|
24 |
"explanation": "Load balancing is a technique in parallel computing that ensures work is distributed evenly across different processing units. Think of it like distributing tasks among team members - when done well, everyone has a fair amount of work and the team is more efficient. There are two main approaches: dynamic balancing (adjusting work distribution as needed) and static balancing (planning the distribution ahead of time)."
|
25 |
}
|
26 |
|
27 |
+
Now explain this text:
|
28 |
+
Please output only the JSON object with no additional text or commentary.`;
|
29 |
|
30 |
const LANGUAGE_PROMPT = `Return a JSON object with "word", "translation", "question", and "answer" keys for the given word in {targetLanguage}.
|
31 |
|
|
|
41 |
"answer": "Declined to accept or comply with a request or proposal."
|
42 |
}
|
43 |
|
44 |
+
Sometimes the input may be malformed or incomplete:
|
45 |
+
Word: "@foreignminister"
|
46 |
+
Phrase: ""
|
47 |
+
|
48 |
+
Example output for malformed input:
|
49 |
+
{
|
50 |
+
"word": "foreign minister",
|
51 |
+
"translation": "bộ trưởng ngoại giao",
|
52 |
+
"question": "The <b>foreign minister</b> announced new trade agreements with neighboring countries.",
|
53 |
+
"answer": "The government minister responsible for a country's foreign policy and relations."
|
54 |
+
}
|
55 |
+
|
56 |
Now explain the word in the phrase below:
|
57 |
Word: "{word}"
|
58 |
+
Phrase: "{phrase}"
|
59 |
+
Please output only the JSON object without any additional text or commentary.`;
|
templates/index.html
CHANGED
@@ -847,11 +847,9 @@
|
|
847 |
systemPrompt.style.display = mode === 'flashcard' ? 'block' : 'none';
|
848 |
document.getElementById('explain-prompt').style.display = mode === 'explain' ? 'block' : 'none';
|
849 |
document.getElementById('language-prompt').style.display = mode === 'language' ? 'block' : 'none';
|
850 |
-
submitBtn.style.display =
|
851 |
-
|
852 |
-
//
|
853 |
-
|
854 |
-
// Update the collection button text and export button visibility as before
|
855 |
updateAddToCollectionButtonText();
|
856 |
updateExportButtonVisibility();
|
857 |
});
|
@@ -867,9 +865,8 @@
|
|
867 |
saveLanguageChoice(targetLanguage);
|
868 |
// Ensure the Language mode button remains selected
|
869 |
document.querySelector('.mode-btn[data-mode="language"]').classList.add('selected');
|
870 |
-
// Keep language buttons visible and Generate button
|
871 |
-
|
872 |
-
submitBtn.style.display = 'none';
|
873 |
// Set the mode to 'language'
|
874 |
mode = 'language';
|
875 |
});
|
@@ -945,12 +942,12 @@
|
|
945 |
return {
|
946 |
element: highlight,
|
947 |
pageNumber: parseInt(pageDiv.dataset.pageNumber),
|
948 |
-
|
949 |
left: rect.left - pageBounds.left,
|
950 |
top: rect.top - pageBounds.top,
|
951 |
width: rect.width,
|
952 |
height: rect.height
|
953 |
-
}
|
954 |
};
|
955 |
}
|
956 |
|
@@ -1036,7 +1033,37 @@
|
|
1036 |
}
|
1037 |
}
|
1038 |
|
1039 |
-
submitBtn.addEventListener('click',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1040 |
|
1041 |
apiKeyInput.addEventListener('change', function () {
|
1042 |
apiKey = this.value;
|
@@ -1284,7 +1311,7 @@
|
|
1284 |
mode = 'language';
|
1285 |
document.querySelector('.mode-btn[data-mode="language"]').classList.add('selected');
|
1286 |
document.getElementById('language-buttons').style.display = 'flex';
|
1287 |
-
document.getElementById('submit-btn').style.display = '
|
1288 |
systemPrompt.style.display = 'none';
|
1289 |
document.getElementById('explain-prompt').style.display = 'none';
|
1290 |
document.getElementById('language-prompt').style.display = 'block';
|
|
|
847 |
systemPrompt.style.display = mode === 'flashcard' ? 'block' : 'none';
|
848 |
document.getElementById('explain-prompt').style.display = mode === 'explain' ? 'block' : 'none';
|
849 |
document.getElementById('language-prompt').style.display = mode === 'language' ? 'block' : 'none';
|
850 |
+
submitBtn.style.display = 'block';
|
851 |
+
|
852 |
+
// Update the collection button text and export button visibility
|
|
|
|
|
853 |
updateAddToCollectionButtonText();
|
854 |
updateExportButtonVisibility();
|
855 |
});
|
|
|
865 |
saveLanguageChoice(targetLanguage);
|
866 |
// Ensure the Language mode button remains selected
|
867 |
document.querySelector('.mode-btn[data-mode="language"]').classList.add('selected');
|
868 |
+
// Keep language buttons visible and Generate button visible
|
869 |
+
submitBtn.style.display = 'block';
|
|
|
870 |
// Set the mode to 'language'
|
871 |
mode = 'language';
|
872 |
});
|
|
|
942 |
return {
|
943 |
element: highlight,
|
944 |
pageNumber: parseInt(pageDiv.dataset.pageNumber),
|
945 |
+
rects: [{
|
946 |
left: rect.left - pageBounds.left,
|
947 |
top: rect.top - pageBounds.top,
|
948 |
width: rect.width,
|
949 |
height: rect.height
|
950 |
+
}]
|
951 |
};
|
952 |
}
|
953 |
|
|
|
1033 |
}
|
1034 |
}
|
1035 |
|
1036 |
+
submitBtn.addEventListener('click', function() {
|
1037 |
+
if (mode === 'language') {
|
1038 |
+
const selection = window.getSelection();
|
1039 |
+
if (selection.rangeCount > 0) {
|
1040 |
+
const range = selection.getRangeAt(0);
|
1041 |
+
const selectedText = selection.toString().trim();
|
1042 |
+
|
1043 |
+
if (selectedText !== '') {
|
1044 |
+
let selectedLanguageButton = document.querySelector('#language-buttons .mode-btn.selected');
|
1045 |
+
if (!selectedLanguageButton) {
|
1046 |
+
// Fallback: use the default language from localStorage
|
1047 |
+
const defaultLanguage = loadLanguageChoice();
|
1048 |
+
setLanguageButton(defaultLanguage);
|
1049 |
+
selectedLanguageButton = document.querySelector('#language-buttons .mode-btn.selected');
|
1050 |
+
}
|
1051 |
+
if (selectedLanguageButton) {
|
1052 |
+
const targetLanguage = selectedLanguageButton.dataset.language;
|
1053 |
+
const phrase = getPhrase(range, selectedText);
|
1054 |
+
generateLanguageFlashcard(selectedText, phrase, targetLanguage);
|
1055 |
+
speakWord(selectedText);
|
1056 |
+
}
|
1057 |
+
} else {
|
1058 |
+
alert('Please select some text first');
|
1059 |
+
}
|
1060 |
+
} else {
|
1061 |
+
alert('Please select some text first');
|
1062 |
+
}
|
1063 |
+
} else {
|
1064 |
+
generateContent();
|
1065 |
+
}
|
1066 |
+
});
|
1067 |
|
1068 |
apiKeyInput.addEventListener('change', function () {
|
1069 |
apiKey = this.value;
|
|
|
1311 |
mode = 'language';
|
1312 |
document.querySelector('.mode-btn[data-mode="language"]').classList.add('selected');
|
1313 |
document.getElementById('language-buttons').style.display = 'flex';
|
1314 |
+
document.getElementById('submit-btn').style.display = 'block';
|
1315 |
systemPrompt.style.display = 'none';
|
1316 |
document.getElementById('explain-prompt').style.display = 'none';
|
1317 |
document.getElementById('language-prompt').style.display = 'block';
|