LE Quoc Dat commited on
Commit
b07ae48
·
1 Parent(s): 8f3e422
Files changed (6) hide show
  1. app.py +1 -0
  2. llm_utils.py +13 -6
  3. models.yaml +4 -0
  4. requirements.txt +0 -0
  5. static/js/models.js +4 -4
  6. templates/index.html +8 -17
app.py CHANGED
@@ -88,6 +88,7 @@ def generate_flashcard():
88
  else:
89
  raise ValueError("No JSON object found in response")
90
  except Exception as parse_err:
 
91
  return jsonify({'error': 'JSON parsing error in language mode: ' + str(parse_err)})
92
  elif mode == 'flashcard':
93
  try:
 
88
  else:
89
  raise ValueError("No JSON object found in response")
90
  except Exception as parse_err:
91
+ print("JSON parsing error in language mode: ", parse_err)
92
  return jsonify({'error': 'JSON parsing error in language mode: ' + str(parse_err)})
93
  elif mode == 'flashcard':
94
  try:
llm_utils.py CHANGED
@@ -32,15 +32,22 @@ def generate_completion(prompt: str, model: str = None, api_key: str = None) ->
32
  first_env_var = list(config['models'][0].keys())[0]
33
  model = config['models'][0][first_env_var][0]
34
 
 
35
  env_var = model_to_env.get(model)
36
  if not env_var:
37
- raise ValueError("Model is not supported.")
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- if api_key is None:
40
- api_key = os.getenv(env_var)
41
- if not api_key:
42
- raise ValueError(f"Please set {env_var} environment variable")
43
-
44
  messages = [{"role": "user", "content": prompt}]
45
 
46
  response = completion(
 
32
  first_env_var = list(config['models'][0].keys())[0]
33
  model = config['models'][0][first_env_var][0]
34
 
35
+ # Get the correct environment variable for the model.
36
  env_var = model_to_env.get(model)
37
  if not env_var:
38
+ raise ValueError(f"Model '{model}' is not supported. Available models: {list(model_to_env.keys())}")
39
+
40
+ # First, check if the environment variable is set.
41
+ env_api_key = os.getenv(env_var)
42
+ if env_api_key:
43
+ # Use the API key from environment if available
44
+ api_key = env_api_key
45
+ elif api_key:
46
+ # Fallback to the API key provided by the user
47
+ pass
48
+ else:
49
+ raise ValueError(f"Please set {env_var} environment variable or provide the API key.")
50
 
 
 
 
 
 
51
  messages = [{"role": "user", "content": prompt}]
52
 
53
  response = completion(
models.yaml CHANGED
@@ -1,6 +1,10 @@
1
  models:
2
  - GEMINI_API_KEY:
3
  - "gemini/gemini-exp-1206"
 
 
 
 
4
 
5
  - OPENROUTER_API_KEY:
6
  - "openrouter/google/gemini-exp-1206:free"
 
1
  models:
2
  - GEMINI_API_KEY:
3
  - "gemini/gemini-exp-1206"
4
+ - "gemini/gemini-2.0-flash-lite-preview-02-05"
5
+ - "gemini/gemini-2.0-flash"
6
+ - "gemini/gemini-2.0-pro-exp-02-05"
7
+ - "gemini/gemini-2.0-flash-thinking-exp-01-21"
8
 
9
  - OPENROUTER_API_KEY:
10
  - "openrouter/google/gemini-exp-1206:free"
requirements.txt CHANGED
File without changes
static/js/models.js CHANGED
@@ -1,9 +1,9 @@
1
  const MODEL_API_KEY_MAPPING = {
 
2
  "gemini/gemini-exp-1206": "GEMINI_API_KEY",
3
- // "gemini/gemini-2.0-flash": "GEMINI_API_KEY",
4
- // "gemini/gemini-2.0-flash-lite-preview-02-05": "GEMINI_API_KEY",
5
- // "gemini/gemini-2.0-pro-exp-02-05": "GEMINI_API_KEY",
6
- // "gemini/gemini-2.0-flash-thinking-exp-01-21": "GEMINI_API_KEY",
7
  "openrouter/google/gemini-exp-1206:free": "OPENROUTER_API_KEY",
8
  "openrouter/anthropic/claude-3-haiku-20240307": "OPENROUTER_API_KEY",
9
  "openrouter/anthropic/claude-3-sonnet-20240229": "OPENROUTER_API_KEY"
 
1
  const MODEL_API_KEY_MAPPING = {
2
+ "gemini/gemini-2.0-flash": "GEMINI_API_KEY",
3
  "gemini/gemini-exp-1206": "GEMINI_API_KEY",
4
+ "gemini/gemini-2.0-flash-lite-preview-02-05": "GEMINI_API_KEY",
5
+ "gemini/gemini-2.0-pro-exp-02-05": "GEMINI_API_KEY",
6
+ "gemini/gemini-2.0-flash-thinking-exp-01-21": "GEMINI_API_KEY",
 
7
  "openrouter/google/gemini-exp-1206:free": "OPENROUTER_API_KEY",
8
  "openrouter/anthropic/claude-3-haiku-20240307": "OPENROUTER_API_KEY",
9
  "openrouter/anthropic/claude-3-sonnet-20240229": "OPENROUTER_API_KEY"
templates/index.html CHANGED
@@ -461,11 +461,6 @@
461
  }
462
 
463
  async function generateLanguageFlashcard(word, phrase, targetLanguage) {
464
- if (!apiKey) {
465
- alert('Please enter your API key first.');
466
- return;
467
- }
468
-
469
  const prompt = document.getElementById('language-prompt').value
470
  .replace('{word}', word)
471
  .replace('{phrase}', phrase)
@@ -493,11 +488,6 @@
493
  }
494
 
495
  async function generateContent() {
496
- if (!apiKey) {
497
- alert('Please enter your API key first.');
498
- return;
499
- }
500
-
501
  const selection = window.getSelection();
502
  if (selection.rangeCount > 0 && selection.toString().trim() !== '') {
503
  const selectedText = selection.toString();
@@ -511,7 +501,7 @@
511
  } else {
512
  return;
513
  }
514
-
515
  // Disable the button and show notification
516
  submitBtn.disabled = true;
517
  submitBtn.style.backgroundColor = '#808080';
@@ -1367,19 +1357,20 @@
1367
  modelSelect.appendChild(option);
1368
  });
1369
 
1370
- // Set default model to Gemini
1371
- modelSelect.value = "gemini/gemini-exp-1206";
1372
- selectedModel = "gemini/gemini-exp-1206";
 
1373
 
1374
- // Update API key placeholder based on selected model
1375
  modelSelect.addEventListener('change', function() {
1376
  selectedModel = this.value;
1377
  const requiredKey = MODEL_API_KEY_MAPPING[selectedModel];
1378
  apiKeyInput.placeholder = `Enter ${requiredKey}`;
1379
  });
1380
 
1381
- // Set initial API key placeholder
1382
- const initialKey = MODEL_API_KEY_MAPPING["gemini/gemini-exp-1206"];
1383
  apiKeyInput.placeholder = `Enter ${initialKey}`;
1384
  });
