Upload folder using huggingface_hub
Browse files
.ipynb_checkpoints/win_run_app-checkpoint.py
ADDED
@@ -0,0 +1,159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import time
|
4 |
+
import traceback
|
5 |
+
import webbrowser
|
6 |
+
|
7 |
+
# uncomment below to ensure CPU install only uses CPU
|
8 |
+
# os.environ['CUDA_VISIBLE_DEVICES'] = ''
|
9 |
+
|
10 |
+
print('__file__: %s' % __file__)
|
11 |
+
path1 = os.path.dirname(os.path.abspath(__file__))
|
12 |
+
sys.path.append(path1)
|
13 |
+
base_path = os.path.dirname(path1)
|
14 |
+
sys.path.append(base_path)
|
15 |
+
os.environ['PYTHONPATH'] = path1
|
16 |
+
print('path1', path1, flush=True)
|
17 |
+
|
18 |
+
os.environ['NLTK_DATA'] = os.path.join(base_path, './nltk_data')
|
19 |
+
path_list = [os.environ['PATH'],
|
20 |
+
os.path.join(base_path, 'poppler/Library/bin/'),
|
21 |
+
os.path.join(base_path, 'poppler/Library/lib/'),
|
22 |
+
os.path.join(base_path, 'Tesseract-OCR'),
|
23 |
+
os.path.join(base_path, 'ms-playwright'),
|
24 |
+
os.path.join(base_path, 'ms-playwright/chromium-1076/chrome-win'),
|
25 |
+
os.path.join(base_path, 'ms-playwright/ffmpeg-1009'),
|
26 |
+
os.path.join(base_path, 'ms-playwright/firefox-1422/firefox'),
|
27 |
+
os.path.join(base_path, 'ms-playwright/webkit-1883'),
|
28 |
+
os.path.join(base_path, 'rubberband/')]
|
29 |
+
os.environ['PATH'] = ';'.join(path_list)
|
30 |
+
print(os.environ['PATH'])
|
31 |
+
|
32 |
+
import shutil, errno
|
33 |
+
|
34 |
+
|
35 |
+
def copy_tree(src, dst):
|
36 |
+
try:
|
37 |
+
shutil.copytree(src, dst)
|
38 |
+
except OSError as exc: # python >2.5
|
39 |
+
if exc.errno in (errno.ENOTDIR, errno.EINVAL):
|
40 |
+
shutil.copy(src, dst)
|
41 |
+
else: raise
|
42 |
+
|
43 |
+
|
44 |
+
def setup_paths():
|
45 |
+
for sub in ['src', 'iterators', 'gradio_utils', 'metrics', 'models', '.']:
|
46 |
+
path2 = os.path.join(base_path, '..', sub)
|
47 |
+
if os.path.isdir(path2):
|
48 |
+
if sub == 'models' and os.path.isfile(os.path.join(path2, 'human.jpg')):
|
49 |
+
os.environ['H2OGPT_MODEL_BASE'] = path2
|
50 |
+
sys.path.append(path2)
|
51 |
+
print(path2, flush=True)
|
52 |
+
|
53 |
+
path2 = os.path.join(path1, '..', sub)
|
54 |
+
if os.path.isdir(path2):
|
55 |
+
if sub == 'models' and os.path.isfile(os.path.join(path2, 'human.jpg')):
|
56 |
+
os.environ['H2OGPT_MODEL_BASE'] = path2
|
57 |
+
sys.path.append(path2)
|
58 |
+
print(path2, flush=True)
|
59 |
+
|
60 |
+
# for app, avoid forbidden for web access
|
61 |
+
if os.getenv('H2OGPT_MODEL_BASE'):
|
62 |
+
base0 = os.environ['H2OGPT_MODEL_BASE']
|
63 |
+
if 'Programs' in os.environ['H2OGPT_MODEL_BASE']:
|
64 |
+
os.environ['H2OGPT_MODEL_BASE'] = os.environ['H2OGPT_MODEL_BASE'].replace('Programs', 'Temp/gradio/')
|
65 |
+
shutil.rmtree(os.environ['H2OGPT_MODEL_BASE'])
|
66 |
+
if os.path.isfile(os.path.join(base0, 'human.jpg')):
|
67 |
+
copy_tree(base0, os.environ['H2OGPT_MODEL_BASE'])
|
68 |
+
|
69 |
+
|
70 |
+
from importlib.metadata import distribution, PackageNotFoundError
|
71 |
+
|
72 |
+
try:
|
73 |
+
dtorch = distribution('torch')
|
74 |
+
assert dtorch is not None
|
75 |
+
have_torch = True
|
76 |
+
torch_version = dtorch.version
|
77 |
+
except (PackageNotFoundError, AssertionError):
|
78 |
+
have_torch = False
|
79 |
+
torch_version = ''
|
80 |
+
|
81 |
+
|
82 |
+
def _main():
|
83 |
+
setup_paths()
|
84 |
+
os.environ['h2ogpt_block_gradio_exit'] = 'False'
|
85 |
+
os.environ['h2ogpt_score_model'] = ''
|
86 |
+
|
87 |
+
try:
|
88 |
+
from pynvml import nvmlInit, nvmlDeviceGetCount
|
89 |
+
nvmlInit()
|
90 |
+
deviceCount = nvmlDeviceGetCount()
|
91 |
+
except Exception as e:
|
92 |
+
print("No GPUs detected by NVML: %s" % str(e))
|
93 |
+
deviceCount = 0
|
94 |
+
|
95 |
+
need_get_gpu_torch = False
|
96 |
+
if have_torch and deviceCount > 0:
|
97 |
+
if '+cu' not in torch_version:
|
98 |
+
need_get_gpu_torch = True
|
99 |
+
elif not have_torch and deviceCount > 0:
|
100 |
+
need_get_gpu_torch = True
|
101 |
+
|
102 |
+
print("Torch Status: have torch: %s need get gpu torch: %s CVD: %s GPUs: %s" % (have_torch, need_get_gpu_torch, os.getenv('CUDA_VISIBLE_DEVICES'), deviceCount))
|
103 |
+
|
104 |
+
auto_install_torch_gpu = False
|
105 |
+
|
106 |
+
import sys
|
107 |
+
if auto_install_torch_gpu and (not have_torch or need_get_gpu_torch) and sys.platform == "win32":
|
108 |
+
print("Installing Torch")
|
109 |
+
# for one-click, don't have torch installed, install now
|
110 |
+
import subprocess
|
111 |
+
import sys
|
112 |
+
|
113 |
+
def install(package):
|
114 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
115 |
+
|
116 |
+
if os.getenv('TORCH_WHEEL'):
|
117 |
+
print("Installing Torch from %s" % os.getenv('TORCH_WHEEL'))
|
118 |
+
install(os.getenv('TORCH_WHEEL'))
|
119 |
+
else:
|
120 |
+
if need_get_gpu_torch:
|
121 |
+
wheel_file = "https://h2o-release.s3.amazonaws.com/h2ogpt/torch-2.1.2%2Bcu118-cp310-cp310-win_amd64.whl"
|
122 |
+
print("Installing Torch from %s" % wheel_file)
|
123 |
+
install(wheel_file)
|
124 |
+
# assume cpu torch part of install
|
125 |
+
#else:
|
126 |
+
# wheel_file = "https://h2o-release.s3.amazonaws.com/h2ogpt/torch-2.1.2-cp310-cp310-win_amd64.whl"
|
127 |
+
# print("Installing Torch from %s" % wheel_file)
|
128 |
+
# install(wheel_file)
|
129 |
+
import importlib
|
130 |
+
importlib.invalidate_caches()
|
131 |
+
import pkg_resources
|
132 |
+
importlib.reload(pkg_resources) # re-load because otherwise cache would be bad
|
133 |
+
|
134 |
+
from generate import entrypoint_main as main_h2ogpt
|
135 |
+
main_h2ogpt()
|
136 |
+
|
137 |
+
server_name = os.getenv('h2ogpt_server_name', os.getenv('H2OGPT_SERVER_NAME', 'localhost'))
|
138 |
+
server_port = os.getenv('GRADIO_SERVER_PORT', str(7860))
|
139 |
+
|
140 |
+
url = "http://%s:%s" % (server_name, server_port)
|
141 |
+
webbrowser.open(url)
|
142 |
+
|
143 |
+
while True:
|
144 |
+
time.sleep(10000)
|
145 |
+
|
146 |
+
|
147 |
+
def main():
|
148 |
+
try:
|
149 |
+
_main()
|
150 |
+
except BaseException as e:
|
151 |
+
with open('h2ogpt_exception.log', 'at') as f:
|
152 |
+
f.write(traceback.format_exc())
|
153 |
+
time.sleep(10)
|
154 |
+
raise
|
155 |
+
time.sleep(10)
|
156 |
+
|
157 |
+
|
158 |
+
if __name__ == "__main__":
|
159 |
+
main()
|