<jupyter_start><jupyter_text><jupyter_code>!pip install cdqa
!pip install flask-ngrok

import os
import pandas as pd
from ast import literal_eval

from cdqa.utils.converters import pdf_converter
from cdqa.pipeline import QAPipeline
from cdqa.utils.download import download_model
download_model(model='bert-squad_1.1', dir='./models')
!ls models
!mkdir docs
!wget -P ./docs/ https://s2.q4cdn.com/299287126/files/doc_financials/2020/q3/AMZN-Q3-2020-Earnings-Release.pdf
!wget -P ./docs/ https://s2.q4cdn.com/299287126/files/doc_financials/2020/q2/Q2-2020-Amazon-Earnings-Release.pdf
!wget -P ./docs/ https://s2.q4cdn.com/299287126/files/doc_financials/2020/Q1/AMZN-Q1-2020-Earnings-Release.pdf
!wget -P ./docs/ https://s2.q4cdn.com/299287126/files/doc_news/archive/Amazon-Q4-2019-Earnings-Release.pdf
!wget -P ./docs/ https://s2.q4cdn.com/299287126/files/doc_news/archive/Q3-2019-Amazon-Financial-Results.pdf
df = pdf_converter(directory_path='./docs/')
df.head()
# pd.set_option('display.max_colwidth', -1)
df.head()
cdqa_pipeline = QAPipeline(reader='./models/bert_qa.joblib', max_df=1.0)

cdqa_pipeline.fit_retriever(df=df)
import joblib
# cdqa_pipeline.to("cpu")
joblib.dump(cdqa_pipeline, './models/bert_qa_customc.joblib')
cdqa_pipeline=joblib.load('./models/bert_qa_customc.joblib')
cdqa_pipeline
query = 'How much is increase in operating cash flow?'
prediction = cdqa_pipeline.predict(query, 3)
cdqa_pipeline
prediction
query = 'What is latest earnings per share?'
cdqa_pipeline.predict(query)
query = 'How many jobs are created in 2020?'
prediction = cdqa_pipeline.predict(query)
print('query: {}'.format(query))
print('answer: {}'.format(prediction[0]))
print('title: {}'.format(prediction[1]))
print('paragraph: {}'.format(prediction[2]))
query = 'How many full time employees are on Amazon roll?'
prediction = cdqa_pipeline.predict(query)
print('query: {}'.format(query))
print('answer: {}'.format(prediction[0]))
print('title: {}'.format(prediction[1]))
print('paragraph: {}'.format(prediction[2]))
query = 'General Availability of which AWS services were announced?'
prediction = cdqa_pipeline.predict(query, n_predictions=5)
prediction
query = 'What is the impact of COVID on business?'
prediction = cdqa_pipeline.predict(query, n_predictions=2)
print('query: {}'.format(query))
print('answer: {}'.format(prediction[0]))
print('title: {}'.format(prediction[1]))
# print('paragraph: {}'.format(prediction[2]))

from google.colab import drive
drive.mount('/content/drive')
!cp ./models/bert_qa_customc.joblib '/content/drive/MyDrive/'


'''import joblib
import requests
import streamlit as st

# st.set_option('deprecation.showfileUploaderEncoding',False)
st.title("Amazon QA WebApp")
st.text("What would you like to know regarding amazon today?")


@st.cache(allow_output_mutation=True)
def load_model():
    model=joblib.load('./models/bert_qa_customc.joblib')
    return model
with st.spinner('loading model into memory...'):
    model=load_model()
text=st.text_input("Enter question here")
if text:
    st.write('Response:- ')
    with st.spinner('Searching for answers'):
        prediction=model.predict(text)
        st.write('answer: {}'.format(prediction[0]))
        st.write('title: {}'.format(prediction[1]))
        st.write('paragraph: {}'.format(prediction[2]))
    st.write('')
'''



import joblib
import requests
# import streamlit as st
from flask import Flask, render_template, url_for, request
import pandas as pd
import pickle
from flask_ngrok import run_with_ngrok
model=joblib.load('./models/bert_qa_customc.joblib')
# from sklearn.externals import joblib

app = Flask(__name__)
run_with_ngrok(app)


@app.route('/')
def home():
    return 'hii'

@app.route('/predict',methods=['GET'])
def predict():
    
    message='amount of jobs created?'
    prediction = model.predict(message)
    return {'Question':message,'Prediction/Answer':list(prediction)}

if __name__ == '__main__':
    app.run()
message='How many jobs are created in 2020?'
prediction = model.predict(message)
{'query':message,
 'prediction':list(prediction)}<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15448545587875825, 'fraction_numerical': 0.030065998533365924, 'mean_word_length': 5.719211822660099, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '10990', 'license': 'Apache-2.0', 'n_tokens_mistral': 1481, 'n_tokens_neox': 1406, 'n_words': 287, 'path': 'Copy_of_Q&A_on_PDF_Files.ipynb', 'repo_name': 'rizwandel/Auto_TS', 'size': '4091'}