GaneshK commited on
Commit
c9a5e5e
1 Parent(s): c6aec96

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -94
app.py CHANGED
@@ -168,112 +168,55 @@
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
-
223
-
224
-
225
-
226
-
227
-
228
  import gradio as gr
229
- import boto3
230
- import os
231
- from langchain.llms import Bedrock
232
- from langchain.chains import ConversationChain
233
- from langchain.memory import ConversationBufferMemory
234
- from langchain.prompts.prompt import PromptTemplate
235
- access_key_id = os.environ['aws_access_key_id']
236
- secret_access_key = os.environ['aws_secret_access_key']
237
- client = boto3.client(service_name='bedrock-runtime',region_name='us-east-1',aws_access_key_id=access_key_id,aws_secret_access_key=secret_access_key)
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
- prompt_temp, temperature=0.2, max_gen_len=1024, top_p=0.95,
251
  ):
 
 
 
 
252
 
253
  generate_kwargs = dict(
254
  temperature=temperature,
255
- max_gen_len=max_gen_len,
256
- top_p=top_p)
257
-
258
- llm = Bedrock(model_id="meta.llama3-8b-instruct-v1:0",model_kwargs=generate_kwargs,client=client)
259
-
260
- conversation = ConversationChain(
261
- prompt=prompt_temp,
262
- llm=llm,
263
- verbose=True,
264
- memory= ConversationBufferMemory(ai_prefix="AI Assistant")
265
  )
266
 
267
- chat_history = []
268
 
269
- #result =conversation.predict(input="Hi there!")
270
-
271
- result = conversation({"input": message, "history":chat_history })
272
- chat_history.append((message, result['response']))
273
- return result['response']
 
 
274
 
275
- demo=gr.ChatInterface(generate)
 
 
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