Skip to content

Commit

Permalink
Merge pull request #11 from madmaverickminion/main
Browse files Browse the repository at this point in the history
Web Development
  • Loading branch information
peteruhrig authored Sep 19, 2023
2 parents 3ec74af + ed75dc3 commit 56a0a9c
Show file tree
Hide file tree
Showing 19 changed files with 1,361 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ openpose_containers_focal_nvcaffe.sif
stickfigs.sif
hidden_faceBAD.mp4
Sujeto_videos/
Sujeto_results_complete/
Sujeto_results_complete/
webpage/static/upload/
41 changes: 41 additions & 0 deletions backend_scripts/rha.slurm
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash

#SBATCH -p gpu
#SBATCH --mem=8gb
#SBATCH --gres=gpu:1#!/bin/bash
#SBATCH -N 1
#SBATCH -c 1
#SBATCH --mem=8gb
#SBATCH --output=test.o%j
#SBATCH --partition=gpu
#SBATCH -C gpu2v100|gpu4v100
#SBATCH --gres=gpu:1 # Request one gpu out of 2 (Max)

#SBATCH --account=mbt8 # substitute appropriate group here

module load singularity
module load CUDA/11.1.1-GCC-10.2.0

export PATH="/usr/local/software/singularity/3.10.4/bin:/usr/local/easybuild_allnodes/software/CUDAcore/11.1.1/nvvm/bin:/usr/local/easybuild_allnodes/software/CUDAcore/11.1.1/bin:/usr/local/easybuild_allnodes/software/binutils/2.35-GCCcore-10.2.0/bin:/usr/local/easybuild_allnodes/software/GCCcore/10.2.0/bin:/home/sxg1373/miniconda3/bin:/home/sxg1373/miniconda3/condabin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/dell/srvadmin/bin:/home/sxg1373/.local/bin:/home/sxg1373/bin"
echo "hello"
echo $PFSDIR
scp -r /scratch/users/sxg1373/input_files/$1 /scratch/users/sxg1373/1.jpg /home/sxg1373/rha_sing.sif $PFSDIR
ls "$PFSDIR"
final_output_dir=$6
input_dir=$PFSDIR/$1
mkdir $PFSDIR/output_files
output_dir=$PFSDIR/output_files
img_dir=$PFSDIR/1.jpg

for file in "$input_dir"/*
do
echo "$file"
file_name=`echo "${file##*/}"`
echo "This is the file name $file_name"
singularity run --nv --bind "$PFSDIR" $PFSDIR/rha_sing.sif "$2" "audiovideo" $3 $4 $5 "$file" "$output_dir/output_$file_name.mp4" "$img_dir"
done
echo "saving files"

