Spaces:
Runtime error
Runtime error
Update incompletesentencefinder.py
Browse files- incompletesentencefinder.py +25 -21
incompletesentencefinder.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
|
| 2 |
import openai
|
| 3 |
import gradio as gr
|
| 4 |
|
|
@@ -17,8 +17,7 @@ class IncompleteSentenceFinder:
|
|
| 17 |
Args:
|
| 18 |
api_key (str): Your OpenAI API key.
|
| 19 |
"""
|
| 20 |
-
|
| 21 |
-
pass
|
| 22 |
|
| 23 |
def _check_incomplete_sentence(self, text: str) -> str:
|
| 24 |
|
|
@@ -31,19 +30,20 @@ class IncompleteSentenceFinder:
|
|
| 31 |
Returns:
|
| 32 |
str: Incomplete sentences identified by GPT-3.
|
| 33 |
"""
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
)
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
incomplete_sentences = response.choices[0].text.strip()
|
| 43 |
-
|
| 44 |
-
print("incomplete_sentences Extracted Successfully!")
|
| 45 |
-
|
| 46 |
-
return incomplete_sentences
|
| 47 |
|
| 48 |
def get_incomplete_sentence(self,pdf_file) -> str:
|
| 49 |
|
|
@@ -54,13 +54,17 @@ class IncompleteSentenceFinder:
|
|
| 54 |
str: Incomplete sentences identified by GPT-3.
|
| 55 |
"""
|
| 56 |
try:
|
| 57 |
-
# Open the PDF
|
| 58 |
-
|
|
|
|
| 59 |
incomplete_text = ""
|
| 60 |
-
|
| 61 |
-
#
|
| 62 |
-
for
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
| 64 |
incomplete_text += self._check_incomplete_sentence(text)
|
| 65 |
|
| 66 |
return incomplete_text
|
|
|
|
| 1 |
+
from PyPDF2 import PdfReader
|
| 2 |
import openai
|
| 3 |
import gradio as gr
|
| 4 |
|
|
|
|
| 17 |
Args:
|
| 18 |
api_key (str): Your OpenAI API key.
|
| 19 |
"""
|
| 20 |
+
self.client = OpenAI()
|
|
|
|
| 21 |
|
| 22 |
def _check_incomplete_sentence(self, text: str) -> str:
|
| 23 |
|
|
|
|
| 30 |
Returns:
|
| 31 |
str: Incomplete sentences identified by GPT-3.
|
| 32 |
"""
|
| 33 |
+
conversation = [
|
| 34 |
+
{"role": "system", "content": "You are a helpful incomplete sentences finder"},
|
| 35 |
+
{"role": "user", "content": f"""list out the incomplete sentences in the following text: {text}"""}
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
# Call OpenAI GPT-3.5-turbo
|
| 39 |
+
chat_completion = self.client.chat.completions.create(
|
| 40 |
+
model = "gpt-3.5-turbo",
|
| 41 |
+
messages = conversation,
|
| 42 |
+
max_tokens=500,
|
| 43 |
+
temperature=0
|
| 44 |
)
|
| 45 |
+
response = chat_completion.choices[0].message.content
|
| 46 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
def get_incomplete_sentence(self,pdf_file) -> str:
|
| 49 |
|
|
|
|
| 54 |
str: Incomplete sentences identified by GPT-3.
|
| 55 |
"""
|
| 56 |
try:
|
| 57 |
+
# Open the multi-page PDF using PdfReaderer
|
| 58 |
+
pdf = PdfReader(pdf_file_path)
|
| 59 |
+
|
| 60 |
incomplete_text = ""
|
| 61 |
+
|
| 62 |
+
# Extract text from each page and pass it to the process_text function
|
| 63 |
+
for page_number in range(len(pdf.pages)):
|
| 64 |
+
|
| 65 |
+
# Extract text from the page
|
| 66 |
+
page = pdf.pages[page_number]
|
| 67 |
+
text = page.extract_text()
|
| 68 |
incomplete_text += self._check_incomplete_sentence(text)
|
| 69 |
|
| 70 |
return incomplete_text
|