initial_commit
Browse files
app.py
CHANGED
@@ -1,63 +1,236 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
"""
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
26 |
-
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
"""
|
43 |
-
For information on
|
44 |
"""
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
|
62 |
-
if __name__ == "__main__":
|
63 |
-
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
## Import required packages
|
4 |
|
5 |
+
from langchain.llms import CTransformers
|
6 |
+
from langchain.prompts import FewShotChatMessagePromptTemplate, ChatPromptTemplate, FewShotPromptTemplate
|
7 |
+
import gradio as gr
|
8 |
+
from langchain.chains import LLMChain
|
9 |
+
from langchain.prompts import PromptTemplate
|
10 |
+
# from langchain.document_loaders import WikipediaLoader
|
11 |
+
from langchain.document_loaders import JSONLoader
|
12 |
+
from langchain.text_splitter import CharacterTextSplitter
|
13 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
14 |
+
from langchain.vectorstores import Chroma
|
15 |
+
from langchain.chains import RetrievalQA
|
16 |
+
from operator import itemgetter
|
17 |
+
from langchain_core.output_parsers import JsonOutputParser
|
18 |
+
from langchain_core.outputs import Generation
|
19 |
+
from typing import Any, List, Optional, Type, TypeVar, Union
|
20 |
+
|
21 |
+
## Defining few variables
|
22 |
+
MODEL_PATH = "TheBloke/Mistral-7B-Claude-Chat-GGUF"
|
23 |
+
MODEL_FILE = "mistral-7b-claude-chat.Q4_K_M.gguf"
|
24 |
+
MODEL_TYPE = "mistral"
|
25 |
+
MAX_NEW_TOKENS = 100
|
26 |
+
temperature = 1
|
27 |
+
top_p = 0.95
|
28 |
+
top_k = 50
|
29 |
+
repetition_penalty = 1.5
|
30 |
+
|
31 |
+
## Defining Model
|
32 |
+
llm = CTransformers(
|
33 |
+
model = MODEL_PATH,
|
34 |
+
model_file=MODEL_FILE,
|
35 |
+
model_type = MODEL_TYPE,
|
36 |
+
config = {
|
37 |
+
"max_new_tokens":MAX_NEW_TOKENS,
|
38 |
+
"temperature": temperature,
|
39 |
+
"top_p": top_p,
|
40 |
+
"top_k": top_k,
|
41 |
+
"repetition_penalty": repetition_penalty,
|
42 |
+
"last_n_tokens": 4,
|
43 |
+
"stream": True,
|
44 |
+
"gpu_layers": 1000
|
45 |
+
}
|
46 |
+
)
|
47 |
+
|
48 |
+
# One shot inferencing
|
49 |
+
examples = [
|
50 |
+
{
|
51 |
+
"query": "Please classify this name: Ketan Jogadankar",
|
52 |
+
"answer":"""{
|
53 |
+
"name": "Ketan Jogadankar",
|
54 |
+
"label": "person",
|
55 |
+
"score": 0.99,
|
56 |
+
"reason": "Ketan is a most famous first name and Jogadankar looks like a surname."
|
57 |
+
}"""
|
58 |
+
}
|
59 |
+
]
|
60 |
+
|
61 |
+
example_template = """
|
62 |
+
User: {query}
|
63 |
+
{answer}
|
64 |
"""
|
65 |
+
|
66 |
+
example_prompt = ChatPromptTemplate.from_messages(
|
67 |
+
[("human", "{query}"),
|
68 |
+
("ai", "{answer}")]
|
69 |
+
)
|
70 |
+
|
71 |
+
prefix = """Act as an AI assistant that classifies names into 3 categories (person, business and other) based on the provided rules and example data.
|
72 |
+
{format_instructions}
|
73 |
+
Do not append any text to human input.
|
74 |
+
Rules:
|
75 |
+
* If the names contains the word "POD", classify it as a other.
|
76 |
+
* If the names contains the word "trust", classify it as a other.
|
77 |
+
* If the names contains the word "llc", classify it as a business.
|
78 |
+
* If the name is non-profit organization then classify it as a other.
|
79 |
+
Here are some examples:
|
80 |
"""
|
|
|
81 |
|
82 |
+
suffix = """Please classify this name: {name}
|
83 |
+
"""
|
84 |
+
|
85 |
+
few_shot_prompt_template = FewShotChatMessagePromptTemplate(
|
86 |
+
examples = examples,
|
87 |
+
example_prompt = example_prompt
|
88 |
+
)
|
89 |
+
|
90 |
+
prompt = ChatPromptTemplate.from_messages(
|
91 |
+
[
|
92 |
+
("system",prefix),
|
93 |
+
few_shot_prompt_template,
|
94 |
+
("human", suffix)
|
95 |
+
]
|
96 |
+
)
|
97 |
+
|
98 |
+
format_instructions = """The output should be formatted as a JSON instance that conforms to the JSON schema below.
|
99 |
+
Here is the output schema:
|
100 |
+
```
|
101 |
+
{"properties": {"name": {"title": "Name", "description": "this is the input name passed by human", "type": "string"}, "label": {"title": "Label", "description": "this is the label predicted for input name", "type": "string"}, "score": {"title": "Score", "description": "This is confidence score for predicted label", "type": "number"}, "reason": {"title": "Reason", "description": "This is to explain why AI has predicted that label", "type": "string"}}, "required": ["name", "label", "score", "reason"]}
|
102 |
+
```
|
103 |
+
"""
|
104 |
+
# RAG
|
105 |
+
data_loader = JSONLoader(file_path="/content/sample_data/anscombe.json",
|
106 |
+
jq_schema='.',text_content=False)
|
107 |
+
data = data_loader.load()
|
108 |
+
data = [doc.page_content for doc in data]
|
109 |
+
|
110 |
+
splitter = CharacterTextSplitter(chunk_size=2, chunk_overlap=1)
|
111 |
+
documents = splitter.create_documents(texts=data)
|
112 |
|
113 |
+
docs_str = [doc.page_content for doc in documents]
|
114 |
+
sentence_emb = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
+
db = Chroma.from_texts(docs_str, sentence_emb, persist_directory="./temp_db")
|
117 |
+
db.persist()
|
|
|
|
|
|
|
118 |
|
119 |
+
retriever = db.as_retriever(
|
120 |
+
search_type="similarity",
|
121 |
+
search_kwargs={'k':1})
|
122 |
|
123 |
+
# Pydantic output validator
|
124 |
+
from pydantic import BaseModel, Field
|
125 |
+
class NameClassification(BaseModel):
|
126 |
+
name:str = Field(description="this is the input name passed by human")
|
127 |
+
label:str = Field(description="this is the label predicted for input name")
|
128 |
+
score:float = Field(description="This is confidence score for predicted label")
|
129 |
+
reason:str = Field(description="This is to explain why AI has predicted that label")
|
130 |
|
131 |
+
def remove_junks(self, text):
|
132 |
+
start_index = text.index("{")
|
133 |
+
stop_index = text.index("}") + 1
|
134 |
+
return text[start_index:stop_index+1]
|
|
|
|
|
|
|
|
|
135 |
|
136 |
+
def parse(self, text):
|
137 |
+
text = self.remove_junks(text)
|
138 |
+
super().invoke(text)
|
139 |
+
|
140 |
+
class CustomParser(JsonOutputParser):
|
141 |
+
|
142 |
+
def parse_result(self, result: List[Generation], *, partial: bool = False) -> Any:
|
143 |
+
text = result[0].text
|
144 |
+
text = text.strip()
|
145 |
+
text = self.remove_junks(text)
|
146 |
+
result = [Generation(text= text)]
|
147 |
+
return super().parse_result(result=result,partial=partial)
|
148 |
+
|
149 |
+
|
150 |
+
def remove_junks(self, text):
|
151 |
+
start_index = text.index("{")
|
152 |
+
stop_index = text.index("}") + 1
|
153 |
+
return text[start_index:stop_index+1]
|
154 |
+
|
155 |
+
parser = CustomParser(pydantic_object=NameClassification)
|
156 |
+
|
157 |
+
chain = (
|
158 |
+
{"context": itemgetter("name") | retriever,
|
159 |
+
"format_instructions": itemgetter("format_instructions"),
|
160 |
+
"name": itemgetter("name")}
|
161 |
+
| prompt
|
162 |
+
| llm
|
163 |
+
| parser
|
164 |
+
)
|
165 |
|
166 |
"""
|
167 |
+
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
168 |
"""
|
169 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
170 |
+
|
171 |
+
def predict(message, history, min_hist_memo = 3):
|
172 |
+
# streamer = chain(message)
|
173 |
+
streamer = chain.invoke({"name":message, "format_instructions":format_instructions})
|
174 |
+
# partial_message = ""
|
175 |
+
# for new_token in streamer:
|
176 |
+
# if new_token != '<':
|
177 |
+
# partial_message += new_token
|
178 |
+
# yield partial_message
|
179 |
+
yield str(streamer)
|
180 |
+
|
181 |
+
gr.ChatInterface(predict, title="Mistral 7B").queue().launch(debug=True)
|
182 |
+
|
183 |
+
# def respond(
|
184 |
+
# message,
|
185 |
+
# history: list[tuple[str, str]],
|
186 |
+
# system_message,
|
187 |
+
# max_tokens,
|
188 |
+
# temperature,
|
189 |
+
# top_p,
|
190 |
+
# ):
|
191 |
+
# messages = [{"role": "system", "content": system_message}]
|
192 |
+
|
193 |
+
# for val in history:
|
194 |
+
# if val[0]:
|
195 |
+
# messages.append({"role": "user", "content": val[0]})
|
196 |
+
# if val[1]:
|
197 |
+
# messages.append({"role": "assistant", "content": val[1]})
|
198 |
+
|
199 |
+
# messages.append({"role": "user", "content": message})
|
200 |
+
|
201 |
+
# response = ""
|
202 |
+
|
203 |
+
# for message in client.chat_completion(
|
204 |
+
# messages,
|
205 |
+
# max_tokens=max_tokens,
|
206 |
+
# stream=True,
|
207 |
+
# temperature=temperature,
|
208 |
+
# top_p=top_p,
|
209 |
+
# ):
|
210 |
+
# token = message.choices[0].delta.content
|
211 |
+
|
212 |
+
# response += token
|
213 |
+
# yield response
|
214 |
+
|
215 |
+
# """
|
216 |
+
# For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
217 |
+
# """
|
218 |
+
# demo = gr.ChatInterface(
|
219 |
+
# respond,
|
220 |
+
# additional_inputs=[
|
221 |
+
# gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
222 |
+
# gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
223 |
+
# gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
224 |
+
# gr.Slider(
|
225 |
+
# minimum=0.1,
|
226 |
+
# maximum=1.0,
|
227 |
+
# value=0.95,
|
228 |
+
# step=0.05,
|
229 |
+
# label="Top-p (nucleus sampling)",
|
230 |
+
# ),
|
231 |
+
# ],
|
232 |
+
# )
|
233 |
|
234 |
|
235 |
+
# if __name__ == "__main__":
|
236 |
+
# demo.launch()
|