1385
  </script>
 
461
  }
462
 
463
  async function generateLanguageFlashcard(word, phrase, targetLanguage) {
 
 
 
 
 
464
  const prompt = document.getElementById('language-prompt').value
465
  .replace('{word}', word)
466
  .replace('{phrase}', phrase)
 
488
  }
489
 
490
  async function generateContent() {
 
 
 
 
 
491
  const selection = window.getSelection();
492
  if (selection.rangeCount > 0 && selection.toString().trim() !== '') {
493
  const selectedText = selection.toString();
 
501
  } else {
502
  return;
503
  }
504
+
505
  // Disable the button and show notification
506
  submitBtn.disabled = true;
507
  submitBtn.style.backgroundColor = '#808080';
 
1357
  modelSelect.appendChild(option);
1358
  });
1359
 
1360
+ // Set default model to the first one in the list instead of hard-coding Gemini
1361
+ const firstModel = availableModels[0]; // New: use the first model from availableModels
1362
+ modelSelect.value = firstModel; // Update the select element
1363
+ selectedModel = firstModel; // Update the global selectedModel value
1364
 
1365
+ // Update API key placeholder based on selected model on change
1366
  modelSelect.addEventListener('change', function() {
1367
  selectedModel = this.value;
1368
  const requiredKey = MODEL_API_KEY_MAPPING[selectedModel];
1369
  apiKeyInput.placeholder = `Enter ${requiredKey}`;
1370
  });
1371
 
1372
+ // Set initial API key placeholder based on the first model
1373
+ const initialKey = MODEL_API_KEY_MAPPING[firstModel]; // Updated to use firstModel
1374
  apiKeyInput.placeholder = `Enter ${initialKey}`;
1375
  });
1376
  </script>