File size: 3,341 Bytes
eaa29a8
 
723eae6
 
c3c9fc4
8e2b825
eaa29a8
ab0470d
 
 
 
 
8e2b825
c582d76
0a6c830
8e2b825
 
7566cf0
8e2b825
 
 
 
 
c02b23b
eaa29a8
 
 
 
 
 
 
 
 
c02b23b
8e2b825
c02b23b
ab0470d
 
eaa29a8
ab0470d
 
 
eaa29a8
ab0470d
c582d76
ab0470d
 
eaa29a8
ab0470d
 
 
eaa29a8
ab0470d
 
eaa29a8
ab0470d
 
 
 
 
eaa29a8
ab0470d
 
0a6c830
 
 
 
 
 
5dc8391
0a6c830
 
 
 
 
 
 
 
 
 
8e2b825
0a6c830
 
 
 
8e2b825
 
 
 
 
56052ae
8e2b825
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import streamlit as st
from transformers import pipeline
import pandas as pd
import datetime
import base64 
import os

@st.cache(allow_output_mutation = True)
def load_model():
    question_answerer = pipeline('question-answering')
    return question_answerer

#function to convert any dataframe to a csv file
# @st.cache
output_path = "results/df_log_file.csv"
def csv_downloader(df):
    # IMPORTANT: Cache the conversion to prevent computation on every rerun
    res = df.to_csv(index=False,sep="\t").encode('utf-8')
    st.download_button(
    label="Download logs data as CSV separated by tab",
    data=res,
    file_name='df_log_file.csv',
    mime='text/csv')

def load_file():
    """Load text from file"""
    uploaded_file = st.file_uploader("Upload Files",type=['txt'])

    if uploaded_file is not None:
        if uploaded_file.type == "text/plain":
            raw_text = str(uploaded_file.read(),"utf-8")
        return raw_text


st.markdown('![Visitor count](https://shields-io-visitor-counter.herokuapp.com/badge?page=https://share.streamlit.io/https://huggingface.co/spaces/aakashgoel12/nlp1&label=VisitorsCount&labelColor=000000&logo=GitHub&logoColor=FFFFFF&color=1D70B8&style=for-the-badge)')

# Loading Model
question_answerer =load_model()

# App title and description
st.title("Answering questions from text")
st.write("Upload text, pose questions, get answers")

# Load file
st.text("Disclaimer: This app stores user's input for model improvement purposes !!")
raw_text = load_file()
if raw_text != None and raw_text != '':

    # Display text
    with st.expander("See text"):
        st.write(raw_text)

    answer = ''
    question = st.text_input('Ask a question')

    if question != '' and raw_text != '':
        answer = question_answerer({
            'question': question,
            'context': raw_text
        })

        st.write("Answer: {}".format(answer["answer"]))
        st.write("Confidence score: {}".format(round(answer["score"],2)))
        res_df = pd.DataFrame({"TimeStamp":[str(datetime.datetime.now())],\
            "Question":[question],\
            "Input":[str(raw_text)],\
            "Answer":[str(answer["answer"])],\
            "Score":[str(round(answer["score"],2))]})
        res_df.to_csv(output_path, mode='a', index=False, sep="\t", header= not os.path.exists(output_path))
        st.dataframe(pd.read_csv(output_path,sep="\t").tail(4))
        csv_downloader(pd.read_csv(output_path,sep="\t"))


# def csv_downloader(data):
#     csvfile = data.to_csv()
#     b64 = base64.b64encode(csvfile.encode()).decode()
#     new_filename = "results/df_log_file.csv"
#     st.markdown("#### Download File ###")
#     href = f'<a href="data:file/tsv;base64,{b64}" download="{new_filename}">Click Here!!</a>'
#     st.markdown(href,unsafe_allow_html=True)

# log_file_object = open('../logs/log_file.tsv','a')
# log_file_object.write(str(datetime.datetime.now())+'\t'+str(raw_text)+'\t'+str(answer["answer"])+'\t'+str(answer["score"]))
# log_file_object.write('\n')
# log_file_object.close()        


# @st.cache(allow_output_mutation=True)
# def Pageviews():
#     return []

# pageviews=Pageviews()
# pageviews.append('dummy')
# try:
#     st.markdown('Page viewed = {} times.'.format(len(pageviews)))
# except ValueError:
#     st.markdown('Page viewed = {} times.'.format(1))