GaneshK commited on
Commit
a65856a
1 Parent(s): 7586a3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -47
app.py CHANGED
@@ -225,70 +225,123 @@
225
 
226
 
227
 
228
-
229
  import gradio as gr
230
- import boto3
231
- import json
232
- from botocore.exceptions import ClientError
233
- import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
 
235
- access_key_id = os.environ['aws_access_key_id']
236
- secret_access_key = os.environ['aws_secret_access_key']
237
 
238
 
239
- bedrock = 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)
 
 
 
 
 
 
 
 
 
 
240
 
241
 
242
- def invoke_llama3_8b(user_message):
243
- try:
244
- # Set the model ID, e.g., Llama 3 8B Instruct.
245
- model_id = "meta.llama3-8b-instruct-v1:0"
246
 
247
 
248
- # Embed the message in Llama 3's prompt format.
249
- prompt = f"""
250
- <|begin_of_text|>
251
- <|start_header_id|>user<|end_header_id|>
252
- {user_message}
253
- <|eot_id|>
254
- <|start_header_id|>assistant<|end_header_id|>
255
- """
256
 
257
- # Format the request payload using the model's native structure.
258
- request = {
259
- "prompt": prompt,
260
- # Optional inference parameters:
261
- "max_gen_len": 1024,
262
- "temperature": 0.6,
263
- "top_p": 0.9,
264
- }
265
 
266
- # Encode and send the request.
267
- response = bedrock.invoke_model(body=json.dumps(request), modelId=model_id)
268
 
269
- # Decode the native response body.
270
- model_response = json.loads(response["body"].read())
271
 
272
- # Extract and print the generated text.
273
- response_text = model_response["generation"]
274
 
275
- return response_text
276
 
277
- except ClientError:
278
- print("Couldn't invoke llama3 8B")
279
- raise
280
 
281
 
282
 
283
- mychatbot = gr.Chatbot(
284
- avatar_images=["./user.png", "./bot.png"], bubble_full_width=False, show_label=False, show_copy_button=True, likeable=True,)
285
 
286
- demo = gr.ChatInterface(fn=invoke_llama3_8b,
287
- chatbot=mychatbot,
288
- title="llama3-Chat",
289
- retry_btn=None,
290
- undo_btn=None
291
- )
292
 
293
- demo.queue().launch(show_api=False)
294
 
 
225
 
226
 
227
 
 
228
  import gradio as gr
229
+ 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)
230
+
231
+ prompt = """
232
+ <|begin_of_text|>
233
+ {history}
234
+ <|start_header_id|>user<|end_header_id|>
235
+ {input}
236
+ <|eot_id|>
237
+ <|start_header_id|>assistant<|end_header_id|>
238
+ """
239
+ prompt_temp = PromptTemplate(input_variables=["history", "input"], template=template)
240
+
241
+ def generate(
242
+ prompt_temp, temperature=0.2, max_gen_len=1024, top_p=0.95,
243
+ ):
244
+ temperature = float(temperature)
245
+ if temperature < 1e-2:
246
+ temperature = 1e-2
247
+ top_p = float(top_p)
248
+
249
+ generate_kwargs = dict(
250
+ temperature=temperature,
251
+ max_gen_len=max_gen_len,
252
+ top_p=top_p)
253
+
254
+ conversation = ConversationChain(
255
+ prompt=prompt_temp,
256
+ llm=llm,
257
+ verbose=True,
258
+ memory= ConversationBufferMemory(ai_prefix="AI Assistant")
259
+ )
260
+
261
+ chat_history = []
262
+
263
+ #result =conversation.predict(input="Hi there!")
264
+
265
+ result = conversation({"input": message, "history":chat_history })
266
+ chat_history.append((message, result['response']))
267
+ return result['response']
268
+
269
+ demo=gr.ChatInterface(qa_fn)
270
+
271
+
272
+ demo.queue().launch(show_api=False)
273
+
274
+
275
+
276
+
277
+
278
+
279
 
 
 
280
 
281
 
282
+ # import gradio as gr
283
+ # import boto3
284
+ # import json
285
+ # from botocore.exceptions import ClientError
286
+ # import os
287
+
288
+ # access_key_id = os.environ['aws_access_key_id']
289
+ # secret_access_key = os.environ['aws_secret_access_key']
290
+
291
+
292
+ # bedrock = 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)
293
 
294
 
295
+ # def invoke_llama3_8b(user_message):
296
+ # try:
297
+ # # Set the model ID, e.g., Llama 3 8B Instruct.
298
+ # model_id = "meta.llama3-8b-instruct-v1:0"
299
 
300
 
301
+ # # Embed the message in Llama 3's prompt format.
302
+ # prompt = f"""
303
+ # <|begin_of_text|>
304
+ # <|start_header_id|>user<|end_header_id|>
305
+ # {user_message}
306
+ # <|eot_id|>
307
+ # <|start_header_id|>assistant<|end_header_id|>
308
+ # """
309
 
310
+ # # Format the request payload using the model's native structure.
311
+ # request = {
312
+ # "prompt": prompt,
313
+ # # Optional inference parameters:
314
+ # "max_gen_len": 1024,
315
+ # "temperature": 0.6,
316
+ # "top_p": 0.9,
317
+ # }
318
 
319
+ # # Encode and send the request.
320
+ # response = bedrock.invoke_model(body=json.dumps(request), modelId=model_id)
321
 
322
+ # # Decode the native response body.
323
+ # model_response = json.loads(response["body"].read())
324
 
325
+ # # Extract and print the generated text.
326
+ # response_text = model_response["generation"]
327
 
328
+ # return response_text
329
 
330
+ # except ClientError:
331
+ # print("Couldn't invoke llama3 8B")
332
+ # raise
333
 
334
 
335
 
336
+ # mychatbot = gr.Chatbot(
337
+ # avatar_images=["./user.png", "./bot.png"], bubble_full_width=False, show_label=False, show_copy_button=True, likeable=True,)
338
 
339
+ # demo = gr.ChatInterface(fn=invoke_llama3_8b,
340
+ # chatbot=mychatbot,
341
+ # title="llama3-Chat",
342
+ # retry_btn=None,
343
+ # undo_btn=None
344
+ # )
345
 
346
+ # demo.queue().launch(show_api=False)
347