LE Quoc Dat commited on
Commit
f2de0b2
·
1 Parent(s): bb56080

working nicely

Browse files
Files changed (3) hide show
  1. app.py +8 -3
  2. static/js/prompts.js +17 -3
  3. templates/index.html +8 -6
app.py CHANGED
@@ -92,9 +92,14 @@ def generate_flashcard():
92
  return jsonify({'error': 'JSON parsing error in language mode: ' + str(parse_err)})
93
  elif mode == 'flashcard':
94
  try:
95
- # Expecting a JSON array, each element having "question" and "answer"
96
- flashcards = json.loads(content)
97
- return jsonify({'flashcards': flashcards})
 
 
 
 
 
98
  except Exception as parse_err:
99
  return jsonify({'error': 'JSON parsing error in flashcard mode: ' + str(parse_err)})
100
  elif mode == 'explain':
 
92
  return jsonify({'error': 'JSON parsing error in language mode: ' + str(parse_err)})
93
  elif mode == 'flashcard':
94
  try:
95
+ # Extract the JSON array substring from the content in case there is extra text.
96
+ json_match = re.search(r'\[[\s\S]*\]', content)
97
+ if json_match:
98
+ json_text = json_match.group(0)
99
+ flashcards = json.loads(json_text)
100
+ return jsonify({'flashcards': flashcards})
101
+ else:
102
+ raise ValueError("No JSON array found in response")
103
  except Exception as parse_err:
104
  return jsonify({'error': 'JSON parsing error in flashcard mode: ' + str(parse_err)})
105
  elif mode == 'explain':
static/js/prompts.js CHANGED
@@ -14,8 +14,8 @@ Example output:
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
 
@@ -56,4 +56,18 @@ Example output for malformed input:
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.`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  }
15
  ]
16
 
17
+ Please output only the JSON array with no additional text or commentary.
18
+ Now generate flashcards for the text below:`;
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
 
 
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.`;
60
+
61
+ /*
62
+ Example input for multiple words:
63
+ Word: "better suited for the region than some of the more traditional ones embraced by agribusiness."
64
+ Phrase: "change and better suited for the region than some of"
65
+
66
+ Example output:
67
+ {
68
+ "word": "better suited for the region than some of the more traditional ones embraced by agribusiness.",
69
+ "translation": "thích hợp hơn cho khu vực hơn là một số trong số những phương pháp truyền thống được chấp nhận bởi ngành nông nghiệp",
70
+ "question": "The proposal is <b>better suited for the region than some of the more traditional ones embraced by agribusiness</b>.",
71
+ "answer": "It means that the proposal fits the local context better compared to older, conventional methods."
72
+ }
73
+ */
templates/index.html CHANGED
@@ -560,7 +560,8 @@
560
  flashcardElement.remove();
561
  updateExportButtonVisibility();
562
  });
563
- flashcardsContainer.appendChild(flashcardElement);
 
564
  });
565
  updateExportButtonVisibility();
566
  }
@@ -573,15 +574,16 @@
573
  flashcardElement.dataset.translation = flashcard.translation;
574
  flashcardElement.dataset.answer = flashcard.answer;
575
  flashcardElement.innerHTML = `
576
- <div style="font-size: 1.2em; margin-bottom: 10px;"><b>${flashcard.word}</b>: ${flashcard.translation}</div>
577
- <div>- ${flashcard.answer}</div>
578
- <button class="remove-btn">Remove</button>
579
- `;
580
  flashcardElement.querySelector('.remove-btn').addEventListener('click', function () {
581
  flashcardElement.remove();
582
  updateExportButtonVisibility();
583
  });
584
- flashcardsContainer.appendChild(flashcardElement);
 
585
  updateExportButtonVisibility();
586
  }
587
 
 
560
  flashcardElement.remove();
561
  updateExportButtonVisibility();
562
  });
563
+ // Prepend new flashcard to the container to show it at the top
564
+ flashcardsContainer.insertBefore(flashcardElement, flashcardsContainer.firstChild);
565
  });
566
  updateExportButtonVisibility();
567
  }
 
574
  flashcardElement.dataset.translation = flashcard.translation;
575
  flashcardElement.dataset.answer = flashcard.answer;
576
  flashcardElement.innerHTML = `
577
+ <div style="font-size: 1.2em; margin-bottom: 10px;"><b>${flashcard.word}</b>: ${flashcard.translation}</div>
578
+ <div>- ${flashcard.answer}</div>
579
+ <button class="remove-btn">Remove</button>
580
+ `;
581
  flashcardElement.querySelector('.remove-btn').addEventListener('click', function () {
582
  flashcardElement.remove();
583
  updateExportButtonVisibility();
584
  });
585
+ // Prepend new language flashcard at the top of the container
586
+ flashcardsContainer.insertBefore(flashcardElement, flashcardsContainer.firstChild);
587
  updateExportButtonVisibility();
588
  }
589