Spaces:
Runtime error
Runtime error
| from openai import OpenAI | |
| import gradio | |
| import shutil | |
| import pdfplumber | |
| import os | |
| def read_PDF_2(fileobj): | |
| print("readPDF called") | |
| pdf_text = "" | |
| with pdfplumber.open(fileobj) 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(file_types=['.pdf']), 'text'], | |
| outputs="text", | |
| ) | |
| demo.launch() | |