Spaces:
Runtime error
Runtime error
ffreemt
commited on
Commit
•
89dc142
1
Parent(s):
6b1569a
Update check ROOT_DIRECTORY and os.getenv("OPENAI_API_KEY")
Browse files
app.py
CHANGED
@@ -14,20 +14,23 @@ from pathlib import Path
|
|
14 |
|
15 |
import gradio as gr
|
16 |
from charset_normalizer import detect
|
|
|
17 |
from langchain.docstore.document import Document
|
18 |
from langchain.document_loaders import CSVLoader, PDFMinerLoader, TextLoader
|
19 |
|
20 |
# from constants import CHROMA_SETTINGS, SOURCE_DIRECTORY, PERSIST_DIRECTORY
|
21 |
from langchain.embeddings import HuggingFaceInstructEmbeddings
|
|
|
22 |
from langchain.text_splitter import (
|
23 |
CharacterTextSplitter,
|
24 |
RecursiveCharacterTextSplitter,
|
25 |
)
|
26 |
-
|
27 |
-
from langchain.vectorstores import Chroma
|
28 |
from loguru import logger
|
29 |
from PyPDF2 import PdfReader # localgpt
|
30 |
from chromadb.config import Settings
|
|
|
31 |
|
32 |
# from utils import xlxs_to_csv
|
33 |
|
@@ -147,7 +150,7 @@ def upload_files(files):
|
|
147 |
logger.info(file_paths)
|
148 |
|
149 |
res = ingest(file_paths)
|
150 |
-
|
151 |
# return [str(elm) for elm in res]
|
152 |
return file_paths
|
153 |
|
@@ -197,6 +200,30 @@ def ingest(file_paths: list[str | Path], model_name="hkunlp/instructor-base", de
|
|
197 |
return [[Path(doc.metadata.get("source")).name, len(doc.page_content)] for doc in documents]
|
198 |
|
199 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
def main1():
|
201 |
"""Lump codes"""
|
202 |
with gr.Blocks() as demo:
|
@@ -208,6 +235,11 @@ def main1():
|
|
208 |
|
209 |
def main():
|
210 |
"""Do blocks."""
|
|
|
|
|
|
|
|
|
|
|
211 |
with gr.Blocks() as demo:
|
212 |
name = gr.Textbox(label="Name")
|
213 |
greet_btn = gr.Button("Submit")
|
|
|
14 |
|
15 |
import gradio as gr
|
16 |
from charset_normalizer import detect
|
17 |
+
from langchain.chains import RetrievalQA
|
18 |
from langchain.docstore.document import Document
|
19 |
from langchain.document_loaders import CSVLoader, PDFMinerLoader, TextLoader
|
20 |
|
21 |
# from constants import CHROMA_SETTINGS, SOURCE_DIRECTORY, PERSIST_DIRECTORY
|
22 |
from langchain.embeddings import HuggingFaceInstructEmbeddings
|
23 |
+
from langchain.llms import HuggingFacePipeline
|
24 |
from langchain.text_splitter import (
|
25 |
CharacterTextSplitter,
|
26 |
RecursiveCharacterTextSplitter,
|
27 |
)
|
28 |
+
# FAISS instead of PineCone
|
29 |
+
from langchain.vectorstores import FAISS, Chroma
|
30 |
from loguru import logger
|
31 |
from PyPDF2 import PdfReader # localgpt
|
32 |
from chromadb.config import Settings
|
33 |
+
from transformers import LlamaTokenizer, LlamaForCausalLM, pipeline
|
34 |
|
35 |
# from utils import xlxs_to_csv
|
36 |
|
|
|
150 |
logger.info(file_paths)
|
151 |
|
152 |
res = ingest(file_paths)
|
153 |
+
|
154 |
# return [str(elm) for elm in res]
|
155 |
return file_paths
|
156 |
|
|
|
200 |
return [[Path(doc.metadata.get("source")).name, len(doc.page_content)] for doc in documents]
|
201 |
|
202 |
|
203 |
+
def gen_local_llm(model_id="TheBloke/vicuna-7B-1.1-HF"):
|
204 |
+
"""Gen a local llm."""
|
205 |
+
model = LlamaForCausalLM.from_pretrained(
|
206 |
+
model_id,
|
207 |
+
# load_in_8bit=True, # set these options if your GPU supports them!
|
208 |
+
# device_map=1#'auto',
|
209 |
+
# torch_dtype=torch.float16,
|
210 |
+
# low_cpu_mem_usage=True
|
211 |
+
)
|
212 |
+
|
213 |
+
pipe = pipeline(
|
214 |
+
"text-generation",
|
215 |
+
model=model,
|
216 |
+
tokenizer=tokenizer,
|
217 |
+
max_length=2048,
|
218 |
+
temperature=0,
|
219 |
+
top_p=0.95,
|
220 |
+
repetition_penalty=1.15
|
221 |
+
)
|
222 |
+
|
223 |
+
local_llm = HuggingFacePipeline(pipeline=pipe)
|
224 |
+
return local_llm
|
225 |
+
|
226 |
+
|
227 |
def main1():
|
228 |
"""Lump codes"""
|
229 |
with gr.Blocks() as demo:
|
|
|
235 |
|
236 |
def main():
|
237 |
"""Do blocks."""
|
238 |
+
logger.info(f"ROOT_DIRECTORY: {ROOT_DIRECTORY}")
|
239 |
+
|
240 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
241 |
+
logger.info(f"openai_api_key (hf space SECRETS/env): {openai_api_key}")
|
242 |
+
|
243 |
with gr.Blocks() as demo:
|
244 |
name = gr.Textbox(label="Name")
|
245 |
greet_btn = gr.Button("Submit")
|