Skip to content

Commit

Permalink
Upgraded to OpenAI API v1.1.0 (#81)
Browse files Browse the repository at this point in the history
Update to work with the latest OpenAI library release, which is not backwards compatible, to say the least.

Also includes some fixes for the Dark theme on MacOS, which got caught in the branch.
  • Loading branch information
machinewrapped authored Nov 8, 2023
1 parent ad9192f commit 96b9d4e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
11 changes: 6 additions & 5 deletions PySubtitleGPT/ChatGPTClient.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
import time
import openai
import openai.error

from PySubtitleGPT.ChatGPTPrompt import ChatGPTPrompt
from PySubtitleGPT.ChatGPTTranslation import ChatGPTTranslation
Expand Down Expand Up @@ -99,9 +98,11 @@ def SendMessages(self, messages : list[str], temperature : float = None):
translation = {}
retries = 0

client = openai.OpenAI(api_key=options.api_key(), base_url=options.api_base())

while retries <= max_retries:
try:
response = openai.ChatCompletion.create(
response = client.chat.completions.create(
model=model,
messages=messages,
temperature=temperature
Expand All @@ -127,7 +128,7 @@ def SendMessages(self, messages : list[str], temperature : float = None):
# Return the response if the API call succeeds
return translation

except openai.error.RateLimitError as e:
except openai.RateLimitError as e:
retry_after = e.headers.get('x-ratelimit-reset-requests') or e.headers.get('Retry-After')
if retry_after:
retry_seconds = ParseDelayFromHeader(retry_after)
Expand All @@ -138,8 +139,8 @@ def SendMessages(self, messages : list[str], temperature : float = None):
logging.warning("Rate limit hit, quota exceeded. Please wait until the quota resets.")
raise

except (openai.error.APIConnectionError, openai.error.Timeout, openai.error.ServiceUnavailableError) as e:
if isinstance(e, openai.error.APIConnectionError) and not e.should_retry:
except (openai.APIConnectionError, openai.APITimeoutError) as e:
if isinstance(e, openai.APIConnectionError) and not e.should_retry:
raise TranslationImpossibleError(str(e), translation)
if retries == max_retries:
logging.warning(f"OpenAI failure {str(e)}, aborting after {retries} retries...")
Expand Down
16 changes: 9 additions & 7 deletions PySubtitleGPT/SubtitleTranslator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ def __init__(self, subtitles : SubtitleFile, options : Options):
raise ValueError('API key must be set in .env or provided as an argument')

if options.api_base():
openai.api_base = options.api_base()
openai.base_url = options.api_base()

logging.debug(f"Using API Key: {openai.api_key}, Using API Base: {openai.api_base}")
logging.debug(f"Using API Key: {openai.api_key}, Using API Base: {openai.base_url}")

self.subtitles = subtitles
self.options = options
Expand All @@ -59,14 +59,16 @@ def GetAvailableModels(cls, api_key : str, api_base : str):
Returns a list of possible values for the LLM model
"""
try:
if api_base:
response = openai.Model.list(api_key, api_base = api_base) if api_key else None
else:
response = openai.Model.list(api_key) if api_key else None
client = openai.OpenAI(
api_key=api_key,
base_url=api_base
)
response = client.models.list()

if not response or not response.data:
return []

model_list = [model.openai_id for model in response.data if model.openai_id.startswith('gpt') and model.openai_id.find('instruct') < 0]
model_list = [model.id for model in response.data if model.id.startswith('gpt') and model.id.find('instruct') < 0 and model.id.find('vision') < 0]

return sorted(model_list)

Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
python-dotenv
openai
openai>=1.1.0
srt
regex
events
requests
pyside6
darkdetect
appdirs
Expand Down
17 changes: 8 additions & 9 deletions theme/subtrans-dark.qss
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@ QTextEdit {
}

QComboBox {
background-color: white;
color: #363636;
}

QComboBox::drop-down {
border: none;
color: white;
background-color: #363636;
background-color: white;
color: #363636;
}

QComboBox::drop-down QAbstractItemView {
background-color: #363636;
color: white;
background-color: white;
color: #363636;
}

.MainWindow, QLabel, QDialog {
Expand Down Expand Up @@ -71,7 +72,7 @@ QTreeView:item:selected {
}

.WidgetHeader {
font-size: 12pt;
font-size:large;
background-color: blanchedalmond;
padding: 4;
padding-left: 10;
Expand All @@ -91,15 +92,15 @@ QTreeView:item:selected {
}

.WidgetSubheading {
font-size: 11pt;
font-size:medium;
padding: 3;
padding-left: 10;
background-color: #363636;
color: lavenderblush;
}

.WidgetBody {
font-size: 10pt;
font-size: small;
padding: 2;
padding-left: 10;
background-color: #242424;
Expand Down Expand Up @@ -127,13 +128,11 @@ QTreeView:item:selected {

.LineItemHeader {
font-weight: bold;
font-size: 10pt;
margin: 0px;
padding: 2px;
}

.LineItemBody {
font-size: 10pt;
margin: 0px;
padding: 0px;
}
Expand Down

0 comments on commit 96b9d4e

Please sign in to comment.