Spaces:
Runtime error
Runtime error
aiwithankit
commited on
Commit
·
0b34d84
1
Parent(s):
49bd8f9
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,65 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
import gradio as gr
|
4 |
def random_response(message, history):
|
5 |
-
|
|
|
|
|
6 |
|
7 |
demo = gr.ChatInterface(random_response)
|
8 |
if __name__ == "__main__":
|
|
|
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 |
+
from llama_index.llms import HuggingFaceLLM
|
9 |
+
|
10 |
+
documents = SimpleDirectoryReader("/content/drive/MyDrive/data").load_data()
|
11 |
+
|
12 |
+
from llama_index.prompts.prompts import SimpleInputPrompt
|
13 |
+
|
14 |
+
|
15 |
+
system_prompt = "You are a Q&A assistant. Your goal is to answer questions as accurately as possible based on the instructions and context provided."
|
16 |
+
|
17 |
+
|
18 |
+
|
19 |
+
# This will wrap the default prompts that are internal to llama-index
|
20 |
+
query_wrapper_prompt = SimpleInputPrompt("<|USER|>{query_str}<|ASSISTANT|>")
|
21 |
+
|
22 |
+
import torch
|
23 |
+
|
24 |
+
llm = HuggingFaceLLM(
|
25 |
+
context_window=4096,
|
26 |
+
max_new_tokens=256,
|
27 |
+
generate_kwargs={"temperature": 0.0, "do_sample": False},
|
28 |
+
system_prompt=system_prompt,
|
29 |
+
query_wrapper_prompt=query_wrapper_prompt,
|
30 |
+
tokenizer_name="NousResearch/Llama-2-7b-hf",
|
31 |
+
model_name="NousResearch/Llama-2-7b-hf",
|
32 |
+
device_map="auto",
|
33 |
+
# uncomment this if using CUDA to reduce memory usage
|
34 |
+
model_kwargs={"torch_dtype": torch.float16 , "load_in_8bit":True}
|
35 |
+
)
|
36 |
+
|
37 |
+
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
|
38 |
+
from llama_index import LangchainEmbedding, ServiceContext
|
39 |
+
|
40 |
+
embed_model = LangchainEmbedding(
|
41 |
+
HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
|
42 |
+
)
|
43 |
+
|
44 |
+
service_context = ServiceContext.from_defaults(
|
45 |
+
chunk_size=1024,
|
46 |
+
llm=llm,
|
47 |
+
embed_model=embed_model
|
48 |
+
)
|
49 |
+
|
50 |
+
index = VectorStoreIndex.from_documents(documents, service_context=service_context)
|
51 |
+
|
52 |
+
#query_engine = index.as_query_engine()
|
53 |
+
#response = query_engine.query("what is the name of this document?")
|
54 |
+
|
55 |
+
#print(response)
|
56 |
+
|
57 |
|
58 |
import gradio as gr
|
59 |
def random_response(message, history):
|
60 |
+
query_engine = index.as_query_engine()
|
61 |
+
response = query_engine.query(message)
|
62 |
+
return response
|
63 |
|
64 |
demo = gr.ChatInterface(random_response)
|
65 |
if __name__ == "__main__":
|