from PyPDF2 import PdfReader from openai import AzureOpenAI import gradio as gr import os class IncorrectSentenceFinder: """ This class finds and displays grammatically incorrect sentences in a PDF document using OpenAI's GPT-3. Args: pdf_file (str): The path to the PDF file. """ def __init__(self): """ Initialize the IncorrectSentenceFinder with the OpenAI API key. """ # openai.api_type = os.getenv['api_type'] # openai.api_base = os.getenv['api_base'] # openai.api_version = os.getenv['api_version'] # openai.api_key = os.getenv['api_key'] pass def _find_incorrect_sentence(self, text: str) -> str: """ Use OpenAI's GPT-3 to identify grammatically incorrect sentences in the given text. Args: text (str): Text to check for grammatical errors. Returns: str: Grammatically incorrect sentences identified by GPT-3. """ # Create a request to OpenAI's GPT-3 engine to identify grammatically incorrect sentences. client = AzureOpenAI(api_key=os.getenv("AZURE_OPENAI_KEY"), api_version="2023-07-01-preview", azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT") ) conversation = [ {"role": "system", "content": "You are a helpful Error sentence finder."}, {"role": "user", "content": f"""list out the grammatical error sentence in the given text:\n{text} format: error sentence:finded error sentence """} ] # Call OpenAI GPT-3.5-turbo chat_completion = client.chat.completions.create( model = "GPT-3", messages = conversation, max_tokens=1000, temperature=0 ) response = chat_completion.choices[0].message.content return response def get_incorrect_sentence(self, pdf_file: str) -> str: """ Extract text from the PDF document and find grammatically incorrect sentences. Returns: str: Grammatically incorrect sentences identified by GPT-3. """ try: # Open the PDF file using PyMuPDF's fitz library doc =PdfReader(pdf_file.name) incorrect_sentences = '' # Iterate through each page in the PDF document and extract the text for page_number in range(len(doc.pages)): # Extract text from the page page = doc.pages[page_number] text = page.extract_text() incorrect_sentences += self._find_incorrect_sentence(text) return incorrect_sentences except Exception as e: print(f"An error occurred: {str(e)}") def file_output_fnn(self,file_path): file_path = file_path.name return file_path def gradio_interface(self): with gr.Blocks(css="style.css",theme='xiaobaiyuan/theme_brief') as demo: with gr.Row(elem_id = "col-container",scale=0.80): with gr.Column(elem_id = "col-container",scale=0.80): file1 = gr.File(label="File",elem_classes="filenameshow") with gr.Column(elem_id = "col-container",scale=0.20): upload_button1 = gr.UploadButton( "Browse File",file_types=[".txt", ".pdf", ".doc", ".docx",".json",".csv"], elem_classes="uploadbutton") incorrect_sentence = gr.Button("Get Headings",elem_classes="uploadbutton") with gr.Row(elem_id = "col-container",scale=0.60): headings = gr.Textbox(label = "Headings") upload_button1.upload(self.file_output_fnn,upload_button1,file1) incorrect_sentence.click(self.get_incorrect_sentence,upload_button1,headings)