Commit a31dd3d5 authored by G.M. Amashi S. Bastiansz's avatar G.M. Amashi S. Bastiansz 🎯

Chatbot creation

parent 757209d2
from werkzeug.utils import secure_filename
import requests
import shutil
from flask import send_file
from reveng.processInput import checkCommon, processInput
from flask import url_for,flash,Flask, render_template, request, redirect
from flask import url_for, flash, Flask, render_template, request, redirect
import json
import sys
import os
from flask_cors import CORS, cross_origin
REVENG_DIR = os.path.dirname(os.path.abspath(__file__))
TEMPLATE = os.path.join(REVENG_DIR, "\\templates")
......@@ -12,6 +16,7 @@ STATIC = os.path.join(REVENG_DIR, "\\static")
app = Flask(__name__)
app.config["DEBUG"] = True
cors = CORS(app)
@app.route('/test', methods=['GET'])
......@@ -42,22 +47,22 @@ def about():
return render_template('about.html')
import requests
# route to display GIF image to the user
@app.route('/tts/response/', methods=['POST'])
@app.route('/tts/get', methods=['GET', 'POST'])
def response():
clearoutputfolder()
message = request.get_json()
responseGIF = processInput(message['message'])
# clearoutputfolder()
# message = request.get_json()
# responseGIF = processInput(message['message'])
URL = 'http://localhost:3000/tts/upload'
PARAMS = {
"files":{
"file":responseGIF
}
}
requests.post(URL,PARAMS)
return send_file(responseGIF, mimetype='image/gif')
request.args.get('keyword')
responseGIF = processInput(request.args.get('keyword'))
# response = {
# "gifPath": responseGIF
# }
return responseGIF
# return send_file(responseGIF, mimetype='image/gif')
# clear the OUTPUT folder after displaying the GIF image
......@@ -73,8 +78,10 @@ def clearoutputfolder():
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
from werkzeug.utils import secure_filename
## upload image
# upload image
@app.route('/tts/upload', methods=['POST'])
def upload_image():
###
......@@ -110,8 +117,11 @@ def display_image(filename):
###
return redirect(url_for('static', filename='uploads/' + filename), code=301)
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
def allowed_file(filename):
###
# This method is used check the extension of selected file
......@@ -125,7 +135,7 @@ def allowed_file(filename):
# route to redirect Sign Translation page
@app.route('/tts')
def textToSignEngine():
return render_template('textToSign.html')
return render_template('chatbot.html')
# route to redirect Contact Us page
......
###
# This file is used to create firbase configuration for the application
# Each application has unique firbase configuration.
# @author Amashi Bastiansz | IT17143950
# @version 1.0
# @since 2020-08-13
# @since 2020-08-13
###
import json
......@@ -54,3 +54,8 @@ def getStorageInstance():
###
return store
def getStorageBucket():
return bucket
###
# This file is used to:
# generate a GIF image
# send the generated GIF to the folder
# delete the GIF from cache
# generate a GIF image
# send the generated GIF to the folder
# delete the GIF from cache
# @author Amashi Bastiansz | IT17143950
# @version 1.5
# @since 2020-10-01
# @since 2020-10-01
###
import imageio
......@@ -16,42 +16,61 @@ import random
import cv2
import PIL.Image as pigm
import PIL.GifImagePlugin as gifHandler
from gcloud.storage.blob import Blob
from reveng.firebaseConfig import getStorageInstance, getStorageBucket
PATH = os.path.dirname(os.path.abspath(__file__))
gifName = ''.join(random.choices(string.ascii_uppercase +
string.digits, k=15))
STATIC_PATH = os.getcwd() + '\\static'
STATIC_PATH = os.getcwd() + "\\static"
Blob.generate_signed_url
store = getStorageInstance()
bucket = getStorageBucket()
def generateGIF(images):
###
# This method is used to generate a path for the created GIF image
# This method is used to generate a path for the created GIF image
# @return the generated GIF path
###
gifPath = os.path.join(STATIC_PATH + "\\output\\" + gifName + '.gif')
blob = bucket.blob(gifName + ".gif")
gifPath = STATIC_PATH + "\\output\\" + gifName + ".gif"
imageio.mimwrite(gifPath, images, duration=0.5)
print(gifPath)
return gifPath
with open(gifPath, 'rb') as gifImage:
blob.upload_from_file(gifImage)
blob.make_public()
response = {
"gifName": gifName,
"gifLocalPath": gifPath,
"gifPublicUrl": blob.public_url
}
print(blob.public_url)
return response
def sendGIF():
###
# This method is used to send the generated GIF to the exact folder to store it
###
x = imageio.mimread(os.path.join(PATH + "\\output\\" + gifName + '.gif'))
return x
def deleteFromCache():
###
# This method is used to delete the generated GIF from cache after uploading it to
# This method is used to delete the generated GIF from cache after uploading it to
# the exact project folder
# @return call to generateGIF() method with selected hand images
###
os.remove(os.path.join(PATH + "\\output\\" + gifName + '.gif'))
* {
margin: 0;
padding: 0;
border: 0;
outline: 0;
}
body {
background-color: #e74c3c;
font-family: Arial;
}
.chat {
width: 300px;
height: 500px;
background-image: url("http://cdn9.staztic.com/app/a/2063/2063481/whatsapp-wallpaper-pack-hd-600519-0-s-156x156.jpg");
background-size: cover;
background-position: center center;
position: absolute;
border: 5px solid #000;
border-radius: 20px;
overflow: hidden;
box-shadow: 0 0 20px #000;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
box-sizing: border-box;
}
.chat .messages {
position: absolute;
left: 0;
bottom: 60px;
height: auto;
width: 100%;
}
.chat .messages .message {
padding: 20px 10px;
width: 100%;
box-sizing: border-box;
}
.chat .messages .message .you,
.chat .messages .message .bot {
border-radius: 10px;
padding: 10px;
width: 75%;
position: relative;
}
.chat .messages .message .you img,
.chat .messages .message .bot img {
width: 100%;
border-radius: 10px;
display: block;
}
.chat .messages .message .you:before,
.chat .messages .message .bot:before {
padding: 5px;
border-radius: 5px;
position: absolute;
top: -15px;
font-weight: bold;
color: #fff;
font-size: 12px;
}
.chat .messages .message .you {
background: #2ecc71;
float: right;
}
.chat .messages .message .you:before {
content: "You:";
background-color: #2ecc71;
right: 10px;
}
.chat .messages .message .bot {
background-color: #3498db;
float: left;
}
.chat .messages .message .bot:before {
content: "Bot:";
background-color: #3498db;
left: 10px;
}
.chat .messages .message:after {
content: "";
clear: both;
display: table;
}
.chat .input {
position: absolute;
left: 0;
bottom: 0;
height: 60px;
width: 100%;
background: #ecf0f1;
}
.chat .input .text {
background-color: #fff;
border-radius: 20px;
height: 40px;
position: absolute;
top: 10px;
left: 10px;
width: 270px;
box-shadow: inset 0 0 5px #aaa;
line-height: 40px;
padding: 0 20px;
box-sizing: border-box;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="static/styles/chatbot-test.css"/>
<script>
window.console = window.console || function(t) {};
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<script>
if (document.location.search.match(/type=embed/gi)) {
window.parent.postMessage("resize", "*");
}
</script>
<script src="chrome-extension://mooikfkahbdckldjjndioackbalphokd/assets/prompt.js"></script>
</head>
<body translate="no" data-new-gr-c-s-check-loaded="14.990.0" data-gr-ext-installed="">
<div class="chat">
<div class="messages">
<div class="message">
<div class="bot">
Type something with "gif me funny cat" to see what happens ;)
</div>
</div>
</div>
<div class="input">
<form action="#" id="chat" method="post">
<input class="text" contenteditable="" placeholder="Type your message here...">
</form>
</div>
</div>
<script src="https://cpwebassets.codepen.io/assets/common stopExecutionOnTimeout-157cd5b220a5c80d4ff8e0e70ac069bffd87a61252088146915e8726e5d9f147.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script id="rendered-js">
(function() {
var app;
$(document).ready(function() {
return app.init();
});
app = {
//api_key: "dc6zaTOxFJmzC", // Public API key from giphy.com
init: function() {
return this.bind_events();
},
bind_events: function() {
return $(document).on("submit", "#chat", function(e) {
app.send_message();
return e.preventDefault();
});
},
send_message: function() {
var msg;
msg = $(".text").val().trim();
if (msg) {
$(".text").val("");
$(".messages").append("<div class='message'><div class='you'>" + msg + "</div></div>");
return this.check(msg);
}
},
check: function(msg) {
var keyword;
if (msg != null) {
return this.get_gif(msg);
} else {
return this.bot_post("Wrong syntax ''gif me keyword''.");
}
},
bot_post: function(msg) {
return $(".messages").append("<div class='message'><div class='bot'>" + msg + "</div></div>");
},
get_gif: function(keyword) {
console.log(keyword)
return $.get(`http://localhost:3000/tts/get?keyword=${keyword}` , function(data) {
var index;
console.log(data)
return app.bot_post("<img src='" + data.gifPublicUrl + "' alt='' />");
/*if (data.data.length === 0) {
return app.bot_post("Sorry I can't find any gif for that :(");
} else {
index = Math.floor(Math.random() * ((data.data.length - 1) - 0 + 1) + 0);
return app.bot_post("<img src='" + data.data[index].images.fixed_height.url + "' alt='' />");
}*/
}
);
}
};
}).call(this);
</script>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment