arjunanand13 commited on
Commit
137b217
·
verified ·
1 Parent(s): 9525474

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +20 -3
handler.py CHANGED
@@ -6,15 +6,32 @@ from io import BytesIO
6
  from PIL import Image
7
  import requests
8
  from transformers import AutoModelForCausalLM, AutoProcessor
9
- def install(package):
10
- subprocess.check_call([sys.executable, "-m", "pip", "install", "--no-warn-script-location", package])
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  class EndpointHandler:
13
  def __init__(self, path=""):
 
 
 
 
 
14
  required_packages = ['timm', 'einops', 'flash-attn', 'Pillow']
15
  for package in required_packages:
16
  try:
17
- install(package)
18
  print(f"Successfully installed {package}")
19
  except Exception as e:
20
  print(f"Failed to install {package}: {str(e)}")
 
6
  from PIL import Image
7
  import requests
8
  from transformers import AutoModelForCausalLM, AutoProcessor
9
+ import os
10
+
11
+ def create_venv(venv_path="env"):
12
+ """Creates a virtual environment."""
13
+ if not os.path.exists(venv_path):
14
+ print(f"[INFO] Creating virtual environment at {venv_path}")
15
+ subprocess.check_call([sys.executable, "-m", "venv", venv_path])
16
+ else:
17
+ print(f"[INFO] Virtual environment already exists at {venv_path}")
18
+
19
+ def install(package, venv_path="env"):
20
+ """Installs a package inside the virtual environment."""
21
+ pip_path = os.path.join(venv_path, "bin", "pip")
22
+ subprocess.check_call([pip_path, "install", "--no-warn-script-location", package])
23
 
24
  class EndpointHandler:
25
  def __init__(self, path=""):
26
+ # Step 1: Create the virtual environment
27
+ self.venv_path = "env"
28
+ create_venv(self.venv_path)
29
+
30
+ # Step 2: Install required packages inside the virtual environment
31
  required_packages = ['timm', 'einops', 'flash-attn', 'Pillow']
32
  for package in required_packages:
33
  try:
34
+ install(package, self.venv_path)
35
  print(f"Successfully installed {package}")
36
  except Exception as e:
37
  print(f"Failed to install {package}: {str(e)}")