Spaces:
Running
Running
File size: 3,931 Bytes
a52afff 684c7c1 588b16e 0bf7883 588b16e 8720566 72c95d6 588b16e cd254ee a52afff cd254ee 7a4d3b0 588b16e 0bf7883 588b16e a52afff 591af0b a52afff 588b16e a52afff 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
from PyPDF2 import PdfReader
from openai import AzureOpenAI
import gradio as gr
import os
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_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 _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.
"""
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 incomplete sentences finder"},
{"role": "user", "content": f"""list out the incomplete sentences in the following text: {text}"""}
]
# Call OpenAI GPT-3.5-turbo
chat_completion = client.chat.completions.create(
model = "ChatGPT",
messages = conversation,
max_tokens=1000,
temperature=0
)
response = chat_completion.choices[0].message.content
return response
def get_incomplete_sentence(self,pdf_file_path) -> str:
"""
Extract text from the PDF document and find incomplete sentences.
Returns:
str: Incomplete sentences identified by GPT-3.
"""
try:
# Open the multi-page PDF using PdfReaderer
pdf = PdfReader(pdf_file_path.name)
incomplete_text = ""
# Extract text from each page and pass it to the process_text function
for page_number in range(len(pdf.pages)):
# Extract text from the page
page = pdf.pages[page_number]
text = page.extract_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)
|