bedrock / app.py
bstraehle's picture
Update app.py
555ef07
raw
history blame
No virus
1.9 kB
import gradio as gr
import boto3, json, os
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
aws_access_key_id = os.environ['AWS_ACCESS_KEY_ID']
aws_secret_access_key = os.environ['AWS_SECRET_ACCESS_KEY']
bedrock_runtime = boto3.client(
aws_access_key_id = aws_access_key_id,
aws_secret_access_key = aws_secret_access_key,
service_name = "bedrock-runtime",
region_name = "us-west-2"
)
def invoke(prompt):
#args = {
# "modelId": "anthropic.claude-v2",
# "contentType": "application/json",
# "accept": "*/*",
# "body": "{\"prompt\":\"Human: " + prompt + "\\nAssistant:\",\"max_tokens_to_sample\":300,\"temperature\":1,\"top_k\":250,\"top_p\":0.999,\"stop_sequences\":[\"\\n\\nHuman:\"],\"anthropic_version\":\"bedrock-2023-05-31\"}"
#}
body = json.dumps({"prompt": "Human: " + prompt + "\nAssistant:",
"max_tokens_to_sample": 300,
"temperature": 1,
"top_k": 250,
"top_p": 0.999,
"stop_sequences": ["\n\nHuman:"]
})
print(body)
modelId = "anthropic.claude-v2"
accept = "application/json"
contentType = "application/json"
response = bedrock_runtime.invoke_model(body=body, modelId=modelId, accept=accept, contentType=contentType)
response_body = json.loads(response.get("body").read())
return response_body["completion"]
gr.close_all()
demo = gr.Interface(fn=invoke,
inputs = [gr.Textbox(label = "Prompt", lines = 1)],
outputs = [gr.Textbox(label = "Result", lines = 1)],
title = "Generative AI - Text",
description = "<a href='https://www.gradio.app/'>Gradio</a> UI using <a href='https://aws.amazon.com/bedrock/'>Amazon Bedrock</a> API with Claude 2 foundation model")
demo.launch()