Spaces:
Running
Running
File size: 3,199 Bytes
588b16e 86fecb6 588b16e |
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
import fitz # PyMuPDF
import openai
import gradio as gr
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_key = openai_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.
response = openai.Completion.create(
engine="text-davinci-003",
prompt=f"list out the grammatical error sentence in the given text:\n{text}",
temperature=0,
max_tokens=1000,
)
# Extract and strip the text of identified grammatically incorrect sentences from the GPT-3 response.
incorrect_sentences = response.choices[0].text.strip()
return incorrect_sentences
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 = fitz.open(pdf_file.name)
incorrect_sentences = ''
# Iterate through each page in the PDF document and extract the text
for page in doc:
text = page.get_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)
|