Sirapatsorn commited on
Commit
6ec5122
·
verified ·
1 Parent(s): ce5cc2b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py CHANGED
@@ -1,3 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import torch
3
  import os
 
1
+ import os
2
+ import subprocess
3
+ import sys
4
+ import importlib
5
+
6
+ def check_and_install_package(package_name):
7
+ try:
8
+ importlib.import_module(package_name)
9
+ print(f"{package_name} is already installed")
10
+ except ImportError:
11
+ print(f"Installing {package_name}...")
12
+ subprocess.check_call([sys.executable, '-m', 'pip', 'install', package_name])
13
+
14
+ def install_requirements():
15
+ try:
16
+ # Upgrade pip first
17
+ subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--upgrade', 'pip'])
18
+
19
+ # Install requirements from file
20
+ requirements_path = os.path.join(os.path.dirname(__file__), 'requirement.txt')
21
+ if os.path.exists(requirements_path):
22
+ print("Installing requirements from requirements.txt...")
23
+ subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-r', requirements_path])
24
+ print("Successfully installed requirements")
25
+ else:
26
+ print(f"requirements.txt not found at {requirements_path}")
27
+
28
+ # Explicitly check and install critical packages
29
+ critical_packages = ['sentencepiece', 'transformers', 'torch', 'gradio']
30
+ for package in critical_packages:
31
+ check_and_install_package(package)
32
+
33
+ except subprocess.CalledProcessError as e:
34
+ print(f"Error installing requirements: {e}")
35
+ raise
36
+ except Exception as e:
37
+ print(f"Unexpected error during installation: {e}")
38
+ raise
39
+
40
+ # Install requirements first
41
+ print("Starting installation of requirements...")
42
+ install_requirements()
43
+ print("Finished installing requirements")
44
+
45
  import gradio as gr
46
  import torch
47
  import os