Baskar2005 commited on
Commit
d8b2662
1 Parent(s): e352259

Update incorrect_sentence_finder.py

Browse files
Files changed (1) hide show
  1. incorrect_sentence_finder.py +24 -15
incorrect_sentence_finder.py CHANGED
@@ -1,4 +1,4 @@
1
- import fitz # PyMuPDF
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
- pass
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
- response = openai.Completion.create(
32
- engine="text-davinci-003",
33
- prompt=f"list out the grammatical error sentence in the given text:\n{text}",
34
- temperature=0,
35
- max_tokens=1000,
 
 
 
 
 
 
 
 
 
36
  )
37
-
38
- # Extract and strip the text of identified grammatically incorrect sentences from the GPT-3 response.
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 = fitz.open(pdf_file.name)
52
  incorrect_sentences = ''
53
  # Iterate through each page in the PDF document and extract the text
54
- for page in doc:
55
- text = page.get_text()
56
- incorrect_sentences += self._find_incorrect_sentence(text)
 
 
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: