Spaces:
Sleeping
Sleeping
Update incorrect_sentence_finder.py
Browse files- incorrect_sentence_finder.py +24 -15
incorrect_sentence_finder.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
|
| 2 |
import openai
|
| 3 |
import gradio as gr
|
| 4 |
|
|
@@ -15,7 +15,7 @@ class IncorrectSentenceFinder:
|
|
| 15 |
Initialize the IncorrectSentenceFinder with the OpenAI API key.
|
| 16 |
"""
|
| 17 |
# openai.api_key = openai_api_key
|
| 18 |
-
|
| 19 |
|
| 20 |
def _find_incorrect_sentence(self, text: str) -> str:
|
| 21 |
"""
|
|
@@ -28,16 +28,23 @@ class IncorrectSentenceFinder:
|
|
| 28 |
str: Grammatically incorrect sentences identified by GPT-3.
|
| 29 |
"""
|
| 30 |
# Create a request to OpenAI's GPT-3 engine to identify grammatically incorrect sentences.
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
)
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
incorrect_sentences = response.choices[0].text.strip()
|
| 40 |
-
return incorrect_sentences
|
| 41 |
|
| 42 |
def get_incorrect_sentence(self, pdf_file: str) -> str:
|
| 43 |
"""
|
|
@@ -48,12 +55,14 @@ class IncorrectSentenceFinder:
|
|
| 48 |
"""
|
| 49 |
try:
|
| 50 |
# Open the PDF file using PyMuPDF's fitz library
|
| 51 |
-
doc =
|
| 52 |
incorrect_sentences = ''
|
| 53 |
# Iterate through each page in the PDF document and extract the text
|
| 54 |
-
for
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
return incorrect_sentences
|
| 58 |
|
| 59 |
except Exception as e:
|
|
|
|
| 1 |
+
from PyPDF2 import PdfReader
|
| 2 |
import openai
|
| 3 |
import gradio as gr
|
| 4 |
|
|
|
|
| 15 |
Initialize the IncorrectSentenceFinder with the OpenAI API key.
|
| 16 |
"""
|
| 17 |
# openai.api_key = openai_api_key
|
| 18 |
+
self.client=OpenAI()
|
| 19 |
|
| 20 |
def _find_incorrect_sentence(self, text: str) -> str:
|
| 21 |
"""
|
|
|
|
| 28 |
str: Grammatically incorrect sentences identified by GPT-3.
|
| 29 |
"""
|
| 30 |
# Create a request to OpenAI's GPT-3 engine to identify grammatically incorrect sentences.
|
| 31 |
+
conversation = [
|
| 32 |
+
{"role": "system", "content": "You are a helpful Error sentence finder."},
|
| 33 |
+
{"role": "user", "content": f"""list out the grammatical error sentence in the given text:\n{text}
|
| 34 |
+
format:
|
| 35 |
+
error sentence:finded error sentence
|
| 36 |
+
"""}
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
# Call OpenAI GPT-3.5-turbo
|
| 40 |
+
chat_completion =client.chat.completions.create(
|
| 41 |
+
model = "gpt-3.5-turbo",
|
| 42 |
+
messages = conversation,
|
| 43 |
+
max_tokens=500,
|
| 44 |
+
temperature=0
|
| 45 |
)
|
| 46 |
+
response = chat_completion.choices[0].message.content
|
| 47 |
+
return response
|
|
|
|
|
|
|
| 48 |
|
| 49 |
def get_incorrect_sentence(self, pdf_file: str) -> str:
|
| 50 |
"""
|
|
|
|
| 55 |
"""
|
| 56 |
try:
|
| 57 |
# Open the PDF file using PyMuPDF's fitz library
|
| 58 |
+
doc =PdfReader(pdf_file.name)
|
| 59 |
incorrect_sentences = ''
|
| 60 |
# Iterate through each page in the PDF document and extract the text
|
| 61 |
+
for page_number in range(len(doc.pages)):
|
| 62 |
+
# Extract text from the page
|
| 63 |
+
page = doc.pages[page_number]
|
| 64 |
+
text = page.extract_text()
|
| 65 |
+
incorrect_sentences += self._find_incorrect_sentence(text)
|
| 66 |
return incorrect_sentences
|
| 67 |
|
| 68 |
except Exception as e:
|