Ganesh Karbhari
commited on
Commit
•
c9a5e5e
1
Parent(s):
c6aec96
Update app.py
Browse files
app.py
CHANGED
@@ -168,112 +168,55 @@
|
|
168 |
|
169 |
|
170 |
|
171 |
-
|
172 |
-
# import gradio as gr
|
173 |
-
# client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
|
174 |
-
|
175 |
-
# def format_prompt(message, history):
|
176 |
-
# prompt = "<s>"
|
177 |
-
# for user_prompt, bot_response in history:
|
178 |
-
# prompt += f"[INST] {user_prompt} [/INST]"
|
179 |
-
# prompt += f" {bot_response}</s> "
|
180 |
-
# prompt += f"[INST] {message} [/INST]"
|
181 |
-
# return prompt
|
182 |
-
|
183 |
-
# def generate(
|
184 |
-
# prompt, history, temperature=0.2, max_new_tokens=3000, top_p=0.95, repetition_penalty=1.0,
|
185 |
-
# ):
|
186 |
-
# temperature = float(temperature)
|
187 |
-
# if temperature < 1e-2:
|
188 |
-
# temperature = 1e-2
|
189 |
-
# top_p = float(top_p)
|
190 |
-
|
191 |
-
# generate_kwargs = dict(
|
192 |
-
# temperature=temperature,
|
193 |
-
# max_new_tokens=max_new_tokens,
|
194 |
-
# top_p=top_p,
|
195 |
-
# repetition_penalty=repetition_penalty,
|
196 |
-
# do_sample=True,
|
197 |
-
# seed=42,
|
198 |
-
# )
|
199 |
-
|
200 |
-
# formatted_prompt = format_prompt(prompt, history)
|
201 |
-
|
202 |
-
# stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
203 |
-
# output = ""
|
204 |
-
|
205 |
-
# for response in stream:
|
206 |
-
# output += response.token.text
|
207 |
-
# yield output
|
208 |
-
# return output
|
209 |
-
|
210 |
-
|
211 |
-
# mychatbot = gr.Chatbot(
|
212 |
-
# avatar_images=["./user.png", "./bot.png"], bubble_full_width=False, show_label=False, show_copy_button=True, likeable=True,)
|
213 |
-
|
214 |
-
# demo = gr.ChatInterface(fn=generate,
|
215 |
-
# chatbot=mychatbot,
|
216 |
-
# title="Mistral-Chat",
|
217 |
-
# retry_btn=None,
|
218 |
-
# undo_btn=None
|
219 |
-
# )
|
220 |
-
|
221 |
-
# demo.queue().launch(show_api=False)
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
import gradio as gr
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
template = """
|
240 |
-
<|begin_of_text|>
|
241 |
-
{history}
|
242 |
-
<|start_header_id|>user<|end_header_id|>
|
243 |
-
{input}
|
244 |
-
<|eot_id|>
|
245 |
-
<|start_header_id|>assistant<|end_header_id|>
|
246 |
-
"""
|
247 |
-
prompt_temp = PromptTemplate(input_variables=["history", "input"], template=template)
|
248 |
|
249 |
def generate(
|
250 |
-
|
251 |
):
|
|
|
|
|
|
|
|
|
252 |
|
253 |
generate_kwargs = dict(
|
254 |
temperature=temperature,
|
255 |
-
|
256 |
-
top_p=top_p
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
conversation = ConversationChain(
|
261 |
-
prompt=prompt_temp,
|
262 |
-
llm=llm,
|
263 |
-
verbose=True,
|
264 |
-
memory= ConversationBufferMemory(ai_prefix="AI Assistant")
|
265 |
)
|
266 |
|
267 |
-
|
268 |
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
|
|
|
|
274 |
|
275 |
-
|
|
|
|
|
276 |
|
|
|
|
|
|
|
|
|
|
|
|
|
277 |
|
278 |
demo.queue().launch(show_api=False)
|
279 |
|
@@ -285,6 +228,9 @@ demo.queue().launch(show_api=False)
|
|
285 |
|
286 |
|
287 |
|
|
|
|
|
|
|
288 |
# import gradio as gr
|
289 |
# import boto3
|
290 |
# import json
|
|
|
168 |
|
169 |
|
170 |
|
171 |
+
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
import gradio as gr
|
173 |
+
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
|
174 |
+
|
175 |
+
def format_prompt(message, history):
|
176 |
+
prompt = "<s>"
|
177 |
+
for user_prompt, bot_response in history:
|
178 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
179 |
+
prompt += f" {bot_response}</s> "
|
180 |
+
prompt += f"[INST] {message} [/INST]"
|
181 |
+
return prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
|
183 |
def generate(
|
184 |
+
prompt, history, temperature=0.2, max_new_tokens=3000, top_p=0.95, repetition_penalty=1.0,
|
185 |
):
|
186 |
+
temperature = float(temperature)
|
187 |
+
if temperature < 1e-2:
|
188 |
+
temperature = 1e-2
|
189 |
+
top_p = float(top_p)
|
190 |
|
191 |
generate_kwargs = dict(
|
192 |
temperature=temperature,
|
193 |
+
max_new_tokens=max_new_tokens,
|
194 |
+
top_p=top_p,
|
195 |
+
repetition_penalty=repetition_penalty,
|
196 |
+
do_sample=True,
|
197 |
+
seed=42,
|
|
|
|
|
|
|
|
|
|
|
198 |
)
|
199 |
|
200 |
+
formatted_prompt = format_prompt(prompt, history)
|
201 |
|
202 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
203 |
+
output = ""
|
204 |
+
|
205 |
+
for response in stream:
|
206 |
+
output += response.token.text
|
207 |
+
yield output
|
208 |
+
return output
|
209 |
|
210 |
+
|
211 |
+
mychatbot = gr.Chatbot(
|
212 |
+
avatar_images=["./user.png", "./bot.png"], bubble_full_width=False, show_label=False, show_copy_button=True, likeable=True,)
|
213 |
|
214 |
+
demo = gr.ChatInterface(fn=generate,
|
215 |
+
chatbot=mychatbot,
|
216 |
+
title="Mistral-Chat",
|
217 |
+
retry_btn=None,
|
218 |
+
undo_btn=None
|
219 |
+
)
|
220 |
|
221 |
demo.queue().launch(show_api=False)
|
222 |
|
|
|
228 |
|
229 |
|
230 |
|
231 |
+
|
232 |
+
|
233 |
+
|
234 |
# import gradio as gr
|
235 |
# import boto3
|
236 |
# import json
|