Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to python server #442

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
12 changes: 5 additions & 7 deletions deprecated-do-not-use-start_server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@ if ! hash python; then
fi

# Check the default python version
orig_ver=$(python -V 2>&1)
orig_ver=$(python --version 2>&1)
major_version=$(echo "$orig_ver" | sed 's/[^0-9]*\([0-9]*\)\..*/\1/')
minor_subversion=$(echo "$orig_ver" | sed -E 's/^[^.]*\.([^.]*).*$/\1/')
if [[ "$major_version" -ge "3" ]] && [[ "$minor_subversion" -ge "7" ]] # Because of uvicorn==0.20.0 in requirements
then
echo "You have valid version of $orig_ver"
if [ "$major_version" -ge "3" ] && [ "$minor_subversion" -ge "7" ]; then
echo "You have a valid version of $orig_ver"
else
echo "Your version $orig_ver not valid, should be >=3.7"
exit 1
echo "Your version $orig_ver is not valid, should be >=3.7"
exit 1
fi


# Check if the desired environment exists
if [ ! -d "server_env" ]; then
# Create the environment if it doesn't exist
Expand Down
32 changes: 6 additions & 26 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
anyio==3.6.2
asyncio==3.4.3
certifi==2022.12.7
charset-normalizer==2.1.1
click==8.1.7
fastapi==0.88.0
h11==0.14.0
httpcore==0.16.3
httptools==0.5.0
httpx==0.25.1
idna==3.4
Pillow==9.3.0
pydantic==1.10.2
python-dotenv==0.21.0
PyYAML==6.0
requests==2.28.1
rfc3986==1.5.0
sniffio==1.3.0
starlette==0.22.0
typing_extensions==4.4.0
urllib3==1.26.13
uvicorn==0.20.0
# uvloop==0.17.0
watchfiles==0.18.1
websockets==10.4
duckduckgo_search==3.9.9
duckduckgo_search==4.1.1
fastapi==0.105.0
httpx==0.26.0
Pillow==10.1.0
uvicorn==0.25.0

95 changes: 44 additions & 51 deletions scripts/main.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
from modules import scripts, processing, shared, images, devices, ui, lowvram
import gradio
import requests
import time
import PIL.Image
import base64
import io
import os.path
import numpy
import itertools
import sys

import gradio as gr
import torch
from fastapi import FastAPI
from fastapi import APIRouter, Request
from fastapi.responses import StreamingResponse
from fastapi import APIRouter, FastAPI, Request
from modules import script_callbacks, scripts, shared


import sys
python_server_dir = 'server/python_server'
python_server_dir = "server/python_server"
extension_dir = scripts.basedir()
python_server_full_path = os.path.join(extension_dir,python_server_dir)
print("python_server_full_path: ",python_server_full_path)
python_server_full_path = os.path.join(extension_dir, python_server_dir)
print("python_server_full_path: ", python_server_full_path)
sys.path.insert(0, python_server_full_path)
import search
import img2imgapi
import search
import serverMain

router = APIRouter()
Expand All @@ -34,73 +22,78 @@
# res = "hello get /config auto-photoshop-sd"
# return {"res": res}

@router.post('/search/image/')
async def searchImage(request:Request):

@router.post("/search/image/")
async def searchImage(request: Request):
try:
json = await request.json()
except:
except:
json = {}


try:
keywords = json.get('keywords','cute cats')
keywords = json.get("keywords", "cute cats")
images = await search.imageSearch(keywords)
print(images)


return {"images":images}

return {"images": images}
except:
print("keywords",keywords)
print("keywords", keywords)
# print(f'{request}')
return {"error": "error message: can't preform an image search"}


@router.post('/mask/expansion/')
async def maskExpansionHandler(request:Request):
@router.post("/mask/expansion/")
async def maskExpansionHandler(request: Request):
try:
json = await request.json()
except:
except:
json = {}

# print("mask expansion json :",json)
try:
# keywords = json.get('keywords','cute dogs')
base64_mask_image = json['mask']
mask_expansion = json['mask_expansion']
blur = json['blur']
#convert base64 to img

await img2imgapi.base64ToPng(base64_mask_image,"original_mask.png")#save a copy of the mask
# keywords = json.get('keywords','cute dogs')
base64_mask_image = json["mask"]
mask_expansion = json["mask_expansion"]
blur = json["blur"]
# convert base64 to img

await img2imgapi.base64ToPng(
base64_mask_image, "original_mask.png"
) # save a copy of the mask

mask_image = img2imgapi.b64_2_img(base64_mask_image)
expanded_mask_img = img2imgapi.maskExpansion(mask_image,mask_expansion,blur)

