BroBro87 commited on
Commit
e10f2ca
β€’
1 Parent(s): e19279d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -61
app.py CHANGED
@@ -1,76 +1,52 @@
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
- import git
8
- from llama_index import SimpleDirectoryReader
9
-
10
  from llama_index import VectorStoreIndex, SimpleDirectoryReader, ServiceContext
11
- documents = SimpleDirectoryReader("./").load_data()
12
-
13
-
14
-
15
- import torch
16
-
17
  from llama_index.llms import LlamaCPP
18
  from llama_index.llms.llama_utils import messages_to_prompt, completion_to_prompt
19
- llm = LlamaCPP(
20
- # You can pass in the URL to a GGML model to download it automatically
21
- model_url='https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.1-GGUF/resolve/main/mistral-7b-instruct-v0.1.Q4_K_M.gguf',
22
- # optionally, you can set the path to a pre-downloaded model instead of model_url
23
- model_path=None,
24
- temperature=0.1,
25
- max_new_tokens=256,
26
- # llama2 has a context window of 4096 tokens, but we set it lower to allow for some wiggle room
27
- context_window=3900,
28
- # kwargs to pass to __call__()
29
- generate_kwargs={},
30
- # kwargs to pass to __init__()
31
- # set to at least 1 to use GPU
32
- model_kwargs={"n_gpu_layers": -1},
33
- # transform inputs into Llama2 format
34
- messages_to_prompt=messages_to_prompt,
35
- completion_to_prompt=completion_to_prompt,
36
- verbose=True,
37
- )
38
-
39
  from langchain.embeddings.huggingface import HuggingFaceEmbeddings
40
  from llama_index.embeddings import LangchainEmbedding
41
 
42
- embed_model = LangchainEmbedding(
43
- HuggingFaceEmbeddings(model_name="thenlper/gte-large")
44
- )
45
-
46
- service_context = ServiceContext.from_defaults(
47
- chunk_size=256,
48
- llm=llm,
49
- embed_model=embed_model
50
- )
51
-
52
- index = VectorStoreIndex.from_documents(documents, service_context=service_context)
53
-
54
-
55
- # Create a Streamlit app file (e.g., app.py) and run it
56
-
57
- import streamlit as st
58
- from transformers import GPT2LMHeadModel, GPT2Tokenizer
59
-
60
- def generate_response(prompt):
61
- model_name = "gpt2"
62
- model = GPT2LMHeadModel.from_pretrained(model_name)
63
- tokenizer = GPT2Tokenizer.from_pretrained(model_name)
64
-
65
- input_ids = tokenizer.encode(prompt, return_tensors="pt")
66
- output = model.generate(input_ids, max_length=100, num_return_sequences=1, no_repeat_ngram_size=2)
67
 
68
- generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
69
- return generated_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
  def main():
72
  st.title("Cloudflare RAG")
73
 
 
 
 
 
 
 
74
  # User input
75
  user_input = st.text_input("Enter your message:")
76
 
@@ -83,4 +59,4 @@ def main():
83
  st.text_area("ChatGPT Response:", response, height=100)
84
 
85
  if __name__ == "__main__":
86
- main()
 
1
  import logging
2
  import sys
3
+ import streamlit as st
 
 
 
 
 
 
4
  from llama_index import VectorStoreIndex, SimpleDirectoryReader, ServiceContext
 
 
 
 
 
 
5
  from llama_index.llms import LlamaCPP
6
  from llama_index.llms.llama_utils import messages_to_prompt, completion_to_prompt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  from langchain.embeddings.huggingface import HuggingFaceEmbeddings
8
  from llama_index.embeddings import LangchainEmbedding
9
 
10
+ # Set up logging
11
+ logging.basicConfig(stream=sys.stdout, level=logging.INFO)
12
+ logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
+ def configure_llama_model():
15
+ model_url = 'https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.1-GGUF/resolve/main/mistral-7b-instruct-v0.1.Q4_K_M.gguf'
16
+ llm = LlamaCPP(
17
+ model_url=model_url,
18
+ temperature=0.1,
19
+ max_new_tokens=256,
20
+ context_window=3900,
21
+ model_kwargs={"n_gpu_layers": -1},
22
+ messages_to_prompt=messages_to_prompt,
23
+ completion_to_prompt=completion_to_prompt,
24
+ verbose=True,
25
+ )
26
+ return llm
27
+
28
+ def configure_embeddings():
29
+ embed_model = LangchainEmbedding(
30
+ HuggingFaceEmbeddings(model_name="thenlper/gte-large")
31
+ )
32
+ return embed_model
33
+
34
+ def configure_service_context(llm, embed_model):
35
+ return ServiceContext.from_defaults(chunk_size=256, llm=llm, embed_model=embed_model)
36
+
37
+ def initialize_vector_store_index(data_path, service_context):
38
+ documents = SimpleDirectoryReader(data_path).load_data()
39
+ return VectorStoreIndex.from_documents(documents, service_context=service_context)
40
 
41
  def main():
42
  st.title("Cloudflare RAG")
43
 
44
+ # Configure and initialize components
45
+ llm = configure_llama_model()
46
+ embed_model = configure_embeddings()
47
+ service_context = configure_service_context(llm, embed_model)
48
+ index = initialize_vector_store_index("/content/Data/", service_context)
49
+
50
  # User input
51
  user_input = st.text_input("Enter your message:")
52
 
 
59
  st.text_area("ChatGPT Response:", response, height=100)
60
 
61
  if __name__ == "__main__":
62
+ main()