0504ankitsharma commited on
Commit
3587fe0
·
verified ·
1 Parent(s): 8c44cf7

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +17 -4
app/main.py CHANGED
@@ -15,19 +15,32 @@ from langchain_community.embeddings import HuggingFaceBgeEmbeddings
15
  import nltk # Importing NLTK
16
  import time
17
 
18
- # Configure NLTK data directory
19
- nltk_data_path = os.path.join(os.getcwd(), 'nltk_data') # Use a writable directory
 
 
 
 
 
 
 
 
20
  nltk.data.path.append(nltk_data_path)
21
 
22
  # Ensure the directory exists
23
- if not os.path.exists(nltk_data_path):
24
- os.makedirs(nltk_data_path)
 
 
 
25
 
26
  # Download required NLTK resources
27
  try:
28
  nltk.download('punkt', download_dir=nltk_data_path)
 
29
  except Exception as e:
30
  print(f"Error downloading NLTK resources: {e}")
 
31
 
32
  def clean_response(response):
33
  # Remove any leading/trailing whitespace, including newlines
 
15
  import nltk # Importing NLTK
16
  import time
17
 
18
+ # Set writable paths for cache and data
19
+ cache_dir = '/tmp'
20
+ nltk_data_path = os.path.join(cache_dir, 'nltk_data')
21
+
22
+ # Configure NLTK and other library paths
23
+ os.environ['TRANSFORMERS_CACHE'] = os.path.join(cache_dir, 'transformers_cache')
24
+ os.environ['HF_HOME'] = os.path.join(cache_dir, 'huggingface')
25
+ os.environ['XDG_CACHE_HOME'] = cache_dir
26
+
27
+ # Add NLTK data path
28
  nltk.data.path.append(nltk_data_path)
29
 
30
  # Ensure the directory exists
31
+ try:
32
+ os.makedirs(nltk_data_path, exist_ok=True)
33
+ except OSError as e:
34
+ print(f"Error creating directory {nltk_data_path}: {e}")
35
+ raise
36
 
37
  # Download required NLTK resources
38
  try:
39
  nltk.download('punkt', download_dir=nltk_data_path)
40
+ print("NLTK 'punkt' resource downloaded successfully.")
41
  except Exception as e:
42
  print(f"Error downloading NLTK resources: {e}")
43
+ raise
44
 
45
  def clean_response(response):
46
  # Remove any leading/trailing whitespace, including newlines