refactor: Update settings handling and add logging for Anki card creation
This commit is contained in:
parent
7002b4437e
commit
35df3eaab8
72
main.py
72
main.py
@ -59,7 +59,11 @@ class FlashcardCreator:
|
|||||||
self.llm_provider = settings.get("provider", self.llm_provider)
|
self.llm_provider = settings.get("provider", self.llm_provider)
|
||||||
self.llm_model = settings.get("model", self.llm_model)
|
self.llm_model = settings.get("model", self.llm_model)
|
||||||
self.base_url = settings.get("base_url", self.base_url)
|
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:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -209,17 +213,24 @@ class FlashcardCreator:
|
|||||||
settings_dir = os.path.join(os.path.expanduser("~"), ".flashcard_creator")
|
settings_dir = os.path.join(os.path.expanduser("~"), ".flashcard_creator")
|
||||||
os.makedirs(settings_dir, exist_ok=True)
|
os.makedirs(settings_dir, exist_ok=True)
|
||||||
|
|
||||||
with open(os.path.join(settings_dir, "settings.json"), "w") as f:
|
settings_path = os.path.join(settings_dir, "settings.json")
|
||||||
print(self.base_url)
|
settings = {}
|
||||||
json.dump(
|
if os.path.exists(settings_path):
|
||||||
{
|
try:
|
||||||
"provider": self.llm_provider,
|
with open(settings_path, "r") as f:
|
||||||
"model": self.llm_model,
|
settings = json.load(f)
|
||||||
"base_url": self.base_url,
|
except:
|
||||||
"api_key": self.api_key,
|
pass
|
||||||
},
|
|
||||||
f,
|
# 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.close(dialog)
|
||||||
e.page.update()
|
e.page.update()
|
||||||
@ -235,9 +246,11 @@ class FlashcardCreator:
|
|||||||
settings = json.load(f)
|
settings = json.load(f)
|
||||||
print(settings)
|
print(settings)
|
||||||
self.llm_provider = settings.get("provider", self.llm_provider)
|
self.llm_provider = settings.get("provider", self.llm_provider)
|
||||||
self.llm_model = settings.get("model", self.llm_model)
|
# Load provider-specific settings
|
||||||
self.base_url = settings.get("base_url", self.base_url)
|
provider_settings = settings.get(self.llm_provider, {})
|
||||||
self.api_key = settings.get("api_key", self.api_key)
|
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:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -248,6 +261,7 @@ class FlashcardCreator:
|
|||||||
ft.dropdown.Option("anthropic"),
|
ft.dropdown.Option("anthropic"),
|
||||||
ft.dropdown.Option("vertexai"),
|
ft.dropdown.Option("vertexai"),
|
||||||
ft.dropdown.Option("huggingface"),
|
ft.dropdown.Option("huggingface"),
|
||||||
|
ft.dropdown.Option("deepseek"),
|
||||||
],
|
],
|
||||||
value=self.llm_provider,
|
value=self.llm_provider,
|
||||||
)
|
)
|
||||||
@ -324,6 +338,7 @@ class FlashcardCreator:
|
|||||||
5. For idiomatic expressions, provide the closest equivalent in the target language
|
5. For idiomatic expressions, provide the closest equivalent in the target language
|
||||||
6. Choose content that represents different difficulty levels
|
6. Choose content that represents different difficulty levels
|
||||||
7. Ensure translations are accurate and natural-sounding in the target language
|
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:
|
OUTPUT FORMAT:
|
||||||
Return your response in valid YAML format with the following structure:
|
Return your response in valid YAML format with the following structure:
|
||||||
@ -354,7 +369,6 @@ class FlashcardCreator:
|
|||||||
- "我很高兴认识你": "Es freut mich, dich kennenzulernen"
|
- "我很高兴认识你": "Es freut mich, dich kennenzulernen"
|
||||||
- "慢走": "Komm gut nach Hause (idiom.)"
|
- "慢走": "Komm gut nach Hause (idiom.)"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Here's the text to process:
|
Here's the text to process:
|
||||||
|
|
||||||
@ -363,7 +377,20 @@ class FlashcardCreator:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
# Set the API key for the selected provider
|
# 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...")
|
logger.info("Sending request to LLM API...")
|
||||||
|
|
||||||
@ -628,6 +655,7 @@ class FlashcardCreator:
|
|||||||
try:
|
try:
|
||||||
# Prepare notes for addition
|
# Prepare notes for addition
|
||||||
notes = []
|
notes = []
|
||||||
|
logging.info(f"Preparing to create {len(cards)} Anki cards in deck: {deck}")
|
||||||
for card in cards:
|
for card in cards:
|
||||||
notes.append(
|
notes.append(
|
||||||
{
|
{
|
||||||
@ -640,14 +668,20 @@ class FlashcardCreator:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Add notes to Anki
|
# Add notes to Anki
|
||||||
|
logging.info("Sending request to AnkiConnect to add notes")
|
||||||
response = requests.post(
|
response = requests.post(
|
||||||
self.anki_connect_url,
|
self.anki_connect_url,
|
||||||
json={"action": "addNotes", "version": 6, "params": {"notes": notes}},
|
json={"action": "addNotes", "version": 6, "params": {"notes": notes}},
|
||||||
)
|
)
|
||||||
data = response.json()
|
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:
|
except Exception as e:
|
||||||
print(f"Error creating Anki cards: {e}")
|
logging.error(f"Error creating Anki cards: {e}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def export_as_yaml(self, e):
|
def export_as_yaml(self, e):
|
||||||
|
Loading…
Reference in New Issue
Block a user