-
-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Flask_server + use now_dir insted of ".."
- Loading branch information
Showing
13 changed files
with
146 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,6 @@ | |
"override": false, | ||
"selected_lang": "en_US" | ||
}, | ||
"flask_server": true, | ||
"version": "3.0.6" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,4 @@ if not exist env ( | |
|
||
env\python.exe app.py --open | ||
echo. | ||
pause | ||
pause |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters