File size: 6,224 Bytes
24445e1
 
 
0e9ce7a
24445e1
f7e770a
0e9ce7a
24445e1
 
 
 
 
 
03e7215
24445e1
 
03e7215
 
 
24445e1
 
 
 
 
 
 
 
03e7215
 
24445e1
 
 
 
 
 
ad7d9a9
efaa4a0
 
ad7d9a9
 
39012bc
ad7d9a9
222d618
 
 
 
 
 
 
5aab34d
 
24445e1
 
 
 
 
 
 
1e7cadb
 
24445e1
 
 
 
 
 
f7e770a
24445e1
1e7cadb
24445e1
 
e832688
5aab34d
 
 
0e9ce7a
ad7d9a9
 
24445e1
4c5f0fe
24445e1
ad7d9a9
24445e1
 
e832688
f0a4462
e832688
 
 
39012bc
e832688
a65c505
e832688
 
ad7d9a9
03e7215
24445e1
03e7215
 
24445e1
03e7215
 
 
ad7d9a9
24445e1
 
ad7d9a9
0e9ce7a
24445e1
 
 
ad7d9a9
 
4c5f0fe
03e7215
 
 
24445e1
 
ad7d9a9
5aab34d
 
0e9ce7a
ad7d9a9
 
 
 
2c7ed51
 
 
 
 
0e9ce7a
ad7d9a9
 
e832688
 
03e7215
 
 
 
ad7d9a9
03e7215
 
 
 
 
ad7d9a9
e832688
4c5f0fe
 
 
 
 
03e7215
4c5f0fe
24445e1
03e7215
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import gradio as gr

from langchain.text_splitter import CharacterTextSplitter
from langchain_community.document_loaders import UnstructuredFileLoader
from langchain.vectorstores.faiss import FAISS
from langchain.vectorstores.utils import DistanceStrategy
from langchain_community.embeddings import HuggingFaceEmbeddings

from langchain.chains import RetrievalQA
from langchain.prompts.prompt import PromptTemplate
from langchain.vectorstores.base import VectorStoreRetriever

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
from langchain_community.llms.huggingface_pipeline import HuggingFacePipeline

from transformers import TextIteratorStreamer
from threading import Thread

# Prompt template
template = """Instruction:
You are an AI assistant for answering questions about the provided context.
You are given the following extracted parts of a long document and a question. Provide a detailed answer.
If you don't know the answer, just say "Hmm, I'm not sure." Don't try to make up an answer.
=======
{context}
=======
Question: {question}
Output:\n"""

QA_PROMPT = PromptTemplate(
  template=template,
  input_variables=["question", "context"]
)

# Load Phi-2 model from hugging face hub
model_id = "microsoft/phi-2"


tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float32, device_map="auto", trust_remote_code=True)

# sentence transformers to be used in vector store
embeddings = HuggingFaceEmbeddings(
      model_name="sentence-transformers/multi-qa-MiniLM-L6-cos-v1",
      model_kwargs={'device': 'cpu'},
      encode_kwargs={'normalize_embeddings': False}
  )

# Returns a faiss vector store retriever given a txt file
def prepare_vector_store_retriever(filename):
  # Load data
  loader = UnstructuredFileLoader(filename)
  raw_documents = loader.load()

  # Split the text
  text_splitter = CharacterTextSplitter(
      separator="\n\n",
      chunk_size=800,
      chunk_overlap=0,
      length_function=len
  )

  documents = text_splitter.split_documents(raw_documents)

  # Creating a vectorstore
  vectorstore = FAISS.from_documents(documents, embeddings, distance_strategy=DistanceStrategy.COSINE)

  return VectorStoreRetriever(vectorstore=vectorstore, search_kwargs={"k": 2})

# Retrieveal QA chian
def get_retrieval_qa_chain(text_file, hf_model):
  retriever = default_retriever
  if text_file != default_text_file:
    retriever = prepare_vector_store_retriever(text_file)

  chain = RetrievalQA.from_chain_type(
      llm=hf_model,
      retriever=retriever,
      chain_type_kwargs={"prompt": QA_PROMPT},
  )
  return chain

# Generates response using the question answering chain defined earlier
def generate(question, answer, text_file, max_new_tokens):
  streamer = TextIteratorStreamer(tokenizer=tokenizer, skip_prompt=True, skip_special_tokens=True, timeout=300.0)
  phi2_pipeline = pipeline(
      "text-generation", tokenizer=tokenizer, model=model, max_new_tokens=max_new_tokens, 
      pad_token_id=tokenizer.eos_token_id, eos_token_id=tokenizer.eos_token_id,
      device_map="auto", streamer=streamer
    )

  hf_model = HuggingFacePipeline(pipeline=phi2_pipeline)
  qa_chain = get_retrieval_qa_chain(text_file, hf_model)

  query = f"{question}"

  thread = Thread(target=qa_chain.invoke, kwargs={"input": {"query": query}})
  thread.start()

  response = ""
  for token in streamer:
    response += token
    yield response.strip()

# replaces the retreiver in the question answering chain whenever a new file is uploaded
def upload_file(file):
  return file, file

with gr.Blocks() as demo:
  gr.Markdown("""
  # Retrieval Augmented Generation with Phi-2: Question Answering demo
  ### This demo uses the Phi-2 language model and Retrieval Augmented Generation (RAG). It allows you to upload a txt file and ask the model questions related to the content of that file.
  ### If you don't have one, there is a txt file already loaded, the new Oppenheimer movie's entire wikipedia page. The movie came out very recently in July, 2023, so the Phi-2 model is not aware of it.
  The context size of the Phi-2 model is 2048 tokens, so even this medium size wikipedia page (11.5k tokens) does not fit in the context window.
  Retrieval Augmented Generation (RAG) enables us to retrieve just the few small chunks of the document that are relevant to the our query and inject it into our prompt.
  The model is then able to answer questions by incorporating knowledge from the newly provided document. RAG can be used with thousands of documents, but this demo is limited to just one txt file.
  """)

  default_text_file = "Oppenheimer-movie-wiki.txt"
  default_retriever = prepare_vector_store_retriever(default_text_file)
  
  text_file = gr.State(default_text_file)

  gr.Markdown("## Upload a txt file or Use the Default 'Oppenheimer-movie-wiki.txt' that has already been loaded")

  file_name = gr.Textbox(label="Loaded text file", value=default_text_file, lines=1, interactive=False)
  upload_button = gr.UploadButton(
      label="Click to upload a text file",
      file_types=["text"],
      file_count="single"
  )
  upload_button.upload(upload_file, upload_button, [file_name, text_file])

  gr.Markdown("## Enter your question")
  tokens_slider = gr.Slider(8, 256, value=64, label="Maximum new tokens", info="A larger `max_new_tokens` parameter value gives you longer text responses but at the cost of a slower response time.")
  
  with gr.Row():
    with gr.Column():
      ques = gr.Textbox(label="Question", placeholder="Enter text here", lines=3)
    with gr.Column():
      ans = gr.Textbox(label="Answer", lines=4, interactive=False)
  with gr.Row():
    with gr.Column():
      btn = gr.Button("Submit")
    with gr.Column():
      clear = gr.ClearButton([ques, ans])

  btn.click(fn=generate, inputs=[ques, ans, text_file, tokens_slider], outputs=[ans])
  examples = gr.Examples(
        examples=[
            "Who portrayed J. Robert Oppenheimer in the new Oppenheimer movie?",
            "In the plot of the movie, why did Lewis Strauss resent Robert Oppenheimer?"
        ],
        inputs=[ques],
    )

demo.queue().launch()