-
Notifications
You must be signed in to change notification settings - Fork 4
/
api_key_manager.py
32 lines (27 loc) · 994 Bytes
/
api_key_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import os
import json
from PyQt5.QtWidgets import QInputDialog, QApplication # Added QApplication import
import openai
# Import constants
from constants import API_KEY_FILE
class APIKeyManager:
def __init__(self, parent=None):
self.parent = parent
self.api_key = self.load_api_key()
if not self.api_key:
self.ask_for_api_key()
def load_api_key(self):
if os.path.exists(API_KEY_FILE):
with open(API_KEY_FILE, 'r') as file:
data = json.load(file)
return data.get('api_key', '')
return ''
def ask_for_api_key(self):
api_key, ok = QInputDialog.getText(self.parent, 'API Key', 'Enter your OpenAI API Key:')
if ok and api_key:
self.api_key = api_key
openai.api_key = api_key
self.save_api_key(api_key)
def save_api_key(self, api_key):
with open(API_KEY_FILE, 'w') as file:
json.dump({'api_key': api_key}, file)