Skip to content

Commit

Permalink
changes to how the webgui performs on hugging face
Browse files Browse the repository at this point in the history
  • Loading branch information
Bikatr7 committed May 27, 2024
1 parent 284784b commit 56a3cb6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
10 changes: 7 additions & 3 deletions modules/common/file_ensurer.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class FileEnsurer():
openai_api_key_path = os.path.join(secrets_dir,'openai_api_key.txt')
gemini_api_key_path = os.path.join(secrets_dir,'gemini_api_key.txt')
google_translate_service_key_json_path = os.path.join(secrets_dir, "google_translate_service_key.json")

## temp files
temp_file_path = os.path.join(script_dir, "temp.txt")

## favicon
favicon_path = os.path.join(gui_lib, "Kudasai_Logo.png")
Expand Down Expand Up @@ -219,9 +222,7 @@ def purge_storage() -> None:
logging.debug("Running on Hugging Face, purging storage")

stuff_to_purge = [
FileEnsurer.secrets_dir,
FileEnsurer.config_dir,
FileEnsurer.archive_dir
FileEnsurer.temp_file_path
]

stuff_to_truncate = [
Expand Down Expand Up @@ -453,6 +454,9 @@ def archive_results(list_of_result_tuples:typing.List[typing.Tuple[str,str]], mo
"""

if(FileEnsurer.is_hugging_space()):
return

archival_path = os.path.join(FileEnsurer.archive_dir, f'{module}_run_{timestamp}')
FileEnsurer.standard_create_directory(archival_path)

Expand Down
24 changes: 21 additions & 3 deletions webgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import typing
import base64
import asyncio
import os

## third-party libraries
import gradio as gr
Expand Down Expand Up @@ -137,6 +138,9 @@ def get_saved_api_key(service_name:typing.Literal["openai","gemini","deepl","goo
"""

if(FileEnsurer.is_hugging_space()):
return ""

service_to_path = {
"openai": FileEnsurer.openai_api_key_path,
"gemini": FileEnsurer.gemini_api_key_path,
Expand Down Expand Up @@ -169,10 +173,14 @@ async def set_translator_api_key(api_key) -> None:
"""

## ik this looks really bad
## but the set_credentials for google translate expects a filepath, and normal way isn't working
## so we write the client secrets to a temp file, send that, then clear the file
## even if it fails, purge_storage is set to kill that path too so we should be good.
if(Translator.TRANSLATION_METHOD == "google translate"):
FileEnsurer.standard_overwrite_file(FileEnsurer.google_translate_service_key_json_path, str(api_key), omit=True)
await asyncio.sleep(2)
api_key = FileEnsurer.google_translate_service_key_json_path
FileEnsurer.standard_overwrite_file(FileEnsurer.temp_file_path, str(api_key), omit=True)
await asyncio.sleep(1)
api_key = FileEnsurer.temp_file_path

try:

Expand All @@ -186,6 +194,13 @@ async def set_translator_api_key(api_key) -> None:
except:
raise gr.Error("Invalid API key")

finally:
try:
os.remove(FileEnsurer.temp_file_path)

except:
pass

##-------------------start-of-update_translator_api_key()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

def update_translator_api_key(api_key) -> None:
Expand All @@ -199,6 +214,9 @@ def update_translator_api_key(api_key) -> None:
"""

if(FileEnsurer.is_hugging_space()):
return

method_to_path = {
"openai": FileEnsurer.openai_api_key_path,
"gemini": FileEnsurer.gemini_api_key_path,
Expand Down

0 comments on commit 56a3cb6

Please sign in to comment.