Update app.py
Browse files
app.py
CHANGED
|
@@ -1,74 +1,65 @@
|
|
| 1 |
-
|
| 2 |
-
import subprocess
|
| 3 |
-
subprocess.run(["/usr/local/bin/python", "-m", "pip", "install", "--upgrade", "sentence-transformers"])
|
| 4 |
-
subprocess.run(["pip", "install", "sentence-transformers"])
|
| 5 |
-
subprocess.run(["pip", "install", "langchain"])
|
| 6 |
-
subprocess.run(["pip", "install", "-q", "pypdf"])
|
| 7 |
-
subprocess.run(["pip", "install", "-q", "python-dotenv"])
|
| 8 |
-
subprocess.run(["pip", "install", "-q", "transformers"])
|
| 9 |
-
subprocess.run(["pip", "install", "llama-cpp-python", "--no-cache-dir", "--install-option", "--CMAKE_ARGS=-DLLAMA_CUBLAS=on", "--install-option", "--FORCE_CMAKE=1"])
|
| 10 |
-
subprocess.run(["pip", "install", "-q", "llama-index"])
|
| 11 |
-
import subprocess
|
| 12 |
-
import gradio as gr
|
| 13 |
import logging
|
| 14 |
import sys
|
| 15 |
-
from llama_index import VectorStoreIndex, SimpleDirectoryReader, ServiceContext
|
| 16 |
-
from llama_index.llms import LlamaCPP
|
| 17 |
-
from llama_index.llms.llama_utils import messages_to_prompt, completion_to_prompt
|
| 18 |
-
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
|
| 19 |
-
from llama_index.embeddings import LangchainEmbedding
|
| 20 |
|
| 21 |
-
# Set up logging
|
| 22 |
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
|
| 23 |
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
documents = SimpleDirectoryReader("/content/Data/").load_data()
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
llm = LlamaCPP(
|
|
|
|
| 30 |
model_url='https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.1-GGUF/resolve/main/mistral-7b-instruct-v0.1.Q4_K_M.gguf',
|
|
|
|
| 31 |
model_path=None,
|
| 32 |
temperature=0.1,
|
| 33 |
max_new_tokens=256,
|
|
|
|
| 34 |
context_window=3900,
|
|
|
|
| 35 |
generate_kwargs={},
|
|
|
|
|
|
|
| 36 |
model_kwargs={"n_gpu_layers": -1},
|
|
|
|
| 37 |
messages_to_prompt=messages_to_prompt,
|
| 38 |
completion_to_prompt=completion_to_prompt,
|
| 39 |
verbose=True,
|
| 40 |
)
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
embed_model = LangchainEmbedding(
|
| 44 |
-
|
| 45 |
)
|
| 46 |
|
| 47 |
-
|
| 48 |
service_context = ServiceContext.from_defaults(
|
| 49 |
chunk_size=256,
|
| 50 |
llm=llm,
|
| 51 |
embed_model=embed_model
|
| 52 |
)
|
| 53 |
|
| 54 |
-
# Create index
|
| 55 |
index = VectorStoreIndex.from_documents(documents, service_context=service_context)
|
|
|
|
| 56 |
query_engine = index.as_query_engine()
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
return
|
| 62 |
-
|
| 63 |
-
# Create Gradio interface
|
| 64 |
-
iface = gr.Interface(
|
| 65 |
-
fn=query_handler,
|
| 66 |
-
inputs=gr.Textbox(prompt="Enter your question here..."),
|
| 67 |
-
outputs=gr.Textbox(),
|
| 68 |
-
live=True,
|
| 69 |
-
capture_session=True,
|
| 70 |
-
interpretation="query",
|
| 71 |
-
)
|
| 72 |
|
| 73 |
-
|
| 74 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import logging
|
| 2 |
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
| 4 |
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
|
| 5 |
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
|
| 6 |
|
| 7 |
+
from llama_index import VectorStoreIndex, SimpleDirectoryReader, ServiceContext
|
| 8 |
+
|
| 9 |
+
|
| 10 |
documents = SimpleDirectoryReader("/content/Data/").load_data()
|
| 11 |
|
| 12 |
+
import torch
|
| 13 |
+
|
| 14 |
+
from llama_index.llms import LlamaCPP
|
| 15 |
+
from llama_index.llms.llama_utils import messages_to_prompt, completion_to_prompt
|
| 16 |
llm = LlamaCPP(
|
| 17 |
+
# You can pass in the URL to a GGML model to download it automatically
|
| 18 |
model_url='https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.1-GGUF/resolve/main/mistral-7b-instruct-v0.1.Q4_K_M.gguf',
|
| 19 |
+
# optionally, you can set the path to a pre-downloaded model instead of model_url
|
| 20 |
model_path=None,
|
| 21 |
temperature=0.1,
|
| 22 |
max_new_tokens=256,
|
| 23 |
+
# llama2 has a context window of 4096 tokens, but we set it lower to allow for some wiggle room
|
| 24 |
context_window=3900,
|
| 25 |
+
# kwargs to pass to __call__()
|
| 26 |
generate_kwargs={},
|
| 27 |
+
# kwargs to pass to __init__()
|
| 28 |
+
# set to at least 1 to use GPU
|
| 29 |
model_kwargs={"n_gpu_layers": -1},
|
| 30 |
+
# transform inputs into Llama2 format
|
| 31 |
messages_to_prompt=messages_to_prompt,
|
| 32 |
completion_to_prompt=completion_to_prompt,
|
| 33 |
verbose=True,
|
| 34 |
)
|
| 35 |
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
|
| 40 |
+
from llama_index.embeddings import LangchainEmbedding
|
| 41 |
+
from llama_index import ServiceContext
|
| 42 |
+
|
| 43 |
embed_model = LangchainEmbedding(
|
| 44 |
+
HuggingFaceEmbeddings(model_name="thenlper/gte-large")
|
| 45 |
)
|
| 46 |
|
| 47 |
+
|
| 48 |
service_context = ServiceContext.from_defaults(
|
| 49 |
chunk_size=256,
|
| 50 |
llm=llm,
|
| 51 |
embed_model=embed_model
|
| 52 |
)
|
| 53 |
|
|
|
|
| 54 |
index = VectorStoreIndex.from_documents(documents, service_context=service_context)
|
| 55 |
+
|
| 56 |
query_engine = index.as_query_engine()
|
| 57 |
+
#response = query_engine.query("What is Fibromyalgia?")
|
| 58 |
|
| 59 |
+
import gradio as gr
|
| 60 |
+
|
| 61 |
+
def text_to_uppercase(text):
|
| 62 |
+
return query_engine.query(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
iface = gr.Interface(fn=text_to_uppercase, inputs="text", outputs="text")
|
| 65 |
+
iface.launch(share=True)
|