Abbeite commited on
Commit
2ad852e
·
verified ·
1 Parent(s): c9b38fc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, ServiceContext
3
+ from llama_index.llms.huggingface import HuggingFaceLLM
4
+ import torch
5
+
6
+ documents = SimpleDirectoryReader("/content/Data").load_data()
7
+ from llama_index.core.prompts.prompts import SimpleInputPrompt
8
+
9
+ 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."
10
+
11
+
12
+ query_wrapper_prompt = SimpleInputPrompt("<|USER|>{query_str}<|ASSISTANT|>")
13
+
14
+
15
+ llm = HuggingFaceLLM(
16
+ context_window=4096,
17
+ max_new_tokens=256,
18
+ generate_kwargs={"temperature": 0.0, "do_sample": False},
19
+ system_prompt=system_prompt,
20
+ query_wrapper_prompt=query_wrapper_prompt,
21
+ tokenizer_name="microsoft/phi-2",
22
+ model_name="microsoft/phi-2",
23
+ device_map="cuda",
24
+ # uncomment this if using CUDA to reduce memory usage
25
+ model_kwargs={"torch_dtype": torch.bfloat16}
26
+ )
27
+
28
+
29
+ !pip install llama-index-embeddings-huggingface
30
+
31
+
32
+ from llama_index.embeddings.huggingface import HuggingFaceEmbedding
33
+
34
+ # loads BAAI/bge-small-en
35
+ # embed_model = HuggingFaceEmbedding()
36
+
37
+ # loads BAAI/bge-small-en-v1.5
38
+ embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
39
+
40
+ service_context = ServiceContext.from_defaults(
41
+ chunk_size=1024,
42
+ llm=llm,
43
+ embed_model=embed_model
44
+ )
45
+
46
+ index = VectorStoreIndex.from_documents(documents, service_context=service_context)
47
+
48
+ query_engine = index.as_query_engine()
49
+
50
+ def predict(input, history):
51
+ response = query_engine.query(input)
52
+ return str(response)
53
+
54
+
55
+
56
+ import gradio as gr
57
+
58
+ gr.ChatInterface(predict).launch(share=True)