Ganesh Karbhari
commited on
Commit
•
a65856a
1
Parent(s):
7586a3f
Update app.py
Browse files
app.py
CHANGED
@@ -225,70 +225,123 @@
|
|
225 |
|
226 |
|
227 |
|
228 |
-
|
229 |
import gradio as gr
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
234 |
|
235 |
-
access_key_id = os.environ['aws_access_key_id']
|
236 |
-
secret_access_key = os.environ['aws_secret_access_key']
|
237 |
|
238 |
|
239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
240 |
|
241 |
|
242 |
-
def invoke_llama3_8b(user_message):
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
|
247 |
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
|
266 |
-
|
267 |
-
|
268 |
|
269 |
-
|
270 |
-
|
271 |
|
272 |
-
|
273 |
-
|
274 |
|
275 |
-
|
276 |
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
|
281 |
|
282 |
|
283 |
-
mychatbot = gr.Chatbot(
|
284 |
-
|
285 |
|
286 |
-
demo = gr.ChatInterface(fn=invoke_llama3_8b,
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
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 |
|