Alimustoofaa/PDF_QuestionAnswer
0
1import pathlib2import base643import streamlit as st4from pathlib import Path5import process6 7st.title("PDF Question Answer")8 9# check directory10SAVE_FOLDER = './assets'11pathlib.Path(SAVE_FOLDER).mkdir(parents=True, exist_ok=True) 12 13def display_pdf(file):14 with open(file, "rb") as f:15 base64_pdf = base64.b64encode(f.read()).decode('utf-8')16 pdf_display = F'<iframe src="data:application/pdf;base64,{base64_pdf}" width="700" height="700" type="application/pdf"></iframe>'17 st.markdown(pdf_display, unsafe_allow_html=True)18 19# Input file upload20pdf_uploaded = st.file_uploader(label = "Upload PDF File", type=["pdf"])21 22if pdf_uploaded:23 save_path = Path(SAVE_FOLDER, pdf_uploaded.name)24 with open(save_path, mode='wb') as w:25 w.write(pdf_uploaded.getvalue())26 27 if save_path.exists():28 st.success(f'File {pdf_uploaded.name} is successfully saved!')29 display_pdf(save_path)30 31 # Display input Question32 st.markdown("**Please fill the below form :**")33 with st.form(key="Form :", clear_on_submit = False):34 question_input = st.text_input('Question : ')35 token_openai_input = st.text_input('Token OpenAI : ')36 submit_btn = st.form_submit_button(label='Submit')37 38 if submit_btn:39 answer = process.main_process(40 pdf_path=f'{SAVE_FOLDER}/{pdf_uploaded.name}',41 question= question_input,42 openai_key=token_openai_input43 )44 st.markdown("Answer : ")45 st.markdown(answer)