Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| from transformers import pipeline | |
| import pandas as pd | |
| import datetime | |
| import base64 | |
| import os | |
| 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('') | |
| # 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)) | |