Contract_Management / incompletesentencefinder.py
robertselvam's picture
Update incompletesentencefinder.py
5b0b305
raw
history blame
3.25 kB
import fitz # PyMuPDF
import openai
import gradio as gr
class IncompleteSentenceFinder:
"""
This class finds and displays incomplete sentences in a PDF document using OpenAI's GPT-3.
Args:
api_key (str): Your OpenAI API key.
"""
def __init__(self):
"""
Initialize the IncompleteSentenceFinder with the PDF file and OpenAI API key.
Args:
api_key (str): Your OpenAI API key.
"""
# openai.api_key = openai_api_key
pass
def _check_incomplete_sentence(self, text: str) -> str:
"""
Use OpenAI's GPT-3 to identify incomplete sentences in the given text.
Args:
text (str): Text to check for incomplete sentences.
Returns:
str: Incomplete sentences identified by GPT-3.
"""
# Create a request to OpenAI's GPT-3 engine to identify incomplete sentences.
response = openai.Completion.create(
engine="text-davinci-003",
prompt=f"list out the incomplete sentences in the following text:\n{text}",
max_tokens=1000,
)
# Extract and strip the text of identified incomplete sentences from the GPT-3 response.
incomplete_sentences = response.choices[0].text.strip()
print("incomplete_sentences Extracted Successfully!")
return incomplete_sentences
def get_incomplete_sentence(self,pdf_file) -> str:
"""
Extract text from the PDF document and find incomplete sentences.
Returns:
str: Incomplete sentences identified by GPT-3.
"""
try:
# Open the PDF file using PyMuPDF's fitz library
doc = fitz.open(pdf_file.name)
incomplete_text = ""
# Iterate through each page in the PDF document and extract the text
for page in doc:
text = page.get_text()
incomplete_text += self._check_incomplete_sentence(text)
return incomplete_text
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")
incomplete_sentence_btn = 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)
incomplete_sentence_btn.click(self.get_incomplete_sentence,upload_button1,headings)