wizzseen commited on
Commit
12037f9
·
1 Parent(s): 27d71f6

Update app.py

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