Skip to content

Commit

Permalink
Flask_server + use now_dir insted of ".."
Browse files Browse the repository at this point in the history
  • Loading branch information
aitronz committed Feb 11, 2024
1 parent 831ff37 commit 7d5a84b
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 15 deletions.
12 changes: 9 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,30 @@
from tabs.report.report import report_tab
from tabs.download.download import download_tab
from tabs.tts.tts import tts_tab
from tabs.settings.presence import presence_tab, load_config
from tabs.settings.presence import presence_tab, load_config_presence
from tabs.settings.flask_server import flask_server_tab
from tabs.settings.themes import theme_tab
from tabs.plugins.plugins import plugins_tab
from tabs.settings.version import version_tab
from tabs.settings.restart import restart_tab
from tabs.settings.lang import lang_tab
from tabs.settings.restart import restart_tab

# Assets
import assets.themes.loadThemes as loadThemes
from assets.i18n.i18n import I18nAuto
import assets.installation_checker as installation_checker
from assets.discord_presence import RPCManager
from assets.flask.server import start_flask, load_config_flask

i18n = I18nAuto()
if load_config == True:
if load_config_presence() == True:
RPCManager.start_presence()
installation_checker.check_installation()
logging.getLogger("uvicorn").disabled = True
logging.getLogger("fairseq").disabled = True
if load_config_flask() == True:
print("Starting Flask server")
start_flask()

my_applio = loadThemes.load_json()
if my_applio:
Expand Down Expand Up @@ -74,6 +79,7 @@

with gr.Tab(i18n("Settings")):
presence_tab()
flask_server_tab()
theme_tab()
version_tab()
lang_tab()
Expand Down
1 change: 1 addition & 0 deletions assets/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"override": false,
"selected_lang": "en_US"
},
"flask_server": true,
"version": "3.0.6"
}
29 changes: 29 additions & 0 deletions assets/flask/routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os, sys
import signal
from flask import Flask, request, redirect

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

from core import run_download_script

app = Flask(__name__)

@app.route('/download/<path:url>', methods=['GET'])
def download(url):
file_path = run_download_script(url)
if file_path == "Model downloaded successfully.":
if 'text/html' in request.headers.get('Accept', ''):
return redirect("https://applio.org", code=302)
else:
return ""
else:
return "Error: Unable to download file", 500

@app.route('/shutdown', methods=['POST'])
def shutdown():
print("This Flask server is shutting down... Please close the window!")
os.kill(os.getpid(), signal.SIGTERM)

if __name__ == '__main__':
app.run(host='localhost', port=8000)
55 changes: 55 additions & 0 deletions assets/flask/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import os
import socket
import subprocess
import time
import requests
import sys
import json

now_dir = os.getcwd()
sys.path.append(now_dir)
config_file = os.path.join(now_dir, "assets", "config.json")
env_path = os.path.join(now_dir, "env", "python.exe")

host = "localhost"
port = 8000

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(2)

def start_flask():
try:
sock.connect((host, port))
print(
f"Something is listening on port {port}; check open connection and restart Applio."
)
print("Trying to start it anyway")
sock.close()
requests.post("http://localhost:8000/shutdown")
time.sleep(3)
script_path = os.path.join(now_dir, "assets", "flask", "routes.py")
try:
subprocess.Popen([env_path, script_path], creationflags=subprocess.CREATE_NEW_CONSOLE)
except Exception as e:
print(f"Failed to start the Flask server")
print(e)
except Exception as e:
sock.close()
script_path = os.path.join(now_dir, "assets", "flask", "routes.py")
try:
subprocess.Popen([env_path, script_path], creationflags=subprocess.CREATE_NEW_CONSOLE)
except Exception as e:
print("Failed to start the Flask server")
print(e)

def load_config_flask():
with open(config_file, "r") as file:
config = json.load(file)
return config["flask_server"]

