import gradio as gr
import os
import time
import shutil
import base64
from pdfquery import PDFQuery
pquery = PDFQuery()
def openai_create(s):
global pquery
return pquery.ask(s)
def chatgpt_clone(input, history, chatbot):
if input == "":
return chatbot, history, ""
history = history or []
s = list(sum(history, ()))
s.append(input)
inp = ' '.join(s)
output = openai_create(input)
history.append((inp, output))
chatbot.append((input, output))
return chatbot, history, ""
title_html = f"
Chat With Pdf
"
gr_L1 = lambda: gr.Row().style()
gr_L2 = lambda scale, elem_id: gr.Column(scale=scale, elem_id=elem_id)
def pdf_to_markdown(file_obj):
try:
shutil.rmtree('./private_upload/')
except:
pass
time_tag = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
os.makedirs(f'private_upload/{time_tag}', exist_ok=True)
file_name = os.path.basename(file_obj.name)
destination = f'private_upload/{time_tag}/{file_name}'
shutil.copy(file_obj.name, destination)
global pquery
pquery.ingest(destination)
with open(destination, "rb") as f:
pdf = base64.b64encode(f.read()).decode('utf-8')
pdf_display = f'