hikinegi commited on
Commit
09b18fa
β€’
1 Parent(s): 9580276

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +154 -0
app.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import langchain
2
+ import gradio as gr
3
+ import random
4
+ import time
5
+ import transformers
6
+ from langchain import HuggingFacePipeline
7
+ from langchain import PromptTemplate, LLMChain
8
+ import os
9
+ import torch
10
+
11
+ import torch
12
+ from transformers import LlamaForCausalLM, LlamaTokenizer
13
+
14
+
15
+ # Hugging Face model_path
16
+ model_id = 'SachinKaushik/docGPT'
17
+ tokenizer = LlamaTokenizer.from_pretrained(model_id)
18
+ model = LlamaForCausalLM.from_pretrained(
19
+ model_id, torch_dtype=torch.float16, device_map='auto',
20
+ )
21
+
22
+
23
+ # set model to eval mode
24
+ model.eval()
25
+
26
+ # Build HF Transformers pipeline
27
+ pipeline=transformers.pipeline(
28
+ "text-generation",
29
+ model=model,
30
+ tokenizer=tokenizer,
31
+ device_map="auto",
32
+ max_length=768,
33
+ do_sample=True,
34
+ top_k=10,
35
+ num_return_sequences=1,
36
+ eos_token_id=tokenizer.eos_token_id
37
+ )
38
+
39
+ # Setup prompt template
40
+ template = PromptTemplate(input_variables=['input'], template='{input}')
41
+
42
+ # Pass hugging face pipeline to langchain class
43
+ llm = HuggingFacePipeline(pipeline=pipeline)
44
+
45
+ # Build stacked LLM chain i.e. prompt-formatting + LLM
46
+ chain = LLMChain(llm=llm, prompt=template)
47
+
48
+ # setup prompt template for an instruction with no input
49
+ prompt = PromptTemplate(
50
+ input_variables=["query"],
51
+ template="""You are a helpful AI assistant, you will answer the users query
52
+ with a short but precise answer. If you are not sure about the answer you state
53
+ "I don't know". This is a conversation, not a webpage, there should be ZERO HTML
54
+ in the response.
55
+
56
+ Remember, Assistant responses are concise. Here is the conversation:
57
+
58
+ User: {query}
59
+ Assistant: """
60
+ )
61
+
62
+ # Pass hugging face pipeline to langchain class
63
+ llm = HuggingFacePipeline(pipeline=pipeline)
64
+
65
+ # Build stacked LLM chain i.e. prompt-formatting + LLM
66
+ llm_chain = LLMChain(llm=llm, prompt=prompt)
67
+
68
+
69
+ # import PDF document loaders and splitter
70
+ from langchain.document_loaders import PyPDFLoader, TextLoader
71
+ from langchain.text_splitter import CharacterTextSplitter
72
+
73
+ # Import chroma as the vector store
74
+ from langchain.vectorstores import Chroma
75
+ from langchain.chains import RetrievalQA
76
+
77
+ # Import vector store tools
78
+ from langchain.agents.agent_toolkits import (
79
+ create_vectorstore_agent,
80
+ VectorStoreToolkit,
81
+ VectorStoreInfo
82
+ )
83
+
84
+ # embedding Class
85
+ from langchain.embeddings import HuggingFaceEmbeddings
86
+
87
+
88
+ # function to generate embeddings
89
+ from langchain.document_loaders import WebBaseLoader
90
+
91
+ def load_data_in_VectorDB(data_source,doc_type='text', model_id='intfloat/e5-base-v2', chunk_size=300, chunk_overlap=100):
92
+ if doc_type=="text":
93
+ loader = TextLoader(data_source,encoding="utf-8" )
94
+ documents = loader.load()
95
+ else:
96
+ loader = WebBaseLoader(data_source)
97
+ documents = loader.load()
98
+
99
+ text_splitter = CharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=0,add_start_index=True )
100
+ texts = text_splitter.split_documents(documents)
101
+ embeddings = HuggingFaceEmbeddings(model_name=model_id)
102
+ return texts, embeddings
103
+
104
+ texts, embeddings = load_data_in_VectorDB(data_source='https://en.wikipedia.org/wiki/2022%E2%80%9323_NBA_season',doc_type='web')
105
+ db = Chroma.from_documents(texts, embeddings)
106
+ retriever = db.as_retriever()
107
+
108
+ # Pass hugging face pipeline to langchain class
109
+ llm = HuggingFacePipeline(pipeline=pipeline)
110
+
111
+ # qa agent using LLM and Retriever
112
+ qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever, return_source_documents=True)
113
+
114
+ import gradio as gr
115
+
116
+ def generate_answer(query):
117
+ ans = qa({"query": query})
118
+ ans = ans['result']
119
+ meta= "\n".join([i for i in {i.metadata['source'] for i in result['source_documents']}])
120
+ return f"DocGPT Response: {ans} \nSource: {meta}"
121
+
122
+ theme = gr.themes.Default(#color contructors
123
+ primary_hue="red",
124
+ secondary_hue="blue",
125
+ neutral_hue="green")
126
+
127
+ with gr.Blocks(css="""#col_container {margin-left: auto; margin-right: auto;}
128
+ # DocumentGPT {height: 520px; overflow: auto;} """, theme=theme ) as demo:
129
+ chatbot = gr.Chatbot(label="DocumentGPTBot")
130
+ msg = gr.Textbox(label = "DocGPT")
131
+ clear = gr.ClearButton([msg, chatbot])
132
+
133
+ def user(user_message, history):
134
+ return "", history + [[user_message, None]]
135
+
136
+ def bot(history):
137
+ bot_message = generate_answer(history[-1][0])
138
+ history[-1][1] = ""
139
+ for character in bot_message:
140
+ history[-1][1] += character
141
+ time.sleep(0.05)
142
+ yield history
143
+
144
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
145
+ bot, chatbot, chatbot
146
+ )
147
+ clear.click(lambda: None, None, chatbot, queue=False)
148
+
149
+ with gr.Row(visible=True) as button_row:
150
+ upvote_btn = gr.Button(value="πŸ‘ Upvote", interactive=True)
151
+ downvote_btn = gr.Button(value="πŸ‘Ž Downvote", interactive=True)
152
+
153
+ demo.queue()
154
+ demo.launch(debug=True)