def save_config(value):
with open(config_file, "r") as file:
config = json.load(file)
config["flask_server"] = value
with open(config_file, "w") as file:
json.dump(config, file, indent=2)
1 change: 1 addition & 0 deletions assets/i18n/languages/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
"Output Path for RVC Audio": "Output Path for RVC Audio",

"Enable Applio integration with Discord presence": "Enable Applio integration with Discord presence",
"Enable Applio integration with applio.org/models using flask": "Enable Applio integration with applio.org/models using flask",
"Theme (Restart required)": "Theme (Restart required)",

"Plugin Installer": "Plugin Installer",
Expand Down
1 change: 1 addition & 0 deletions assets/i18n/languages/es_ES.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"Output Path for TTS Audio": "Ruta de salida para el audio TTS",
"Output Path for RVC Audio": "Ruta de salida para el audio RVC",
"Enable Applio integration with Discord presence": "Habilita la integración de Applio con la presencia de Discord",
"Enable Applio integration with applio.org/models using flask": "Habilita la integración de Applio con applio.org/models usando flask",
"Theme (Restart required)": "Tema (Reinicio requerido)",
"Plugin Installer": "Instalador de complementos",
"Drag your plugin.zip to install it": "Arrastra tu plugin.zip para instalarlo",
Expand Down
2 changes: 1 addition & 1 deletion run-applio.bat
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ if not exist env (

env\python.exe app.py --open
echo.
pause
pause
7 changes: 3 additions & 4 deletions tabs/extra/processing/processing.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import sys

sys.path.append("..")
import os
import os, sys

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

from rvc.train.process_ckpt import (
extract_small_model,
)
Expand Down
2 changes: 1 addition & 1 deletion tabs/report/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from assets.i18n.i18n import I18nAuto

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

i18n = I18nAuto()

Expand Down
37 changes: 37 additions & 0 deletions tabs/settings/flask_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os
import sys
import gradio as gr
from assets.i18n.i18n import I18nAuto
import requests

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

from assets.flask.server import start_flask, load_config_flask, save_config

i18n = I18nAuto()

def flask_server_tab():
with gr.Row():
with gr.Column():
flask_checkbox = gr.Checkbox(
label=i18n("Enable Applio integration with applio.org/models using flask"),
interactive=True,
value=load_config_flask(),
)
flask_checkbox.change(
fn=toggle,
inputs=[flask_checkbox],
outputs=[],
)


def toggle(checkbox):
save_config(bool(checkbox))
if load_config_flask() == True:
start_flask()
else:
try:
requests.post("http://localhost:8000/shutdown")
except requests.exceptions.ConnectionError:
pass
8 changes: 4 additions & 4 deletions tabs/settings/presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
from assets.discord_presence import RPCManager

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

i18n = I18nAuto()
config_file = os.path.join(now_dir, "assets", "config.json")


def load_config():
def load_config_presence():
with open(config_file, "r") as file:
config = json.load(file)
return config["discord_presence"]
Expand All @@ -32,7 +32,7 @@ def presence_tab():
presence = gr.Checkbox(
label=i18n("Enable Applio integration with Discord presence"),
interactive=True,
value=load_config(),
value=load_config_presence(),
)
presence.change(
fn=toggle,
Expand All @@ -43,7 +43,7 @@ def presence_tab():

def toggle(checkbox):
save_config(bool(checkbox))
if load_config() == True:
if load_config_presence() == True:
try:
RPCManager.start_presence()
except KeyboardInterrupt:
Expand Down
4 changes: 3 additions & 1 deletion tabs/settings/restart.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@


def restart_applio():
if os.name == "nt":
if os.name != "nt":
os.system("clear")
else:
os.system("cls")
python = sys.executable
os.execl(python, python, *sys.argv)
Expand Down
2 changes: 1 addition & 1 deletion tabs/settings/themes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import assets.themes.loadThemes as loadThemes

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

i18n = I18nAuto()

Expand Down

0 comments on commit 7d5a84b

Please sign in to comment.