diff --git a/README-Docker.md b/README-Docker.md index df5b530..b6fbd68 100644 --- a/README-Docker.md +++ b/README-Docker.md @@ -4,6 +4,7 @@ First make a .env file with the API keys like this: ```bash +GEMINI_API_KEY=put_your_gemini_api_key_here OPENAI_API_KEY=sk-_put_your_openai_api_key_here ELEVENLABS_API_KEY=put_your_eleven_labs_api_key_here PEXELS_API_KEY=put_your_pexels_api_key_here diff --git a/gui/ui_tab_config.py b/gui/ui_tab_config.py index 4b45612..f7cc984 100644 --- a/gui/ui_tab_config.py +++ b/gui/ui_tab_config.py @@ -31,7 +31,7 @@ def verify_eleven_key(self, eleven_key, remaining_chars): raise gr.Error(e.args[0]) return remaining_chars - def save_keys(self, openai_key, eleven_key, pexels_key): + def save_keys(self, openai_key, eleven_key, pexels_key, gemini_key): '''Save the keys in the database''' if (self.api_key_manager.get_api_key("OPENAI_API_KEY") != openai_key): self.api_key_manager.set_api_key("OPENAI_API_KEY", openai_key) @@ -43,12 +43,16 @@ def save_keys(self, openai_key, eleven_key, pexels_key): return gr.update(value=openai_key),\ gr.update(value=eleven_key),\ gr.update(value=pexels_key),\ + gr.update(value=gemini_key),\ gr.update(choices=new_eleven_voices),\ gr.update(choices=new_eleven_voices) + if (self.api_key_manager.get_api_key("GEMINI_API_KEY") != gemini_key): + self.api_key_manager.set_api_key("GEMINI_API_KEY", gemini_key) return gr.update(value=openai_key),\ gr.update(value=eleven_key),\ gr.update(value=pexels_key),\ + gr.update(value=gemini_key),\ gr.update(visible=True),\ gr.update(visible=True) @@ -84,9 +88,14 @@ def create_ui(self): pexels_textbox = gr.Textbox(value=self.api_key_manager.get_api_key("PEXELS_API_KEY"), label=f"PEXELS KEY", show_label=True, interactive=True, show_copy_button=True, type="password", scale=40) show_pexels_key = gr.Button("Show", size="sm", scale=1) show_pexels_key.click(self.on_show, [show_pexels_key], [pexels_textbox, show_pexels_key]) + with gr.Row(): + gemini_textbox = gr.Textbox(value=self.api_key_manager.get_api_key("GEMINI_API_KEY"), label=f"GEMINI API KEY", show_label=True, interactive=True, show_copy_button=True, type="password", scale=40) + show_gemini_key = gr.Button("Show", size="sm", scale=1) + show_gemini_key.click(self.on_show, [show_gemini_key], [gemini_textbox, show_gemini_key]) + save_button = gr.Button("save", size="sm", scale=1) save_button.click(self.verify_eleven_key, [eleven_labs_textbox, eleven_characters_remaining], [eleven_characters_remaining]).success( - self.save_keys, [openai_textbox, eleven_labs_textbox, pexels_textbox], [openai_textbox, eleven_labs_textbox, pexels_textbox, AssetComponentsUtils.voiceChoice(), AssetComponentsUtils.voiceChoiceTranslation()]) + self.save_keys, [openai_textbox, eleven_labs_textbox, pexels_textbox, gemini_textbox], [openai_textbox, eleven_labs_textbox, pexels_textbox, gemini_textbox, AssetComponentsUtils.voiceChoice(), AssetComponentsUtils.voiceChoiceTranslation()]) save_button.click(lambda _: gr.update(value="Keys Saved !"), [], [save_button]) save_button.click(self.back_to_normal, [], [save_button]) - return config_ui + return config_ui \ No newline at end of file diff --git a/gui/ui_tab_short_automation.py b/gui/ui_tab_short_automation.py index 5ab7cb8..beef8ea 100644 --- a/gui/ui_tab_short_automation.py +++ b/gui/ui_tab_short_automation.py @@ -144,8 +144,9 @@ def inspect_create_inputs(self, background_video_list, background_music_list, wa raise gr.Error("Watermark should be at least 3 characters long.") openai_key = ApiKeyManager.get_api_key("OPENAI_API_KEY") - if not openai_key: - raise gr.Error("OPENAI API key is missing. Please go to the config tab and enter the API key.") + gemini_key = ApiKeyManager.get_api_key("GEMINI_API_KEY") + if not openai_key and not gemini_key: + raise gr.Error("GEMINI OR OPENAI API key is missing. Please go to the config tab and enter the API key.") eleven_labs_key = ApiKeyManager.get_api_key("ELEVENLABS_API_KEY") if self.tts_engine == AssetComponentsUtils.ELEVEN_TTS and not eleven_labs_key: raise gr.Error("ELEVENLABS_API_KEY API key is missing. Please go to the config tab and enter the API key.") diff --git a/gui/ui_tab_video_automation.py b/gui/ui_tab_video_automation.py index e23ce99..6bff7a3 100644 --- a/gui/ui_tab_video_automation.py +++ b/gui/ui_tab_video_automation.py @@ -49,8 +49,9 @@ def __init__(self, shortGptUI: gr.Blocks): def is_key_missing(self): openai_key = ApiKeyManager.get_api_key("OPENAI_API_KEY") - if not openai_key: - return "Your OpenAI key is missing. Please go to the config tab and enter the API key." + gemini_key = ApiKeyManager.get_api_key("GEMINI_API_KEY") + if not openai_key and not gemini_key: + return "Your Genmini or OpenAI key is missing. Please go to the config tab and enter the API key." pexels_api_key = ApiKeyManager.get_api_key("PEXELS_API_KEY") if not pexels_api_key: diff --git a/installation-notes.md b/installation-notes.md index b82784f..d9116f6 100644 --- a/installation-notes.md +++ b/installation-notes.md @@ -8,6 +8,7 @@ First make a .env file with the API keys like this: ```bash +GEMINI_API_KEY=put_your_gemini_api_key_here OPENAI_API_KEY=sk-_put_your_openai_api_key_here ELEVENLABS_API_KEY=put_your_eleven_labs_api_key_here PEXELS_API_KEY=put_your_pexels_api_key_here diff --git a/shortGPT/config/api_db.py b/shortGPT/config/api_db.py index 05da149..7dbd10a 100644 --- a/shortGPT/config/api_db.py +++ b/shortGPT/config/api_db.py @@ -5,6 +5,7 @@ load_dotenv('./.env') class ApiProvider(enum.Enum): OPENAI = "OPENAI_API_KEY" + GEMINI = "GEMINI_API_KEY" ELEVEN_LABS = "ELEVENLABS_API_KEY" PEXELS = "PEXELS_API_KEY" diff --git a/shortGPT/gpt/gpt_utils.py b/shortGPT/gpt/gpt_utils.py index f15d611..7b89978 100644 --- a/shortGPT/gpt/gpt_utils.py +++ b/shortGPT/gpt/gpt_utils.py @@ -10,13 +10,13 @@ from shortGPT.config.api_db import ApiKeyManager -def num_tokens_from_messages(texts, model="gpt-3.5-turbo-0301"): +def num_tokens_from_messages(texts, model="gpt-4o-mini"): """Returns the number of tokens used by a list of messages.""" try: encoding = tiktoken.encoding_for_model(model) except KeyError: encoding = tiktoken.get_encoding("cl100k_base") - if model == "gpt-3.5-turbo-0301": # note: future models may deviate from this + if model == "gpt-4o-mini": # note: future models may deviate from this if isinstance(texts, str): texts = [texts] score = 0 @@ -67,10 +67,22 @@ def load_local_yaml_prompt(file_path): def open_file(filepath): with open(filepath, 'r', encoding='utf-8') as infile: return infile.read() - - -def llm_completion(chat_prompt="", system="", temp=0.7, model="gpt-4o-mini", max_tokens=2000, remove_nl=True, conversation=None): - openai.api_key = ApiKeyManager.get_api_key("OPENAI_API_KEY") +from openai import OpenAI + +def llm_completion(chat_prompt="", system="", temp=0.7, max_tokens=2000, remove_nl=True, conversation=None): + openai_key= ApiKeyManager.get_api_key("OPENAI_API_KEY") + gemini_key = ApiKeyManager.get_api_key("GEMINI_API_KEY") + if gemini_key: + client = OpenAI( + api_key=gemini_key, + base_url="https://generativelanguage.googleapis.com/v1beta/openai/" + ) + model="gemini-2.0-flash-lite-preview-02-05" + elif openai_key: + client = OpenAI( api_key=openai_key) + model="gpt-4o-mini" + else: + raise Exception("No OpenAI or Gemini API Key found for LLM request") max_retry = 5 retry = 0 error = "" @@ -83,7 +95,7 @@ def llm_completion(chat_prompt="", system="", temp=0.7, model="gpt-4o-mini", max {"role": "system", "content": system}, {"role": "user", "content": chat_prompt} ] - response = openai.chat.completions.create( + response = client.chat.completions.create( model=model, messages=messages, max_tokens=max_tokens,