Skip to content

Commit

Permalink
trying redis
Browse files Browse the repository at this point in the history
  • Loading branch information
mukulpatnaik committed Feb 7, 2023
1 parent 0c7d593 commit fd25c44
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
32 changes: 22 additions & 10 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
import os
import requests
from flask_cors import CORS
import redis
from _md5 import md5

app = Flask(__name__)
db=redis.from_url(os.environ['REDISCLOUD_URL'])
# db = redis.StrictRedis(host='localhost', port=6379, db=0)
CORS(app)


Expand Down Expand Up @@ -137,27 +141,27 @@ def gpt(self, prompt):
response = {'answer': answer, 'sources': sources}
return response

def reply(self, prompt):
print(prompt)
prompt = self.create_prompt(df, prompt)
return self.gpt(prompt)

@app.route("/", methods=["GET", "POST"])
def index():
return render_template("index.html")

@app.route("/process_pdf", methods=['POST'])
def process_pdf():
print("Processing pdf")
key = md5(request.data).hexdigest()
print(key)
x = pd.DataFrame(columns=['keys'])
x = x.append({'keys': key}, ignore_index=True)
x.to_csv('static/keys.csv', mode='a', index=False, header=False)
file = request.data
pdf = PdfReader(BytesIO(file))
chatbot = Chatbot()
paper_text = chatbot.parse_paper(pdf)
df = chatbot.paper_df(paper_text)
df = chatbot.calculate_embeddings(df)
print(df.head(5))
# save dataframe to csv at static/df.csv
df.to_csv('static/df.csv')
if db.get(key) is None:
db.set(key, df.to_json())
print("Done processing pdf")
return "PDF file processed and embeddings calculated"

Expand All @@ -166,13 +170,17 @@ def download_pdf():
chatbot = Chatbot()
url = request.json['url']
r = requests.get(str(url))
print(r.headers)
key = md5(r.content).hexdigest()
x = pd.DataFrame(columns=['keys'])
x = x.append({'keys': key}, ignore_index=True)
x.to_csv('static/keys.csv', mode='a', index=False, header=False)
pdf = PdfReader(BytesIO(r.content))
paper_text = chatbot.parse_paper(pdf)
df = chatbot.paper_df(paper_text)
df = chatbot.calculate_embeddings(df)
print(df.head(5))
df.to_csv('static/df.csv')
if db.get(key) is None:
db.set(key, df.to_json())
print("Done processing pdf")
return "PDF file processed and embeddings calculated"

Expand All @@ -181,7 +189,11 @@ def reply():
chatbot = Chatbot()
query = request.json['query']
query = str(query)
df = pd.read_csv('static/df.csv')
x = pd.read_csv('static/keys.csv')
key = x['keys'].iloc[-1]
print(key)
print(db.get(key))
df = pd.read_json(BytesIO(db.get(key)))
print(df.head(5))
prompt = chatbot.create_prompt(df, query)
response = chatbot.gpt(prompt)
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ flask-cors
matplotlib
scipy
plotly
redis
zlib
gunicorn==20.1.0
scikit-learn==0.24.1
2 changes: 1 addition & 1 deletion static/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ input.addEventListener("change", async function() {
const file = this.files[0];
const fileArrayBuffer = await file.arrayBuffer();
console.log(fileArrayBuffer);
// Make a post request to "http://127.0.0.1:5000/process_pdf" with the file
// Make a post request to /process_pdf with the file
fetch('/process_pdf', {
method: 'POST',
body: fileArrayBuffer,
Expand Down
2 changes: 2 additions & 0 deletions static/keys.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
keys
d56d71ecadf2137be09d8b1d35c6c042
2 changes: 1 addition & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<p style="color: white; margin: 10px; /* padding: 10px; */ /* border-radius: 10px; */ /* background-color: grey; */ width: fit-content; /* max-width: 80%; */ text-align: start; overflow-wrap: break-word; /* font-style: normal; */ font-variant-caps: normal; /* font-weight: normal; */ /* font-stretch: normal; */ /* font-size: 1.2em; */ line-height: normal; font-family: Roboto;">or</p>

<form id="up" action="http://127.0.0.1:5000/process_pdf" method="post" style="margin-top: 50px; margin-left: 15px;" enctype="multipart/form-data">
<form id="up" style="margin-top: 50px; margin-left: 15px;" enctype="multipart/form-data">
<label for="file-input" class="upload-btn">Upload PDF</label>
<input type="file" accept=".pdf" id="file-input"/>
</form>
Expand Down

0 comments on commit fd25c44

Please sign in to comment.