Spaces:
Runtime error
Runtime error
ffreemt
commited on
Commit
·
0519f89
1
Parent(s):
74b4e24
Update torch.cuda.is_available() for device/device_type
Browse files- app.py +27 -9
- requirements-gpu.txt +26 -0
app.py
CHANGED
|
@@ -12,6 +12,7 @@ from textwrap import dedent
|
|
| 12 |
from types import SimpleNamespace
|
| 13 |
|
| 14 |
import gradio as gr
|
|
|
|
| 15 |
from charset_normalizer import detect
|
| 16 |
from chromadb.config import Settings
|
| 17 |
from epub2txt import epub2txt
|
|
@@ -65,7 +66,7 @@ CHROMA_SETTINGS = Settings(
|
|
| 65 |
persist_directory=PERSIST_DIRECTORY,
|
| 66 |
anonymized_telemetry=False,
|
| 67 |
)
|
| 68 |
-
ns = SimpleNamespace(qa=None)
|
| 69 |
|
| 70 |
|
| 71 |
def load_single_document(file_path: str | Path) -> Document:
|
|
@@ -171,11 +172,15 @@ def upload_files(files):
|
|
| 171 |
file_paths = [file.name for file in files]
|
| 172 |
logger.info(file_paths)
|
| 173 |
|
|
|
|
| 174 |
res = ingest(file_paths)
|
| 175 |
-
logger.info("Processed:\n{res}")
|
|
|
|
|
|
|
|
|
|
| 176 |
del res
|
| 177 |
|
| 178 |
-
ns.qa = load_qa()
|
| 179 |
|
| 180 |
# return [str(elm) for elm in res]
|
| 181 |
return file_paths
|
|
@@ -184,7 +189,7 @@ def upload_files(files):
|
|
| 184 |
|
| 185 |
|
| 186 |
def ingest(
|
| 187 |
-
file_paths: list[str | Path], model_name="hkunlp/instructor-base", device_type=
|
| 188 |
):
|
| 189 |
"""Gen Chroma db.
|
| 190 |
|
|
@@ -195,7 +200,14 @@ def ingest(
|
|
| 195 |
'C:\\Users\\User\\AppData\\Local\\Temp\\gradio\\9390755bb391abc530e71a3946a7b50d463ba0ef\\README.md',
|
| 196 |
'C:\\Users\\User\\AppData\\Local\\Temp\\gradio\\3341f9a410a60ffa57bf4342f3018a3de689f729\\requirements.txt']
|
| 197 |
"""
|
| 198 |
-
logger.info("Doing ingest...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
if device_type in ["cpu", "CPU"]:
|
| 200 |
device = "cpu"
|
| 201 |
elif device_type in ["mps", "MPS"]:
|
|
@@ -267,9 +279,15 @@ def gen_local_llm(model_id="TheBloke/vicuna-7B-1.1-HF"):
|
|
| 267 |
return local_llm
|
| 268 |
|
| 269 |
|
| 270 |
-
def load_qa(device
|
| 271 |
"""Gen qa."""
|
| 272 |
logger.info("Doing qa")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
# device = 'cpu'
|
| 274 |
# model_name = "hkunlp/instructor-xl"
|
| 275 |
# model_name = "hkunlp/instructor-large"
|
|
@@ -310,7 +328,7 @@ def main():
|
|
| 310 |
logger.info(f"ROOT_DIRECTORY: {ROOT_DIRECTORY}")
|
| 311 |
|
| 312 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 313 |
-
logger.info(f"openai_api_key (hf space SECRETS
|
| 314 |
|
| 315 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 316 |
# name = gr.Textbox(label="Name")
|
|
@@ -350,6 +368,7 @@ def main():
|
|
| 350 |
bot_message = "Upload some file(s) for processing first."
|
| 351 |
chat_history.append((message, bot_message))
|
| 352 |
return "", chat_history
|
|
|
|
| 353 |
try:
|
| 354 |
res = ns.qa(message)
|
| 355 |
answer, docs = res["result"], res["source_documents"]
|
|
@@ -366,12 +385,11 @@ def main():
|
|
| 366 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 367 |
|
| 368 |
try:
|
| 369 |
-
from google import colab
|
| 370 |
|
| 371 |
share = True # start share when in colab
|
| 372 |
except Exception:
|
| 373 |
share = False
|
| 374 |
-
|
| 375 |
demo.launch(share=share)
|
| 376 |
|
| 377 |
|
|
|
|
| 12 |
from types import SimpleNamespace
|
| 13 |
|
| 14 |
import gradio as gr
|
| 15 |
+
import torch
|
| 16 |
from charset_normalizer import detect
|
| 17 |
from chromadb.config import Settings
|
| 18 |
from epub2txt import epub2txt
|
|
|
|
| 66 |
persist_directory=PERSIST_DIRECTORY,
|
| 67 |
anonymized_telemetry=False,
|
| 68 |
)
|
| 69 |
+
ns = SimpleNamespace(qa=None, ingest_done=None)
|
| 70 |
|
| 71 |
|
| 72 |
def load_single_document(file_path: str | Path) -> Document:
|
|
|
|
| 172 |
file_paths = [file.name for file in files]
|
| 173 |
logger.info(file_paths)
|
| 174 |
|
| 175 |
+
ns.ingest_done = False
|
| 176 |
res = ingest(file_paths)
|
| 177 |
+
logger.info(f"Processed:\n{res}")
|
| 178 |
+
|
| 179 |
+
# flag ns.qadone
|
| 180 |
+
ns.ingest_done = True
|
| 181 |
del res
|
| 182 |
|
| 183 |
+
# ns.qa = load_qa()
|
| 184 |
|
| 185 |
# return [str(elm) for elm in res]
|
| 186 |
return file_paths
|
|
|
|
| 189 |
|
| 190 |
|
| 191 |
def ingest(
|
| 192 |
+
file_paths: list[str | Path], model_name="hkunlp/instructor-base", device_type=None
|
| 193 |
):
|
| 194 |
"""Gen Chroma db.
|
| 195 |
|
|
|
|
| 200 |
'C:\\Users\\User\\AppData\\Local\\Temp\\gradio\\9390755bb391abc530e71a3946a7b50d463ba0ef\\README.md',
|
| 201 |
'C:\\Users\\User\\AppData\\Local\\Temp\\gradio\\3341f9a410a60ffa57bf4342f3018a3de689f729\\requirements.txt']
|
| 202 |
"""
|
| 203 |
+
logger.info("\n\t Doing ingest...")
|
| 204 |
+
|
| 205 |
+
if device_type is None:
|
| 206 |
+
if torch.cuda.is_available():
|
| 207 |
+
device_type = "cuda"
|
| 208 |
+
else:
|
| 209 |
+
device_type = "cpu"
|
| 210 |
+
|
| 211 |
if device_type in ["cpu", "CPU"]:
|
| 212 |
device = "cpu"
|
| 213 |
elif device_type in ["mps", "MPS"]:
|
|
|
|
| 279 |
return local_llm
|
| 280 |
|
| 281 |
|
| 282 |
+
def load_qa(device=None, model_name: str = "hkunlp/instructor-base"):
|
| 283 |
"""Gen qa."""
|
| 284 |
logger.info("Doing qa")
|
| 285 |
+
if device is None:
|
| 286 |
+
if torch.cuda.is_available():
|
| 287 |
+
device = "cuda"
|
| 288 |
+
else:
|
| 289 |
+
device = "cpu"
|
| 290 |
+
|
| 291 |
# device = 'cpu'
|
| 292 |
# model_name = "hkunlp/instructor-xl"
|
| 293 |
# model_name = "hkunlp/instructor-large"
|
|
|
|
| 328 |
logger.info(f"ROOT_DIRECTORY: {ROOT_DIRECTORY}")
|
| 329 |
|
| 330 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 331 |
+
logger.info(f"openai_api_key (env var/hf space SECRETS): {openai_api_key}")
|
| 332 |
|
| 333 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 334 |
# name = gr.Textbox(label="Name")
|
|
|
|
| 368 |
bot_message = "Upload some file(s) for processing first."
|
| 369 |
chat_history.append((message, bot_message))
|
| 370 |
return "", chat_history
|
| 371 |
+
|
| 372 |
try:
|
| 373 |
res = ns.qa(message)
|
| 374 |
answer, docs = res["result"], res["source_documents"]
|
|
|
|
| 385 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 386 |
|
| 387 |
try:
|
| 388 |
+
from google import colab # noqa
|
| 389 |
|
| 390 |
share = True # start share when in colab
|
| 391 |
except Exception:
|
| 392 |
share = False
|
|
|
|
| 393 |
demo.launch(share=share)
|
| 394 |
|
| 395 |
|
requirements-gpu.txt
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain==0.0.166
|
| 2 |
+
chromadb==0.3.22
|
| 3 |
+
llama-cpp-python==0.1.48
|
| 4 |
+
urllib3==1.26.6
|
| 5 |
+
pdfminer.six==20221105
|
| 6 |
+
InstructorEmbedding
|
| 7 |
+
|
| 8 |
+
# required by sentence-transformers
|
| 9 |
+
# --extra-index-url https://download.pytorch.org/whl/cpu
|
| 10 |
+
# torch
|
| 11 |
+
# torchvision
|
| 12 |
+
sentence-transformers
|
| 13 |
+
faiss-cpu
|
| 14 |
+
huggingface_hub
|
| 15 |
+
transformers
|
| 16 |
+
protobuf==3.20.0
|
| 17 |
+
accelerate
|
| 18 |
+
bitsandbytes
|
| 19 |
+
click
|
| 20 |
+
openpyxl
|
| 21 |
+
loguru
|
| 22 |
+
gradio
|
| 23 |
+
charset-normalizer
|
| 24 |
+
PyPDF2
|
| 25 |
+
epub2txt
|
| 26 |
+
docx2txt
|