Skip to content

Commit

Permalink
Update the files
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshilShah1804 committed Jun 27, 2024
1 parent 77ad168 commit 86cb03e
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 67 deletions.
Binary file modified __pycache__/chatbot.cpython-311.pyc
Binary file not shown.
12 changes: 7 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,31 @@ def allowed_file(filename):
bot = PDF_Chatbot()
@app.route('/')
def index():
return render_template('index.html')
return render_template('index.html', file = filename_global if upload else "No PDF uploaded")

@app.route('/upload', methods=['POST'])
def upload_file():

if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
return "No file found"
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
return "Upload a valid file"
if file and allowed_file(file.filename):
filename = file.filename
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
flash('File uploaded successfully')
global upload
upload = True
global filename_global
filename_global = filename
bot.parse(filename)
return redirect(url_for('index'))
return "Upload successful! Let's chat!"
else:
flash('Invalid file format. Only PDFs are allowed.')
return redirect(request.url)
return "Invalid file format. Only PDFs are allowed."

@app.route('/query',methods=['POST'])

Expand Down
23 changes: 13 additions & 10 deletions chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ def __init__(self):

self.service_context = ServiceContext.from_defaults(
chunk_size = 512, llm = self.llm, embed_model = self.embed_model
)
)
self.chat_engine = None
self.memory = None
self.index = None

def parse(self,name):
parser = LlamaParse(
Expand All @@ -36,18 +39,18 @@ def parse(self,name):
)

self.document = parser.load_data(f"uploads\{name}")