scp -r $output_dir/* $final_output_dir
rm -rf "$PFSDIR"/*
rm -r /scratch/users/sxg1373/input_files/$1
43 changes: 43 additions & 0 deletions backend_scripts/scheduler_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash
#this script will setup the files in correct directories before scheduling the
#batch script

#read files of all the directories in input directory one by one and transfer
#them to some input directory in scratch folder

#set the parameters of anonymization according to the config.json file for batch
#batch job script

module load Python/3.10.8-GCCcore-12.2.0-bare
export PATH="/usr/local/easybuild_allnodes/software/Python/3.10.8-GCCcore-12.2.0-bare/bin:/usr/local/easybuild_allnodes/software/OpenSSL/1.1/bin:/usr/local/easybuild_allnodes/software/XZ/5.2.7-GCCcore-12.2.0/bin:/usr/local/easybuild_allnodes/software/SQLite/3.39.4-GCCcore-12.2.0/bin:/usr/local/easybuild_allnodes/software/Tcl/8.6.12-GCCcore-12.2.0/bin:/usr/local/easybuild_allnodes/software/ncurses/6.3-GCCcore-12.2.0/bin:/usr/local/easybuild_allnodes/software/bzip2/1.0.8-GCCcore-12.2.0/bin:/usr/local/easybuild_allnodes/software/binutils/2.39-GCCcore-12.2.0/bin:/usr/local/easybuild_allnodes/software/GCCcore/12.2.0/bin:/usr/local/software/singularity/3.10.4/bin:/usr/local/easybuild_allnodes/software/CUDAcore/11.1.1/nvvm/bin:/usr/local/easybuild_allnodes/software/CUDAcore/11.1.1/bin:/home/sxg1373/miniconda3/bin:/home/sxg1373/miniconda3/condabin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/dell/srvadmin/bin:/home/sxg1373/.local/bin:/home/sxg1373/bin"
scratch_dir="/scratch/users/sxg1373/input_files"
for f in /home/sxg1373/sxg1373gallinahome/anonymizer_storage/input/*; do
if [ -d "$f" ]; then
echo $f
status=`grep -o '"status": "[^"]*' $f/config.json | grep -o '[^"]*$'`

#python /home/sxg1373/change_status.py $f/config.json
#change the status to 1 by default the status is 0
cp -r $f/ $scratch_dir/

ls $scratch_dir
project_name=`grep -o '"project_name": "[^"]*' $f/config.json | grep -o '[^"]*$'`
visual_anonymization=`grep -o '"visual_anonymization": "[^"]*' $scratch_dir/$project_name/config.json | grep -o '[^"]*$'`
pitch=`grep -o '"pitch": "[^"]*' $scratch_dir/$project_name/config.json | grep -o '[^"]*$'`
echo_=`grep -o '"echo": "[^"]*' $scratch_dir/$project_name/config.json | grep -o '[^"]*$'`
distortion=`grep -o '"distortion": "[^"]*' $scratch_dir/$project_name/config.json | grep -o '[^"]*$'`
rm $scratch_dir/$project_name/config.json
mkdir /home/sxg1373/sxg1373gallinahome/anonymizer_storage/output/$project_name
output_dir="sxg1373@hpc8:/home/sxg1373/sxg1373gallinahome/anonymizer_storage/output/$project_name"
echo $project_name $visual_anonymization $pitch $echo_ $distortion $output_dir
#call the batch script and pass the parameters to it
job_id_str=`sbatch rha.slurm $project_name $visual_anonymization $pitch $echo_ $distortion $output_dir`
echo "batch job scheduled $job_id_str"
rm -r $f
echo "removed file from anonymizer_storage"

#else
#echo "No files found"
fi

done
16 changes: 16 additions & 0 deletions change_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import json
import sys

if __name__=="__main__":
path=sys.argv[1]
print(path)
with open(path,'r') as file:
data = json.load(file)
# data['status']=1
print(data)
data['status']=1
with open(path,'w') as outfile:
json.dump(data, outfile)
print(data)


36 changes: 36 additions & 0 deletions webpage/background_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# background_runner.py
# from flask_mail import Message
import time
import uuid
import os


class BackgroundRunner:
def __init__(self, executor):
self.executor = executor
# self.upload_key=upload_key

def do_task(self, upload_key):
inpath=f"/home/saksham/Desktop/RED_HEN/RedHenAnonymizer/webpage/static/upload/{upload_key}/swap_output_elon.mp4"
# facepath=""
outpath=f"/home/saksham/Desktop/RED_HEN/RedHenAnonymizer/webpage/static/upload/{upload_key}/swap_output_elon.mp4"
pitch="-4"
cmd=f"python /home/saksham/Desktop/RED_HEN/RedHenAnonymizer/rha.py --inpath {inpath} --outpath {outpath} --pitch {pitch} --anonymize 'audiovideo' --visual_anonymization 'hider'"
print(cmd)
error = os.system(cmd)
if error:
raise Exception(f'unable to swap faces. Check fsgan. error code: {error}')

print("Done")


def do_task_async(self, upload_key):
task_id = upload_key
self.executor.submit_stored(task_id, self.do_task, upload_key)
return task_id

def task_status(self, task_id):
if not self.executor.futures.done(task_id):
return "running"
self.executor.futures.pop(task_id)
return "completed"
237 changes: 237 additions & 0 deletions webpage/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
from flask import Flask, render_template,request,redirect,flash, abort, jsonify,send_file
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, login_user, UserMixin, login_required,current_user, logout_user
import os
import json
import glob
from uuid import uuid4
from datetime import datetime
from background_runner import BackgroundRunner
from flask_executor import Executor
import shutil
from io import BytesIO
from zipfile import ZipFile
app=Flask(__name__)
executor = Executor(app)
background_runner = BackgroundRunner(executor)

# @login_required
# def show_videos():
# for user in db.session.query(Video_details).filter_by(username=current_user.username):
# print(user.username)
# return "Hello World"
app.config['SQLALCHEMY_DATABASE_URI']='mysql://anonymizer:1g%$Y18yj6%4068Mjd@127.0.0.1/anonymizer'
app.secret_key = 'abrakndf'
app.config['UPLOAD_EXTENSIONS']=['.mpg', '.mp2', '.mpeg', '.mpe', '.mpv', '.mp4', '.m4p', '.m4v', '.avi', '.wmv']
db=SQLAlchemy(app)
login_manager=LoginManager()
login_manager.init_app(app)

class Video_details(UserMixin,db.Model):
sno= db.Column(db.Integer, primary_key=True)
username=db.Column(db.String(20), nullable=False)
filepath=db.Column(db.String(200), nullable=False)
outpath=db.Column(db.String(200),nullable=False)
project_name=db.Column(db.String(200), nullable=False)
date=db.Column(db.String(20),nullable=True)
# def __repr__(self):
# return '<User %r>' % self.username
def __init__(self,username,filepath,outpath,project_name,date):
self.username=username
self.filepath=filepath
self.outpath=outpath
self.project_name=project_name
self.date=date
def get_id(self):
return (self.sno)

class User(UserMixin,db.Model):
sno= db.Column(db.Integer, primary_key=True)
name=db.Column(db.String(80),nullable=False)
username=db.Column(db.String(20),unique=True, nullable=False)
email=db.Column(db.String(80), unique=True,nullable=False)
password=db.Column(db.String(120),nullable=False)
def __init__(self, name,username,email,password):
self.name=name
self.username=username
self.email=email
self.password=password
def __repr__(self):
return '<User %r>' % self.username
def get_id(self):
return (self.sno)


@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))

@app.route("/",methods=['GET','POST'])
def login():
if request.method=='POST':
username=request.form.get('username')
password=request.form.get('password')
user=User.query.filter_by(username=username).first()
if user and password==user.password:
login_user(user)
return redirect("dashboard")
else :
flash('Invalid Credentials', 'warning ')
return redirect('/')

return render_template("login.html")

@app.route('/logout')
def logout():
# Logout the user
logout_user()
return redirect('/')


@app.route("/register", methods=['POST','GET'])
def register():
if request.method=='POST':
email=request.form.get('email')
name=request.form.get('name')
username=request.form.get('username')
password=request.form.get('password')
# print(email,password,username,name)
user=User(name=name,username=username,email=email,password=password)
db.session.add(user)
db.session.commit()
flash("Registered Successfully!!",'success')
return redirect("/")
return render_template("register.html")

@app.route("/main")
@login_required
def main():
return render_template("main.html")

@app.route("/dashboard")
@login_required
def dashboard():
# Name type uploaded_on download_link
data=[]
i=1
for user in db.session.query(Video_details).filter_by(username=current_user.username):
temp=[]
temp.append(i)
i=i+1
temp.append(user.project_name)
temp.append("audio-video")
temp.append(user.date)
temp.append(user.project_name)
data.append(temp)

return render_template("dashboard.html", Name=current_user.name, username=current_user.username,data=data)

# @app.route("/show_videos")
# @login_required
# def show_videos():
# for user in db.session.query(Video_details).filter_by(username=current_user.username):
# print(user.username)
# return "Hello World"
@app.route("/download/<filename>")
@login_required
def download(filename):
stream = BytesIO()
path=f"/mnt/rds/redhen/gallina/home/sxg1373/anonymizer_storage/output/{filename}"
dir_list = os.listdir(path)
if len(dir_list)==0:
return "Try again later"
print(path)
with ZipFile(stream, 'w') as zf:
for file in dir_list:
print("this is the file", file)
zf.write(f"{path}/{file}", os.path.basename(file))
# shutil.make_archive(f"{filename}", 'zip', path)
stream.seek(0)

return send_file(
stream,
as_attachment=True,
attachment_filename=f'{filename}.zip'
)

return filename

@app.route("/upload",methods=['POST','GET'])
@login_required
def upload():
if request.method=='POST':
files=request.files
form=request.form

upload_key=str(uuid4())
is_ajax = False
if form.get("__ajax", None) == "true":
is_ajax = True

# Target folder for these uploads.
# make a different folder on the server gallo in which the user files are stored permanently
target = "/mnt/rds/redhen/gallina/home/sxg1373/anonymizer_storage/input/{}".format(form.get("project_name",None))#temporary directory in which files are stored
outpath = "/mnt/rds/redhen/gallina/home/sxg1373/anonymizer_storage/output/{}".format(form.get("project_name",None))#temporary directory in which files are stored
print("This is the path of the final result",outpath)
print(target)
try:
os.mkdir(target)
except:
if is_ajax:
print("could not create upload directory")
return ajax_response(False, "Couldn't create upload directory: {}".format(target))
else:
return "Couldn't create upload directory: {}".format(target)

# make a json object and store all the anonymization parameters in it
# project_name anonymization_type pitch echo distortion
project_info={"project_name": form.get("project_name",None),
"visual_anonymization": form.get("visual_anonymization",None),
"pitch": form.get("pitch",None),
"echo":str(float(form.get("echo",None))/10),
"distortion":form.get("distortion",None),
"status":"0"
}
# print("echo",str(float(form.get("echo",None))/10))
project_info_json=json.dumps(project_info)
print(project_info)
with open(f"{target}/config.json", "w") as outfile:
outfile.write(project_info_json)
# save that object into a file in target folder


for upload in request.files.getlist("file"):
filename = upload.filename.rsplit("/")[0]
file_ext=os.path.splitext(filename)[1]
if file_ext not in app.config['UPLOAD_EXTENSIONS']:
os.rmdir(target)
if is_ajax:
return ajax_response(False, upload_key)
else:
flash("Please add only video files",'failure')
return redirect("/main")
destination = "/".join([target, filename])
print("Accept incoming file:", filename)
print("Save it to:", destination)
upload.save(destination)
now = datetime.now()

video_details=Video_details(username=current_user.username, filepath=target,outpath=outpath,project_name=form.get("project_name",None), date=now.strftime('%Y-%m-%d %H:%M:%S'))
db.session.add(video_details)
db.session.commit()
if is_ajax:
return ajax_response(True, upload_key)
else:
return redirect("/dashboard")


def ajax_response(status, msg):
status_code = "ok" if status else "error"
return json.dumps(dict(
status=status_code,
msg=msg,
))

with app.app_context():
db.create_all()
app.run(debug=True, port=80000)
Loading

0 comments on commit 56a0a9c

Please sign in to comment.