diff --git a/main.py b/main.py index 3a9e23f..bbf7c8d 100644 --- a/main.py +++ b/main.py @@ -59,7 +59,11 @@ class FlashcardCreator: self.llm_provider = settings.get("provider", self.llm_provider) self.llm_model = settings.get("model", self.llm_model) self.base_url = settings.get("base_url", self.base_url) - self.api_key = settings.get("api_key", self.api_key) + # Load provider-specific settings + provider_settings = settings.get(self.llm_provider, {}) + self.llm_model = provider_settings.get("model", self.llm_model) + self.base_url = provider_settings.get("base_url", self.base_url) + self.api_key = provider_settings.get("api_key", self.api_key) except: pass @@ -209,17 +213,24 @@ class FlashcardCreator: settings_dir = os.path.join(os.path.expanduser("~"), ".flashcard_creator") os.makedirs(settings_dir, exist_ok=True) - with open(os.path.join(settings_dir, "settings.json"), "w") as f: - print(self.base_url) - json.dump( - { - "provider": self.llm_provider, - "model": self.llm_model, - "base_url": self.base_url, - "api_key": self.api_key, - }, - f, - ) + settings_path = os.path.join(settings_dir, "settings.json") + settings = {} + if os.path.exists(settings_path): + try: + with open(settings_path, "r") as f: + settings = json.load(f) + except: + pass + + # Update provider-specific settings + settings[self.llm_provider] = { + "model": self.llm_model, + "base_url": self.base_url, + "api_key": self.api_key, + } + + with open(settings_path, "w") as f: + json.dump(settings, f, indent=4) e.page.close(dialog) e.page.update() @@ -235,9 +246,11 @@ class FlashcardCreator: settings = json.load(f) print(settings) self.llm_provider = settings.get("provider", self.llm_provider) - self.llm_model = settings.get("model", self.llm_model) - self.base_url = settings.get("base_url", self.base_url) - self.api_key = settings.get("api_key", self.api_key) + # Load provider-specific settings + provider_settings = settings.get(self.llm_provider, {}) + self.llm_model = provider_settings.get("model", self.llm_model) + self.base_url = provider_settings.get("base_url", self.base_url) + self.api_key = provider_settings.get("api_key", self.api_key) except: pass @@ -248,6 +261,7 @@ class FlashcardCreator: ft.dropdown.Option("anthropic"), ft.dropdown.Option("vertexai"), ft.dropdown.Option("huggingface"), + ft.dropdown.Option("deepseek"), ], value=self.llm_provider, ) @@ -324,6 +338,7 @@ class FlashcardCreator: 5. For idiomatic expressions, provide the closest equivalent in the target language 6. Choose content that represents different difficulty levels 7. Ensure translations are accurate and natural-sounding in the target language + 8. Make sure to create flashcards for ALL relevant words in the input! If you are unsure, err on the side of too many flashcards. OUTPUT FORMAT: Return your response in valid YAML format with the following structure: @@ -354,7 +369,6 @@ class FlashcardCreator: - "我很高兴认识你": "Es freut mich, dich kennenzulernen" - "慢走": "Komm gut nach Hause (idiom.)" ``` - Here's the text to process: @@ -363,7 +377,20 @@ class FlashcardCreator: try: # Set the API key for the selected provider - os.environ[f"{self.llm_provider.upper()}_API_KEY"] = self.api_key + # Load provider-specific settings + settings_path = os.path.join( + os.path.expanduser("~"), ".flashcard_creator", "settings.json" + ) + settings = {} + if os.path.exists(settings_path): + with open(settings_path, "r") as f: + try: + settings = json.load(f) + except: + pass + provider_settings = settings.get(self.llm_provider, {}) + api_key = provider_settings.get("api_key", "") + os.environ[f"{self.llm_provider.upper()}_API_KEY"] = api_key logger.info("Sending request to LLM API...") @@ -628,6 +655,7 @@ class FlashcardCreator: try: # Prepare notes for addition notes = [] + logging.info(f"Preparing to create {len(cards)} Anki cards in deck: {deck}") for card in cards: notes.append( { @@ -640,14 +668,20 @@ class FlashcardCreator: ) # Add notes to Anki + logging.info("Sending request to AnkiConnect to add notes") response = requests.post( self.anki_connect_url, json={"action": "addNotes", "version": 6, "params": {"notes": notes}}, ) data = response.json() - return all(id is not None for id in data.get("result", [])) + success = all(id is not None for id in data.get("result", [])) + if success: + logging.info("Successfully added all cards to Anki") + else: + logging.warning("Some cards failed to be added to Anki") + return success except Exception as e: - print(f"Error creating Anki cards: {e}") + logging.error(f"Error creating Anki cards: {e}") return False def export_as_yaml(self, e):