Neurolingua commited on
Commit
dc30935
·
verified ·
1 Parent(s): 0daaf05

Update other_function.py

Browse files
Files changed (1) hide show
  1. other_function.py +31 -3
other_function.py CHANGED
@@ -10,7 +10,7 @@ import os
10
  from pypdf import PdfReader
11
  from ai71 import AI71
12
  import os
13
-
14
  import pandas as pd
15
 
16
  from inference_sdk import InferenceHTTPClient
@@ -19,6 +19,7 @@ UPLOAD_FOLDER = '/code/uploads'
19
  if not os.path.exists(UPLOAD_FOLDER):
20
  os.makedirs(UPLOAD_FOLDER)
21
 
 
22
  AI71_API_KEY = os.environ.get('AI71_API_KEY')
23
  def generate_response(query,chat_history):
24
  response = ''
@@ -169,7 +170,7 @@ def download_and_save_as_txt(url, account_sid, auth_token):
169
  file.write(response.content)
170
 
171
  print(f"Media downloaded successfully and saved as {txt_filepath}")
172
- return txt_filepath
173
 
174
  except requests.exceptions.HTTPError as err:
175
  print(f"HTTP error occurred: {err}")
@@ -193,4 +194,31 @@ def download_file(url, extension):
193
  print(f"HTTP error occurred: {err}")
194
  except Exception as err:
195
  print(f"An error occurred: {err}")
196
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  from pypdf import PdfReader
11
  from ai71 import AI71
12
  import os
13
+ import PyPDF2
14
  import pandas as pd
15
 
16
  from inference_sdk import InferenceHTTPClient
 
19
  if not os.path.exists(UPLOAD_FOLDER):
20
  os.makedirs(UPLOAD_FOLDER)
21
 
22
+ pdf_text=''
23
  AI71_API_KEY = os.environ.get('AI71_API_KEY')
24
  def generate_response(query,chat_history):
25
  response = ''
 
170
  file.write(response.content)
171
 
172
  print(f"Media downloaded successfully and saved as {txt_filepath}")
173
+ return txt_filepath,extract_text_from_pdf(pdf_path)
174
 
175
  except requests.exceptions.HTTPError as err:
176
  print(f"HTTP error occurred: {err}")
 
194
  print(f"HTTP error occurred: {err}")
195
  except Exception as err:
196
  print(f"An error occurred: {err}")
197
+ return None
198
+
199
+
200
+ def extract_text_from_pdf(pdf_path):
201
+ global pdf_text
202
+ with open(pdf_path, 'rb') as file:
203
+ reader = PyPDF2.PdfReader(file)
204
+ pdf_text = ''
205
+ for page_num in range(len(reader.pages)):
206
+ page = reader.pages[page_num]
207
+ pdf_text += page.extract_text()
208
+ return pdf_text
209
+
210
+
211
+ def respond_pdf(extracted_text,query):
212
+ if
213
+ res = ''
214
+ for chunk in AI71(AI71_API_KEY).chat.completions.create(
215
+ model="tiiuae/falcon-11b",
216
+ messages=[
217
+ {"role": "system", "content": "You are a pdf answering assistant."},
218
+ {"role": "user", "content": f"Content:{extracted_text},Query:{query}"},
219
+ ],
220
+ stream=True,
221
+ ):
222
+ if chunk.choices[0].delta.content:
223
+ res += chunk.choices[0].delta.content
224
+ return ( res.replace("User:",'').strip())