Navanjana commited on
Commit
5cee539
1 Parent(s): 7ba356d

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +81 -0
  2. requirements.txt +11 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import logging
3
+ import sys
4
+ import gradio as gr
5
+ logging.basicConfig(stream=sys.stdout, level=logging.INFO)
6
+ logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
7
+
8
+ from llama_index import VectorStoreIndex, SimpleDirectoryReader, ServiceContext
9
+ from llama_index.llms import HuggingFaceLLM
10
+
11
+
12
+ documents = SimpleDirectoryReader(
13
+ input_files=["bio.pdf"]
14
+ ).load_data()
15
+
16
+ from llama_index.prompts.prompts import SimpleInputPrompt
17
+
18
+
19
+ 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."
20
+
21
+
22
+
23
+ # This will wrap the default prompts that are internal to llama-index
24
+ query_wrapper_prompt = SimpleInputPrompt("<|USER|>{query_str}<|ASSISTANT|>")
25
+
26
+ from huggingface_hub import login
27
+ login(token="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")
28
+
29
+
30
+ import torch
31
+
32
+ llm = HuggingFaceLLM(
33
+ context_window=4096,
34
+ max_new_tokens=256,
35
+ generate_kwargs={"temperature": 0.0, "do_sample": False},
36
+ system_prompt=system_prompt,
37
+ query_wrapper_prompt=query_wrapper_prompt,
38
+ tokenizer_name="meta-llama/Llama-2-7b-chat-hf",
39
+ model_name="meta-llama/Llama-2-7b-chat-hf",
40
+ device_map="auto",
41
+ # uncomment this if using CUDA to reduce memory usage
42
+ model_kwargs={"torch_dtype": torch.float16 , "load_in_8bit":True}
43
+ )
44
+
45
+ from langchain.embeddings.huggingface import HuggingFaceEmbeddings
46
+ from llama_index import LangchainEmbedding, ServiceContext
47
+
48
+ embed_model = LangchainEmbedding(
49
+ HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
50
+ )
51
+
52
+ service_context = ServiceContext.from_defaults(
53
+ chunk_size=1024,
54
+ llm=llm,
55
+ embed_model=embed_model
56
+ )
57
+
58
+ index = VectorStoreIndex.from_documents(documents, service_context=service_context)
59
+
60
+
61
+ query_engine = index.as_query_engine()
62
+
63
+ # Define a function to get responses from your Q&A model
64
+ def get_response(query):
65
+ response = query_engine.query(query)
66
+ return response
67
+
68
+ # Create an input component for user queries
69
+ query_input = gr.inputs.Textbox(label="Enter your question", lines=2)
70
+
71
+ # Create an output component to display the response
72
+ response_output = gr.outputs.Textbox(label="Response")
73
+
74
+ # Create a Gradio interface
75
+ gr.Interface(
76
+ fn=get_response,
77
+ inputs=query_input,
78
+ outputs=response_output,
79
+ title="Q&A Assistant",
80
+ description="Ask a question and get an answer based on the provided documents.",
81
+ ).launch()
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pypdf
2
+ python-dotenv
3
+ transformers
4
+ einops
5
+ accelerate
6
+ langchain
7
+ bitsandbytes
8
+ sentence_transformers
9
+ llama-index
10
+ gradio
11
+ huggingface_hub