i am getting this code to perfectly in VSCODE but when i use this code in pythonanywhere it is always having some issues.
I resolved it with using sys.path and it worked for me.
but when the flask code is run, and get adn post request are sent at that time i am getting some tensorflow error.
Steps to reproduce.
https://github.com/experience-ml/cartoonize.git
cd cartoonize
pip install -r requirement.txt
flask code shall run but then there are directory error for it. and i tried the minified the code and used absolute path but then now i am getting some tensorflow libraries to not work.
this is the error i am getting
2024-02-05 05:50:33 Python main interpreter initialized at 0x5629901dee50
2024-02-05 05:50:33 your server socket listen backlog is limited to 100 connections 2024-02-05 05:50:33 your mercy for graceful operations on workers is 60 seconds 2024-02-05 05:50:33 setting request body buffering size to 65536 bytes 2024-02-05 05:50:33 mapped 334256 bytes (326 KB) for 1 cores 2024-02-05 05:50:33 *** Operational MODE: single process *** 2024-02-05 05:50:33 initialized 38 metrics 2024-02-05 05:50:33 2024-02-05 05:50:25.029450: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory 2024-02-05 05:50:33 2024-02-05 05:50:25.029470: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine. 2024-02-05 05:50:33 111111111111111111111 /home/shahhtejas/mysite/config.yaml 2024-02-05 05:50:33 /usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer_v1.py:1694: UserWarning: `layer.apply` is deprecated and will be removed in a future version. Please use `layer.__call__` method instead.#012 warnings.warn('`layer.apply` is deprecated and ' 2024-02-05 05:50:33 2024-02-05 05:50:33.328003: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 AVX512F FMA#012To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. 2024-02-05 05:50:33 2024-02-05 05:50:33.336705: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/lib/python3.7/site-packages/cv2/../../lib64: 2024-02-05 05:50:33 2024-02-05 05:50:33.336722: W tensorflow/stream_executor/cuda/cuda_driver.cc:269] failed call to cuInit: UNKNOWN ERROR (303) 2024-02-05 05:50:33 2024-02-05 05:50:33.336742: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (green-liveweb3): /proc/driver/nvidia/version does not exist 2024-02-05 05:50:33 2024-02-05 05:50:33.356572: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:354] MLIR V1 optimization pass is not enabled 2024-02-05 05:50:33 Weights successfully loaded 2024-02-05 05:50:33 WSGI app 0 (mountpoint='') ready in 11 seconds on interpreter 0x5629901dee50 pid: 1 (default app) 2024-02-05 05:50:33 *** uWSGI is running in multiple interpreter mode *** 2024-02-05 05:50:33 gracefully (RE)spawned uWSGI master process (pid: 1) 2024-02-05 05:50:33 spawned uWSGI worker 1 (pid: 22, cores: 1) 2024-02-05 05:50:33 metrics collector thread started 2024-02-05 05:50:33 spawned 2 offload threads for uWSGI worker 1 2024-02-05 05:50:55 announcing my loyalty to the Emperor... for this code in pythonanywhere what should i do for this ?
and this is the code that i am running for app.py
i have my all the network.py , cartoonizer.py in the site-package as a workaround and even did the changes to the directory pointing and this is the final code
import os
import io
import uuid
import sys
import yaml
import traceback
from markupsafe import Markup
import tensorflow as tf
sys.path.insert(0, './home/shahhtejas/mysite/white_box_cartoonizer/')
from white_box_cartoonizer.cartoonize import WB_Cartoonize
# /home/shahhtejas/files/mysite/myfile.txt
from pathlib import Path
THIS_FOLDER = Path(__file__).parent.resolve()
my_file = THIS_FOLDER / "config.yaml"
print("111111111111111111111",my_file)
with open(my_file, 'r') as fd:
opts = yaml.safe_load(fd)
sys.path.insert(0, './home/shahhtejas/mysite/white_box_cartoonizer/')
import cv2
from flask import Flask, render_template, make_response, flash
import flask
from PIL import Image
import numpy as np
from white_box_cartoonizer.cartoonize import WB_Cartoonize
from flask_cors import CORS
app = Flask(__name__)
CORS(app, origins = '*')
app.config['UPLOAD_FOLDER_VIDEOS'] = 'static/uploaded_videos'
app.config['CARTOONIZED_FOLDER'] = 'static/cartoonized_images'
app.config['OPTS'] = opts
# wb_cartoonizer = WB_Cartoonize(os.path.abspath("white_box_cartoonizer/saved_models/"), opts['gpu'])
home_directory = os.path.expanduser("~")
# Define the path to the weights directory
weights_directory = os.path.join(home_directory, "mysite/white_box_cartoonizer/saved_models/")
# Initialize the WB_Cartoonize object
wb_cartoonizer = WB_Cartoonize(weights_directory, opts['gpu'])
def convert_bytes_to_image(img_bytes):
pil_image = Image.open(io.BytesIO(img_bytes))
if pil_image.mode=="RGBA":
image = Image.new("RGB", pil_image.size, (255,255,255))
image.paste(pil_image, mask=pil_image.split()[3])
else:
image = pil_image.convert('RGB')
image = np.array(image)
return image
@app.route('/')
@app.route('/cartoonize', methods=["POST", "GET"])
def cartoonize():
opts = app.config['OPTS']
if flask.request.method == 'POST':
try:
if flask.request.files.get('image'):
img = flask.request.files["image"].read()
image = convert_bytes_to_image(img)
print("32222222222222222222",image)
img_name = str(uuid.uuid4())
print("img_name",img_name)
cartoon_image = wb_cartoonizer.infer(image)
cartoonized_img_name = os.path.join(app.config['CARTOONIZED_FOLDER'], img_name + ".jpg")
cv2.imwrite(cartoonized_img_name, cv2.cvtColor(cartoon_image, cv2.COLOR_RGB2BGR))
if not opts["run_local"]:
output_uri = upload_blob("cartoonized_images", cartoonized_img_name, img_name + ".jpg", content_type='image/jpg')
print(output_uri)
os.system("rm " + cartoonized_img_name)
cartoonized_img_name = generate_signed_url(output_uri)
print(cartoonized_img_name)
return cartoonized_img_name
except Exception:
print(traceback.print_exc())
flash("Our server hiccuped :/ Please upload another file! :)")
return "Error"
else:
return "Please use POST method to upload the image"
if __name__ == "__main__":
app.run(debug=False, host='127.0.0.1', port=int(os.environ.get('PORT', 8080)))
i need to know what should i do for tensor flow library and how to solve that error for the tensor flow directory error to point to the location just like it does in VS code and not in the pythonanywhere.com code