Skip to content

Commit

Permalink
Add plugins support
Browse files Browse the repository at this point in the history
  • Loading branch information
aitronz committed Feb 5, 2024
1 parent 4e5b6aa commit 923a4fa
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def __init__(self, message="InstallationError"):
from tabs.tts.tts import tts_tab
from tabs.settings.presence import presence_tab
from tabs.settings.themes import theme_tab
from tabs.plugins.plugins import plugins_tab

import assets.themes.loadThemes as loadThemes

Expand Down Expand Up @@ -89,6 +90,9 @@ def __init__(self, message="InstallationError"):
with gr.Tab(i18n("Download")):
download_tab()

with gr.Tab(i18n("Plugins")):
plugins_tab()

with gr.Tab(i18n("Report a Bug")):
report_tab()

Expand Down
41 changes: 41 additions & 0 deletions tabs/plugins/plugins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os, sys
import gradio as gr
import importlib.util
import tabs.plugins.plugins_core as plugins_core

from assets.i18n.i18n import I18nAuto

i18n = I18nAuto()

now_dir = os.getcwd()
sys.path.append(now_dir)

plugins_core.check_new_folders()

def plugins_tab():
gr.Markdown(
value=i18n(
"This section contains some extra utilities that often may be in experimental phases."
)
)

with gr.TabItem(i18n("Install Plugins")):
dropbox = gr.File(
label=i18n(
"Drag your plugin.zip to install it."
),
type="filepath",
)

dropbox.upload(
fn=plugins_core.save_plugin_dropbox,
inputs=[dropbox],
outputs=[dropbox],
)

for plugin in os.listdir(os.path.join(now_dir, "tabs", "plugins", "installed")):
plugin_main = f"tabs.plugins.installed.{plugin}.plugin"
plugin_import = importlib.import_module(plugin_main)

with gr.TabItem(plugin):
plugin_import.applio_plugin()
74 changes: 74 additions & 0 deletions tabs/plugins/plugins_core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import os, sys, shutil
import json
import gradio as gr
import zipfile
import subprocess

from assets.i18n.i18n import I18nAuto

i18n = I18nAuto()

now_dir = os.getcwd()
sys.path.append(now_dir)

plugins_path = os.path.join(now_dir, "tabs", "plugins", "installed")
json_file_path = os.path.join(now_dir, "tabs", "plugins", "installed_list.json")
current_folders = os.listdir(plugins_path)

def get_existing_folders():
if os.path.exists(json_file_path):
with open(json_file_path, 'r') as file:
return json.load(file)
else:
return []

def save_existing_folders(existing_folders):
with open(json_file_path, 'w') as file:
json.dump(existing_folders, file)

def save_plugin_dropbox(dropbox):
if "zip" not in dropbox:
raise gr.Error(
message="The file you dropped is not a valid plugin.zip. Please try again."
)
else:
file_name = (os.path.basename(dropbox))
folder_name = (file_name.split(".zip")[0])
folder_path = os.path.join(plugins_path, folder_name)
zip_file_path = os.path.join(plugins_path, file_name)

if os.path.exists(folder_name):
os.remove(folder_name)

shutil.move(dropbox, os.path.join(plugins_path, file_name))
print("Proceeding with the extraction...")

with zipfile.ZipFile(zip_file_path, "r") as zip_ref:
zip_ref.extractall(plugins_path)
os.remove(zip_file_path)

if os.path.exists(os.path.join(folder_path, "requirements.txt")):
subprocess.run([os.path.join("env", "python.exe"), "-m", "pip", "install", "-r", os.path.join(folder_path, "requirements.txt")])
else:
print("No requirements.txt file found in the plugin folder.")

save_existing_folders(get_existing_folders() + [folder_name])

print(f"{folder_name} plugin installed in {plugins_path}! Restart applio to see the changes.")
gr.Info(f"{folder_name} plugin installed in {plugins_path}! Restart applio to see the changes.")
return None

def check_new_folders():
existing_folders = get_existing_folders()
new_folders = set(current_folders) - set(existing_folders)
if new_folders:
for new_folder in new_folders:
complete_path = os.path.join(plugins_path, new_folder)
print(f"New Plugin {new_folder} found! Installing...")

if os.path.exists(os.path.join(complete_path, "requirements.txt")):
subprocess.run([os.path.join("env", "python.exe"), "-m", "pip", "install", "-r", os.path.join(complete_path, "requirements.txt")])
else:
print("No requirements.txt file found in the plugin folder.")
print("Plugins checked and installed! Restart applio to see the changes.")
save_existing_folders(current_folders)

0 comments on commit 923a4fa

Please sign in to comment.