def query(self, query):
memory = ChatMemoryBuffer.from_defaults(token_limit=2000)
index = VectorStoreIndex.from_documents(self.document, service_context=self.service_context)
chat_engine = index.as_chat_engine(

self.memory = ChatMemoryBuffer.from_defaults(token_limit=2000)
self.index = VectorStoreIndex.from_documents(self.document, service_context=self.service_context)
self.chat_engine = self.index.as_chat_engine(
chat_mode="context",
memory=memory,
memory=self.memory,
system_prompt=(
"You are formal chatbot. Give your answer in html rendering, without <html> and <body> tags."
"You are formal chatbot. Give your answer in html rendering, without <html> and <body> tags. Only answer if it is present in the pdf."
),
)

response = chat_engine.chat(query)

def query(self, query):
response = self.chat_engine.chat(query)
print(response)
return response
66 changes: 45 additions & 21 deletions static/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
body {
margin: 0;
padding: 0;
background-color: #9DB2BF;
background-color: #333333;
overflow: hidden;
}

Expand All @@ -19,13 +19,13 @@ body {
left: 0;
width: 100%;
height: 75vh;
background-color: #9DB2BF;
background-color: #333333;
/* border: 1px solid #141414; */
/* min-height: 100%; */
min-height: 68%;
border-radius: 15px;
overflow-y: auto;
scrollbar-width: none;
scrollbar-width: none;
}

.text-field {
Expand All @@ -34,7 +34,7 @@ body {
width: 100%;
height: 15vh;
overflow-y: auto;
background-color: #27374D;
background-color: #202020;
border: 1px solid #5A5A5A;
border-radius: 15px;
display: flex;
Expand All @@ -48,10 +48,10 @@ body {
height: auto;
padding: 2%;
margin: 2%;
background-color: #526D82;
background-color: #444444;
border-radius: 15px 15px 15px 0px;
word-wrap: break-word;
color: #FFFFFF;
color: #eeeeee;
}

.pre-received {
Expand All @@ -60,7 +60,7 @@ body {
height: 25px;
padding: 2%;
margin: 2%;
background-color: #27374D;
background-color: #444444;
border-radius: 15px 15px 15px 0px;
word-wrap: break-word;
color: #FFFFFF;
Expand Down Expand Up @@ -117,44 +117,49 @@ body {
text-align: left;
position: relative;
align-self: flex-end;
background-color: #526D82;
background-color: #191919;
border-radius: 15px 15px 0px 15px;
word-wrap: normal;
color: #ffffff;
color: #eeeeee;
}

.formc {
min-width: 80%;
background-color: #9DB2BF;
background-color: #202020;
margin: 0%;
align-items: center;
min-height: 40%;
padding: 1rem;
border-radius: 10px;
display: flex;
flex-direction: row;
justify-content: space-between;
/* flex-direction: row; */
/* justify-content: space-between; */
}

.form_ {
min-width: 60%;
align-items: center;
display: flex;
flex-direction: row;
/* flex-direction: row; */
/* justify-content: space-between; */
}

#query {
max-height: 5vh;
min-height: 3rem;
min-width: 80vw;
min-width: 75vw;
box-sizing: border-box;
/* margin-right: 5rem; */
resize: none;
overflow: auto;
background-color: #888888;
color: #FFFFFF;
border: 1px solid #AAAAAA;
background-color: #444444;
color: #eeeeee;
border-radius: 5px;
padding: 4px;
}
#query:focus {
outline: none;
border-bottom: 2px solid #FFDAB9;
}

img {
Expand All @@ -163,24 +168,43 @@ img {
}

button {
background-color: #526D82;
background-color: #FFDAB9;
border: none;
border-radius: 10px;
padding: 0rem;
margin: 0rem;
font-size: 1rem;
margin: 0rem 0rem 0rem 0.5rem;
/* font-size: 2rem; */
cursor: pointer;
overflow: hidden;
color: #141616;
align-items: flex-end;

}

header{
display: flex;
justify-content: space-between;
background-color: #FFDAB9;
align-items: center;
}

.header{
height: 6vh;
background-color: #526D82;
text-align: left;
font-size: 1.5rem;
font-weight:bold;
padding-left: 2%;
padding-top: 1%;
/* padding: 0.5rem; */
border-radius: 15px 15px 0px 0px;
}

.curr-pdf{
height: auto;
margin-right: 5rem;
padding-left: 1rem;
padding-right: 1rem;
padding-bottom: 4px;
border-bottom: 2px solid #202020;
border-radius: 5px;
}
46 changes: 33 additions & 13 deletions static/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ window.addEventListener('load', () => {
scrollableDiv.scrollTop = scrollableDiv.scrollHeight;
});

function sent() {
// event.preventDefault();
function showAnimation(){
const parentDiv = document.getElementById("chat_box");
const sent_cover = document.createElement("div");
const anim_div = document.createElement("div");
anim_div.className = "pre-received";
anim_div.innerHTML = '<div></div><div></div><div></div>'
parentDiv.appendChild(anim_div);
const scrollableDiv = document.getElementById('chat_box');
scrollableDiv.scrollTop = scrollableDiv.scrollHeight;
}

function sent() {
const parentDiv = document.getElementById("chat_box");
const sent_cover = document.createElement("div");
sent_cover.className = "sent-cover";
const div = document.createElement("div");
div.className = "sent";
Expand All @@ -18,13 +25,9 @@ function sent() {
sent_cover.appendChild(div);
parentDiv.appendChild(sent_cover);
document.getElementById("query").value = "";
anim_div.innerHTML = '<div></div><div></div><div></div>'
parentDiv.appendChild(anim_div);


const scrollableDiv = document.getElementById('chat_box');
scrollableDiv.scrollTop = scrollableDiv.scrollHeight;

showAnimation();
}

function recieved(response){
Expand All @@ -35,15 +38,10 @@ function recieved(response){
div.innerHTML = response;
parentDiv.removeChild(anim);
parentDiv.appendChild(div);

const scrollableDiv = document.getElementById('chat_box');
scrollableDiv.scrollTop = scrollableDiv.scrollHeight;
}

// document.getElementById('myForm').addEventListener('submit', function(event) {
// event.preventDefault(); // Prevent form submission
// });


document.getElementById('myForm').addEventListener('keydown', function(event) {
if (event.shiftKey && event.key === 'Enter') {
Expand Down Expand Up @@ -86,6 +84,28 @@ document.getElementById('sendQuery').addEventListener('click', function(event) {
}
});

document.getElementById('fileInput').addEventListener('change', function(event) {
event.preventDefault(); // Prevent default form submission
document.getElementById('curr-pdf').innerHTML = document.getElementById('fileInput').files[0].name;
showAnimation();
recieved('Uploading...');
showAnimation();
var form = document.getElementById('uploadForm');
var file_data = new FormData(form);
var oReq = new XMLHttpRequest();
oReq.open("POST", "/upload", true);
oReq.onload = function(oEvent) {
if (oReq.status == 200) {
console.log(oReq.responseText);
recieved(oReq.responseText);
} else {
console.log("Error " + oReq.status + " occurred when trying to upload your file.");
recieved("Error " + oReq.status + " occurred when trying to upload your file.");
}
};
oReq.send(file_data);

})

// document.getElementById('submit').addEventListener('click', function(event) {
// event.preventDefault();
Expand Down
34 changes: 16 additions & 18 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@
<div class="header">
PDF - ChatBot
</div>

<div class="curr-pdf" id="curr-pdf">
{{file}}
</div>
</header>
<section class="chat-box" id="chat_box">
<div class="received">
Hello Lorem ipsum, dolor sit amet consectetur adipisicing elit. Ea officia nihil suscipit tempora fugit sint, eum iure totam consequatur. Atque quia obcaecati, eligendi iusto ullam nulla hic recusandae illum iste.
</div>
<div class="sent-cover">
<div class="sent">
Hiii
</div>
<div class="received" >
Welcome to PDF - ChatBot. Please upload the PDF file to get started.
</div>


<!-- <div class="pre-received">
<div></div>
Expand All @@ -38,22 +36,22 @@

<section class="text-field">
<div class="formc">

<form class="form_" id="myForm" action="/query" method="post">
<textarea id="query", name="textarea"></textarea>
<button id = "sendQuery">
<span class="material-symbols-outlined">
send
</span>
</button>

<textarea id="query", name="textarea"></textarea>

<button id = "sendQuery">
<svg xmlns="http://www.w3.org/2000/svg" height="40px" viewBox="0 -960 960 960" width="40px" fill="#000000"><path d="M120-160v-640l760 320-760 320Zm66.67-102 520.66-218-520.66-220v158.67L428-480l-241.33 60v158Zm0 0v-438 438Z"/></svg>
</button>

</form>

<form action="/upload" method="post" enctype="multipart/form-data" id="uploadForm">
<input type="file" id="fileInput" name="file" accept="application/pdf" onchange="document.getElementById('uploadForm').submit()" style="display: none;">
<input type="file" id="fileInput" name="file" accept="application/pdf" style="display: none;">

<button type="button" class="upload-button" onclick="document.getElementById('fileInput').click();">
<span class="material-symbols-outlined">
upload_file
</span>
<svg xmlns="http://www.w3.org/2000/svg" height="40px" viewBox="0 -960 960 960" width="40px" fill="#000000"><path d="M448-201.33h66.67V-391l76 76 46.66-47L480-516.67l-156 156L371-314l77-77v189.67ZM226.67-80q-27 0-46.84-19.83Q160-119.67 160-146.67v-666.66q0-27 19.83-46.84Q199.67-880 226.67-880H574l226 226v507.33q0 27-19.83 46.84Q760.33-80 733.33-80H226.67Zm314-542.67v-190.66h-314v666.66h506.66v-476H540.67Zm-314-190.66v190.66-190.66 666.66-666.66Z"/></svg>
</button>
<input type="submit" value="Upload" style="display: none;">
</form>
Expand Down

0 comments on commit 86cb03e

Please sign in to comment.