expanded_mask_img = img2imgapi.maskExpansion(mask_image, mask_expansion, blur)
base64_expanded_mask_image = img2imgapi.img_2_b64(expanded_mask_img)
await img2imgapi.base64ToPng(base64_expanded_mask_image,"expanded_mask.png")#save a copy of the mask
await img2imgapi.base64ToPng(
base64_expanded_mask_image, "expanded_mask.png"
) # save a copy of the mask

return {"mask": base64_expanded_mask_image}

return {"mask":base64_expanded_mask_image}

except:
# print("request",request)
raise Exception(f"couldn't preform mask expansion",json)
raise Exception(f"couldn't preform mask expansion", json)
# return response
return {"error": "error message: can't preform an mask expansion"}



def on_app_started(demo: gr.Blocks, app: FastAPI):
# print("hello on_app_started auto-photoshop-plugin")

if shared.cmd_opts.api:
app.include_router(serverMain.router, prefix="/sdapi/auto-photoshop-sd", tags=['Auto Photoshop SD Plugin API'])
app.include_router(
serverMain.router,
prefix="/sdapi/auto-photoshop-sd",
tags=["Auto Photoshop SD Plugin API"],
)
# app.include_router(router, prefix="/sdapi/auto-photoshop-sd", tags=['Auto Photoshop SD Plugin API'])


else:
print("COMMANDLINE_ARGS does not contain --api, API won't be mounted.")

# logger.warning("COMMANDLINE_ARGS does not contain --api, API won't be mounted.")
# if you wanted to do anything massive to the UI, you could modify demo, but why?

script_callbacks.on_app_started(on_app_started)

script_callbacks.on_app_started(on_app_started)
48 changes: 27 additions & 21 deletions server/python_server/global_state.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#code copied from controlnet repo global_state.py
# code copied from controlnet repo global_state.py


preprocessor_filters = {
Expand All @@ -20,7 +20,8 @@
"T2IA": "none",
}

cn_preprocessor_modules = ["none",
cn_preprocessor_modules = [
"none",
"canny",
"depth",
"depth_leres",
Expand Down Expand Up @@ -87,24 +88,26 @@
"inpaint": "inpaint_global_harmonious",
}

def filter_selected_helper(k,preprocessor_list,model_list):
if 'None' not in model_list:
model_list = ['None'] + model_list
ui_preprocessor_keys = ['none', preprocessor_aliases['invert']]


ui_preprocessor_keys += sorted([preprocessor_aliases.get(k, k)
for k in preprocessor_list
if preprocessor_aliases.get(k, k) not in ui_preprocessor_keys])

def filter_selected_helper(k, preprocessor_list, model_list):
if "None" not in model_list:
model_list = ["None"] + model_list
ui_preprocessor_keys = ["none", preprocessor_aliases["invert"]]

ui_preprocessor_keys += sorted(
[
preprocessor_aliases.get(k, k)
for k in preprocessor_list
if preprocessor_aliases.get(k, k) not in ui_preprocessor_keys
]
)

preprocessor_list = ui_preprocessor_keys
# print("preprocessor_list sorted: ",preprocessor_list)
model_list = list(model_list)
# print("list(model_list): ",model_list)

# print("k:",k,k.lower())


default_option = preprocessor_filters[k]
pattern = k.lower()
Expand All @@ -113,13 +116,11 @@ def filter_selected_helper(k,preprocessor_list,model_list):
return [
preprocessor_list,
model_list,
'none', #default option
"None" #default model
]
"none", # default option
"None", # default model
]
filtered_preprocessor_list = [
x
for x in preprocessor_list
if pattern in x.lower() or x.lower() == "none"
x for x in preprocessor_list if pattern in x.lower() or x.lower() == "none"
]
if pattern in ["canny", "lineart", "scribble", "mlsd"]:
filtered_preprocessor_list += [
Expand All @@ -135,7 +136,7 @@ def filter_selected_helper(k,preprocessor_list,model_list):
# else:
# print("pattern:",pattern, "not in model.lower():",model.lower())
##Debug end

filtered_model_list = [
x for x in model_list if pattern in x.lower() or x.lower() == "none"
]
Expand All @@ -150,5 +151,10 @@ def filter_selected_helper(k,preprocessor_list,model_list):
if "11" in x.split("[")[0]:
default_model = x
break

return [filtered_preprocessor_list,filtered_model_list, default_option,default_model]

return [
filtered_preprocessor_list,
filtered_model_list,
default_option,
default_model,
]
Loading