File size: 1,403 Bytes
5ddf6ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pathlib
import base64
import streamlit as st
from pathlib import Path
import process

st.title("PDF Question Answer")

# check directory
SAVE_FOLDER = './assets'
pathlib.Path(SAVE_FOLDER).mkdir(parents=True, exist_ok=True) 

def display_pdf(file):
	with open(file, "rb") as f:
		base64_pdf = base64.b64encode(f.read()).decode('utf-8')
	pdf_display = F'<iframe src="data:application/pdf;base64,{base64_pdf}" width="700" height="700" type="application/pdf"></iframe>'
	st.markdown(pdf_display, unsafe_allow_html=True)

# Input file upload
pdf_uploaded = st.file_uploader(label = "Upload PDF File", type=["pdf"])

if pdf_uploaded:
	save_path = Path(SAVE_FOLDER, pdf_uploaded.name)
	with open(save_path, mode='wb') as w:
		w.write(pdf_uploaded.getvalue())

	if save_path.exists():
		st.success(f'File {pdf_uploaded.name} is successfully saved!')
		display_pdf(save_path)

		# Display input Question
		st.markdown("**Please fill the below form :**")
		with st.form(key="Form :", clear_on_submit = False):
			question_input      = st.text_input('Question : ')
			token_openai_input  = st.text_input('Token OpenAI : ')
			submit_btn = st.form_submit_button(label='Submit')

		if submit_btn:
			answer = process.main_process(
						pdf_path=f'{SAVE_FOLDER}/{pdf_uploaded.name}',
						question= question_input,
						openai_key=token_openai_input
					)
			st.markdown("Answer : ")
			st.markdown(answer)