Spaces:
Sleeping
Sleeping
File size: 1,267 Bytes
6901ae8 b79ae95 6901ae8 329a527 6901ae8 46c86b1 b79ae95 9fb18d3 b79ae95 6901ae8 329a527 6901ae8 b7a9dd4 b79ae95 b7a9dd4 67d2ddc b7a9dd4 6901ae8 b7a9dd4 5c0545e | 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 |
from openai import OpenAI
import gradio
import pdfplumber
import os
from io import BytesIO
def read_PDF_2(fileobj):
print("readPDF called")
pdf_text = ""
pdf_file = BytesIO(fileobj)
with pdfplumber.open(pdf_file) as pdf:
for page in pdf.pages:
pdf_text += page.extract_text()
return pdf_text
def askPDF(upload_PDF, prompt):
print("askPDF called")
client = OpenAI()
pdf_text = read_PDF_2(upload_PDF)
systemMessage = '"""' + pdf_text + '"""' + \
'\nAnswer all questions according to the text delimited in tripple quotes above'
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": systemMessage},
{"role": "user", "content": prompt}
]
)
return (completion.choices[0].message).content
# gradio deployment
demo = gradio.Interface(fn=askPDF,
inputs=[gradio.File(type = "binary",file_types=['.pdf']), 'text'],
outputs="text",
title = "Ask questions from pdf",
description = "Limitations: it works for small files, breaks for long PDFs."
)
demo.launch()
|